BeanPostProcessor is an interface in Spring which helps the java developer to do some work when each an every bean is initialized. It does not matter weather you have one bean or many. The post processor acts on all of the beans.
First a class that implements the BeanPostProcessor should be created. The BeanPostProcessor implements two methods. One which does work before initializing a bean and another which does work after initializing a bean.
public class MyBeanPostProcessor implements BeanPostProcessor{
//two methods that the BeanPostProcessor goes here(Use the ide to get the methods)
// arguments in the methods are the bean itself and the name of the bean
}
Then you need to declare the implemented class in the xml file as a bean
<bean class="package.MyBeanPostProcessor" />
Note that we have not used an id or a name for the bean. This is because the bean only needs to be declared in the xml file and is not used in any other bean.
Another post processor that is used in spring is the BeanFactoryPostProcessor which does work when the bean factory is initialized. The procedure is same as above except that this time the class extends the BeanFactoryPostProcessor.
One example for a BeanFactoryPostProcessor is the PropertyPlaceholderConfigurer in spring framework.
You can store values in a filename.properties file and these values can be used in properties of a bean by using the PropertyPlaceholder.
No comments:
Post a Comment