Example usage for org.springframework.context.support GenericApplicationContext addBeanFactoryPostProcessor

List of usage examples for org.springframework.context.support GenericApplicationContext addBeanFactoryPostProcessor

Introduction

In this page you can find the example usage for org.springframework.context.support GenericApplicationContext addBeanFactoryPostProcessor.

Prototype

@Override
    public void addBeanFactoryPostProcessor(BeanFactoryPostProcessor postProcessor) 

Source Link

Usage

From source file:org.pentaho.platform.engine.core.system.objfac.spring.PentahoBeanScopeValidatorPostProcessorTest.java

@Override
protected ApplicationContext getSpringApplicationContext() {

    GenericApplicationContext appCtx = (GenericApplicationContext) super.getSpringApplicationContext();

    if (appCtx != null) {
        appCtx.addBeanFactoryPostProcessor(new PentahoBeanScopeValidatorPostProcessorTester()); // add our postProcessor
    }/*w ww. j  a  v a2 s . c  o  m*/

    return appCtx;
}

From source file:org.springframework.context.expression.ApplicationContextExpressionTests.java

@Test
public void genericApplicationContext() throws Exception {
    GenericApplicationContext ac = new GenericApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);

    ac.getBeanFactory().registerScope("myScope", new Scope() {
        @Override//  w w  w .ja va2 s . c om
        public Object get(String name, ObjectFactory<?> objectFactory) {
            return objectFactory.getObject();
        }

        @Override
        public Object remove(String name) {
            return null;
        }

        @Override
        public void registerDestructionCallback(String name, Runnable callback) {
        }

        @Override
        public Object resolveContextualObject(String key) {
            if (key.equals("mySpecialAttr")) {
                return "42";
            } else {
                return null;
            }
        }

        @Override
        public String getConversationId() {
            return null;
        }
    });

    PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
    Properties placeholders = new Properties();
    placeholders.setProperty("code", "123");
    ppc.setProperties(placeholders);
    ac.addBeanFactoryPostProcessor(ppc);

    GenericBeanDefinition bd0 = new GenericBeanDefinition();
    bd0.setBeanClass(TestBean.class);
    bd0.getPropertyValues().add("name", "myName");
    bd0.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "original"));
    ac.registerBeanDefinition("tb0", bd0);

    GenericBeanDefinition bd1 = new GenericBeanDefinition();
    bd1.setBeanClassName("#{tb0.class}");
    bd1.setScope("myScope");
    bd1.getConstructorArgumentValues().addGenericArgumentValue("XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ");
    bd1.getConstructorArgumentValues().addGenericArgumentValue("#{mySpecialAttr}");
    ac.registerBeanDefinition("tb1", bd1);

    GenericBeanDefinition bd2 = new GenericBeanDefinition();
    bd2.setBeanClassName("#{tb1.class.name}");
    bd2.setScope("myScope");
    bd2.getPropertyValues().add("name", "{ XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ }");
    bd2.getPropertyValues().add("age", "#{mySpecialAttr}");
    bd2.getPropertyValues().add("country", "${code} #{systemProperties.country}");
    ac.registerBeanDefinition("tb2", bd2);

    GenericBeanDefinition bd3 = new GenericBeanDefinition();
    bd3.setBeanClass(ValueTestBean.class);
    bd3.setScope("myScope");
    ac.registerBeanDefinition("tb3", bd3);

    GenericBeanDefinition bd4 = new GenericBeanDefinition();
    bd4.setBeanClass(ConstructorValueTestBean.class);
    bd4.setScope("myScope");
    ac.registerBeanDefinition("tb4", bd4);

    GenericBeanDefinition bd5 = new GenericBeanDefinition();
    bd5.setBeanClass(MethodValueTestBean.class);
    bd5.setScope("myScope");
    ac.registerBeanDefinition("tb5", bd5);

    GenericBeanDefinition bd6 = new GenericBeanDefinition();
    bd6.setBeanClass(PropertyValueTestBean.class);
    bd6.setScope("myScope");
    ac.registerBeanDefinition("tb6", bd6);

    System.getProperties().put("country", "UK");
    try {
        ac.refresh();

        TestBean tb0 = ac.getBean("tb0", TestBean.class);

        TestBean tb1 = ac.getBean("tb1", TestBean.class);
        assertEquals("XXXmyNameYYY42ZZZ", tb1.getName());
        assertEquals(42, tb1.getAge());

        TestBean tb2 = ac.getBean("tb2", TestBean.class);
        assertEquals("{ XXXmyNameYYY42ZZZ }", tb2.getName());
        assertEquals(42, tb2.getAge());
        assertEquals("123 UK", tb2.getCountry());

        ValueTestBean tb3 = ac.getBean("tb3", ValueTestBean.class);
        assertEquals("XXXmyNameYYY42ZZZ", tb3.name);
        assertEquals(42, tb3.age);
        assertEquals(42, tb3.ageFactory.getObject().intValue());
        assertEquals("123 UK", tb3.country);
        assertEquals("123 UK", tb3.countryFactory.getObject());
        System.getProperties().put("country", "US");
        assertEquals("123 UK", tb3.country);
        assertEquals("123 US", tb3.countryFactory.getObject());
        System.getProperties().put("country", "UK");
        assertEquals("123 UK", tb3.country);
        assertEquals("123 UK", tb3.countryFactory.getObject());
        assertSame(tb0, tb3.tb);

        tb3 = (ValueTestBean) SerializationTestUtils.serializeAndDeserialize(tb3);
        assertEquals("123 UK", tb3.countryFactory.getObject());

        ConstructorValueTestBean tb4 = ac.getBean("tb4", ConstructorValueTestBean.class);
        assertEquals("XXXmyNameYYY42ZZZ", tb4.name);
        assertEquals(42, tb4.age);
        assertEquals("123 UK", tb4.country);
        assertSame(tb0, tb4.tb);

        MethodValueTestBean tb5 = ac.getBean("tb5", MethodValueTestBean.class);
        assertEquals("XXXmyNameYYY42ZZZ", tb5.name);
        assertEquals(42, tb5.age);
        assertEquals("123 UK", tb5.country);
        assertSame(tb0, tb5.tb);

        PropertyValueTestBean tb6 = ac.getBean("tb6", PropertyValueTestBean.class);
        assertEquals("XXXmyNameYYY42ZZZ", tb6.name);
        assertEquals(42, tb6.age);
        assertEquals("123 UK", tb6.country);
        assertSame(tb0, tb6.tb);
    } finally {
        System.getProperties().remove("country");
    }
}

