Add the bean spring of the life cycle of the postprocessor

(. 1) of the post-processor allows the bean to bean additional processing before and after the initialization method is called.

(2) bean post-processor processing one by one for all the Bean instance IOC container, rather than a single instance. Typical applications are: checking the correctness Bean property or change the bean properties based on specific criteria.

(3) For the Bean postprocessor Interface BeanPostProcessor need to implement the interface, before and after the initialization method is invoked, spring will each instance passed to the bean are the following two methods described above interfaces.

postProcessAfterInitialization、postProcessBeforeInitialization

Add bean life cycle after the bean post-processors:

  • Create an instance constructor or bean plant via methods;
  • Bean and other bean references to set the value of the property;
  • Examples of the method of the bean is transmitted to postProcessBeforeInitialization Bean post-processor;
  • Initialization method is called the Bean;
  • Examples of the method of the bean is transmitted to postProcessAfterInitialization Bean post-processor;
  • bean may be used;
  • When the container is closed, call the bean method of destruction;

Car.java

package com.gong.spring.beans.cycle;

public class Car {
    private String name;
    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println("setName...");
        this.name = name;
    }

    public Car() {
        System.out.println (     "CAR constructor" );
    }
    
    public void init() {
        System.out.println("init...");
    }
    public void destroy() {
        System.out.println("destroy...");
    }

    @Override
    public String toString() {
        return "Car [name=" + name + "]";
    }
    
    
}

myBeanPostProcessor.java

package com.gong.spring.beans.cycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanPostProcessor implements BeanPostProcessor {
    
    /*
     * Bean: instance itself
     * BeanName: bean names IOC container configuration
     * The return value is actually returned to the bean user Note: You can modify Bean returned in the following two methods,
     * Even return a new bean
     * */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        // TODO Auto-generated method stub
        System.out.println("postProcessAfterInitialization"+", "+bean+", "+beanName);
        Car car = new Car();
        car.setName("benchi");
        return car;
    }

    @Override
    public Object postProcessBeforeInitialization (Object the bean, the beanName String) throws BeansException {
         // the TODO Auto-Generated Stub Method 
        System.out.println ( "postProcessBeforeInitialization" + "," + the bean + "," + the beanName);
         // Since on all the bean will be executed, it is determined to specify the desired bean processed 
        IF ( "CAR" .equals (the beanName)) {
             // ... 
        }
         return bean;
    }

}

beans-cycle.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="car" class="com.gong.spring.beans.cycle.Car" init-method="init"
    destroy-method="destroy">
        <property name="name" value="baoma"></property>
    </bean>
    <!-- 配置Bean的后置处理器 -->
    <bean class="com.gong.spring.beans.cycle.MyBeanPostProcessor"></bean>
</beans>

Main.java

package com.gong.spring.beans.cycle;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public  class the Main {
     public  static  void main (String [] args) {
         // 1. Create spring IOC container object of 
        the ClassPathXmlApplicationContext CTX = new new the ClassPathXmlApplicationContext ( "Beans-cycle.xml" );
         // 2. Bean instance acquired from the container 
        = CAR CAR (CAR) ctx.getBean ( "CAR" );
        System.out.println(car.toString());
        ctx.close();
    }
}

Output:

We want to focus on two locations:

(1) as compared to the post-processor without bean life cycle, after the addition of the extra two processes.

(2) modify the attributes in postProcessBeforeInitialization Car object and returned to the user, resulting in the car named attribute name value Main.java bean also undergone a corresponding change.

Guess you like

Origin www.cnblogs.com/xiximayou/p/12153043.html