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());
}
}
Aggregations