From source file:uk.co.modularaudio.util.spring.SpringComponentHelper.java

public GenericApplicationContext makeAppContext(final String beansResourcePath, final String configResourcePath,
        final String[] additionalBeansResources, final String[] additionalConfigResources)
        throws DatastoreException {
    GenericApplicationContext appContext = null;

    final Class<SpringComponentHelper> thisClass = SpringComponentHelper.class;

    // Do any work needed before we instantiate the context
    for (final SpringContextHelper helper : contextHelpers) {
        try {/*from www  .j  a va 2 s .c  o  m*/
            helper.preContextDoThings();
        } catch (final Exception ce) {
            final String msg = "Exception caught calling precontext of helper " + helper.getClass() + ": "
                    + ce.toString();
            log.error(msg, ce);
            // Will halt context creation.
            throw new DatastoreException(msg, ce);
        }
    }

    try {
        final InputStream bIStream = thisClass.getResourceAsStream(beansResourcePath);
        if (bIStream != null) {
            final InputSource bISource = new InputSource(bIStream);

            appContext = new GenericApplicationContext();
            appContext.addBeanFactoryPostProcessor(beanInstantiationList);

            final XmlBeanDefinitionReader xbdr = new XmlBeanDefinitionReader(appContext);

            xbdr.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
            xbdr.loadBeanDefinitions(bISource);

            // And load any additional beans files now we've loaded the "driver"
            if (additionalBeansResources != null && additionalBeansResources.length > 0) {
                for (final String additionalBeansFilename : additionalBeansResources) {
                    InputStream aBiStream = null;
                    InputSource aBiSource = null;
                    try {
                        aBiStream = thisClass.getResourceAsStream(additionalBeansFilename);

                        if (aBiStream != null) {
                            aBiSource = new InputSource(aBiStream);
                            xbdr.loadBeanDefinitions(aBiSource);
                        } else {
                            throw new DatastoreException(
                                    "Failed to load additional services from file: " + additionalBeansFilename);
                        }
                    } finally {
                        if (aBiStream != null) {
                            aBiStream.close();
                        }
                    }
                }
            }

            final BeanDefinition bd = appContext.getBeanDefinition("configurationService");
            final MutablePropertyValues mpvs = bd.getPropertyValues();
            // Push in the configuration file if one needs to be set
            if (configResourcePath != null) {
                mpvs.removePropertyValue(CONFIG_RESOURCE_PATH_PROPERTY);
                mpvs.addPropertyValue(CONFIG_RESOURCE_PATH_PROPERTY, configResourcePath);

            }
            if (additionalConfigResources != null && additionalConfigResources.length > 0) {
                mpvs.removePropertyValue(ADDITIONAL_RESOURCE_PATHS);
                mpvs.addPropertyValue(ADDITIONAL_RESOURCE_PATHS, additionalConfigResources);
            }

            // Do any work needed before we refresh the context
            // Prerefresh
            for (final SpringContextHelper helper : contextHelpers) {
                try {
                    helper.preRefreshDoThings(appContext);
                } catch (final Exception prer) {
                    final String msg = "Exception caught calling prerefresh of helper " + helper.getClass()
                            + ": " + prer.toString();
                    log.error(msg, prer);
                    // Will halt context creation
                    throw new DatastoreException(msg, prer);
                }
            }

            appContext.refresh();

        } else {
            // Didn't find the beans file
            final String msg = "Unable to find beans file: " + beansResourcePath;
            log.error(msg);
            throw new DatastoreException(msg);
        }
    } catch (final Exception e) {
        final String msg = "Exception caught setting up app context: " + e.toString();
        log.error(msg, e);
        throw new DatastoreException(msg, e);
    }

    // Perform a GC pass here to clean up before things are launched post refresh
    Runtime.getRuntime().gc(); // NOPMD by dan on 30/07/15 15:00

    // Do any work needed after we refresh the context
    // Post refresh calls
    for (final SpringContextHelper helper : contextHelpers) {
        try {
            helper.postRefreshDoThings(appContext, beanInstantiationList);
        } catch (final Exception pre) {
            final String msg = "Exception caught calling postrefresh of helper " + helper.getClass() + ": "
                    + pre.toString();
            log.error(msg, pre);
            // Will halt context creation
            throw new DatastoreException(msg, pre);
        }
    }

    return appContext;
}