List of usage examples for org.springframework.beans.factory.config InstantiationAwareBeanPostProcessorAdapter InstantiationAwareBeanPostProcessorAdapter
InstantiationAwareBeanPostProcessorAdapter
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()); } }