mercredi 25 mars 2015

What does postProcessBeforeInitialization means in Spring BeanPostProcessor?

In XML file



<bean id="triangle" class="com.company.aop.model.Triangle">
<property name="name" value="myTriangle"></property>
</bean>

<bean class="com.company.aop.DisplayNameBeanPostProcessor"></bean>


In DisplayNameBeanPostProcessor.java class



public class DisplayNameBeanPostProcessor implements BeanPostProcessor{

@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
// TODO Auto-generated method stub
if(bean instanceof Triangle) {
// System.out.println("Tr "+(((Triangle) bean).getName().toString()));
System.out.println("I am after intialisation");
}
return bean;
}

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
// TODO Auto-generated method stub
if(bean instanceof Triangle) {
System.out.println("Tr "+(((Triangle) bean).getName().toString()));
}
return bean;
}

}


Now when i run this code, it goes to postProcessBeforeInitialization() method with argument bean and beanName and prints the message "myTriangle". This bean has information like its name field with value "myTriangle" in my case. But the method signature says that it is before initialisation then what is this bean that has been passed into it if it has not been initialised yet ? And what is the difference between



public Object postProcessAfterInitialization(Object bean, String beanName)


and



public Object postProcessBeforeInitialization(Object bean, String beanName)


Why this line



System.out.println("Tr "+(((Triangle) bean).getName().toString()));


prints the name in method postProcessBeforeInitialization if the method has been called before initialisation ?


Aucun commentaire:

Enregistrer un commentaire