Example usage for org.springframework.beans.factory.config InstantiationAwareBeanPostProcessorAdapter InstantiationAwareBeanPostProcessorAdapter

List of usage examples for org.springframework.beans.factory.config InstantiationAwareBeanPostProcessorAdapter InstantiationAwareBeanPostProcessorAdapter

Introduction

In this page you can find the example usage for org.springframework.beans.factory.config InstantiationAwareBeanPostProcessorAdapter InstantiationAwareBeanPostProcessorAdapter.

Prototype

InstantiationAwareBeanPostProcessorAdapter

Source Link

Usage

From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java

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/*from  www . j  a v  a  2s .c  om*/
        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());
    }
}