Spring Bean Post Processor

Spring  Bean Post Processor

Spring provides two types of post processors

1. Bean post-processor

2. Container Post Processor

 

Many functions of Spring are to provide high-quality services through these post-processing. And programmers only need a few simple post-processors that can also customize powerful functions. Today we will briefly describe the Bean post-processor.

 

1. Bean post-processor

Used to enhance bean handling in the Spring container. Custom processing of beans can be done before and after the bean is initialized in the spring container.

1. Customize a custom bean post-processor:

       public class MyBeanProcessor implements BeanPostProcessor{

               @Override

               public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {

                       System.out.println("bean:"+arg1+" after");

                       return arg0;

               }

               @Override

               public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException{

                      System.out.println("bean:"+arg1+" before");

                      return arg0;

               }

        }

Bean post-processor requires the implementation of the interface BeanPostProcessor

This interface requires two methods to be implemented:

       Object postProcessBeforeInitialization(Object object,String name)

              Spring will call back this method before the bean is initialized

              Parameters: object bean to be postprocessed

                               name is the id of the post-processed bean

              Returns: the processed bean

       Object postProcessAfterInitialization(Object object,String name)

              Spring will call back this method after the bean is initialized

              Parameters: object bean to be postprocessed

                               name is the id of the post-processed bean

              Returns: the processed bean

2. Register the bean post-processor to the container

       <!-- bean post-processing bean -->

<bean class="cn.zhaoyuening.utils.MyBeanProcessor"></bean>

In the process of bean initialization, if Spring finds that the bean implements the BeanPostProcessor interface, it will register it as a bean post-processor, which acts on all beans in the spring container. Any bean will be additionally enhanced by the bean post-processor during the initialization process. operate.

3. When the post-processor is called during Bean initialization



 

 

4. Next we make a small demo

Create a Persion Bean:

 

public class Persion{

        private String name;

        private int age;

        private char gender;

 

        public void work(){

                System.out.println(name+"working...");

        }

        public void init(){

                System.out .println (" persion bean is initialized");

        }

        //Omit some get&set methods

}

register:

//Declare the initialization method as init, which will be called at the beginning of the initialization process

<bean id="persion" class="cn.zhaoyuening.model.Persion" init-method="init"></bean>

Create a Bean post-processor:



  

      Bean handler after registration:

      <!-- bean post-processing bean -->

      <bean class="cn.zhaoyuening.utils.MyBeanProcessor"></bean>

      There is no id value set for the post-processing bean here, because when spring automatically loads all beans, it finds that a bean that implements the BeanPostProcessor interface will be automatically registered as a post-processing bean.

But if the bean is not automatically loaded such as using (BeanFactory), you need to set the id value and register it manually:

<bean class="cn.zhaoyuening.utils.MyBeanProcessor" id=”processor”></bean>

BeanPostProcessor processor = (BeanPostProcessor)baenFactory.getBean(“processor”);

beanFactory.addBeanPostProcessor(processor);

 

       implement:

private ApplicationContext beansContext = new ClassPathXmlApplicationContext("applicationContext.xml");

       @Test

       public void test() throws Exception {

               Persion persion = beansContext.getBean("persion",Persion.class);

               persion.work();

}

       result:

bean:persion before #Execute the postProcessBeforeInitialization method of the bean post-processor

session bean is initialized #Bean initialized

bean:persion after #Execute the postProcessAfterInitialization method of the bean post processor

#Execute the initialization method of the persistence bean, and set the name in the post-processor to buynow

buynow working... 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326948268&siteId=291194637