Search in sources :

Example 1 with InstantiationAwareBeanPostProcessorAdapter

use of org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method doTestFieldSettingWithInstantiationAwarePostProcessor.

private void doTestFieldSettingWithInstantiationAwarePostProcessor(final boolean skipPropertyPopulation) {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    int ageSetByPropertyValue = 27;
    bd.getPropertyValues().addPropertyValue(new PropertyValue("age", new Integer(ageSetByPropertyValue)));
    lbf.registerBeanDefinition("test", bd);
    final String nameSetOnField = "nameSetOnField";
    lbf.addBeanPostProcessor(new InstantiationAwareBeanPostProcessorAdapter() {

        @Override
        public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
            TestBean tb = (TestBean) bean;
            try {
                Field f = TestBean.class.getDeclaredField("name");
                f.setAccessible(true);
                f.set(tb, nameSetOnField);
                return !skipPropertyPopulation;
            } catch (Exception ex) {
                fail("Unexpected exception: " + ex);
                // Keep compiler happy about return
                throw new IllegalStateException();
            }
        }
    });
    lbf.preInstantiateSingletons();
    TestBean tb = (TestBean) lbf.getBean("test");
    assertEquals("Name was set on field by IAPP", nameSetOnField, tb.getName());
    if (!skipPropertyPopulation) {
        assertEquals("Property value still set", ageSetByPropertyValue, tb.getAge());
    } else {
        assertEquals("Property value was NOT set and still has default value", 0, tb.getAge());
    }
}
Also used : InstantiationAwareBeanPostProcessorAdapter(org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) PropertyValue(org.springframework.beans.PropertyValue) ParseException(java.text.ParseException) TypeMismatchException(org.springframework.beans.TypeMismatchException) NotWritablePropertyException(org.springframework.beans.NotWritablePropertyException) ExpectedException(org.junit.rules.ExpectedException) MalformedURLException(java.net.MalformedURLException) BeansException(org.springframework.beans.BeansException) Field(java.lang.reflect.Field) ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeansException(org.springframework.beans.BeansException)

Aggregations

Field (java.lang.reflect.Field)1 MalformedURLException (java.net.MalformedURLException)1 ParseException (java.text.ParseException)1 ExpectedException (org.junit.rules.ExpectedException)1 BeansException (org.springframework.beans.BeansException)1 NotWritablePropertyException (org.springframework.beans.NotWritablePropertyException)1 PropertyValue (org.springframework.beans.PropertyValue)1 TypeMismatchException (org.springframework.beans.TypeMismatchException)1 InstantiationAwareBeanPostProcessorAdapter (org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter)1 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)1 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)1 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)1 ITestBean (org.springframework.tests.sample.beans.ITestBean)1 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)1 TestBean (org.springframework.tests.sample.beans.TestBean)1