(1)bean的后置处理器允许在调用初始化方法前后对bean进行额外的处理。
(2)bean的后置处理器对IOC容器里的所有Bean实例逐一处理,而非单一的实例。其典型应用是:检查Bean属性的正确性或根据特定的标准更改bean的属性。
(3)对Bean后置处理器而言,需要实现Interface BeanPostProcessor接口,在初始化方法被调用前后,spring将把每个bean的实例分别传递给上述接口的以下两个方法。
postProcessAfterInitialization、postProcessBeforeInitialization
添加bean后置处理器之后的bean的生命周期:
- 通过构造器或工厂方法创建bean的实例;
- 为bean的属性设置值并对其他bean的引用;
- 将bean的实例传递给Bean的后置处理器的postProcessBeforeInitialization方法;
- 调用Bean的初始化方法;
- 将bean的实例传递给Bean的后置处理器的postProcessAfterInitialization方法;
- bean可以被使用了;
- 当容器关闭时,调用bean的销毁方法;
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的构造器"); } 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:实例本身 * beanName:IOC容器配置的bean的名字 * 返回值是实际上是返回给用户的那个bean,注意:可以在以下两个方法中修改返回的Bean, * 甚至返回一个新的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 bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("postProcessBeforeInitialization"+", "+bean+", "+beanName); //由于会对所有的bean都会执行,所以要判断,指定想要处理的Bean if("car".equals(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 Main { public static void main(String[] args) { //1.创建spring的IOC容器对象 ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans-cycle.xml"); //2.从容器中获取Bean实例 Car car = (Car) ctx.getBean("car"); System.out.println(car.toString()); ctx.close(); } }
输出:
我们要关注两个位置:
(1) 与不加后置处理器的bean的生命周期相比,加了之后的多出了两个过程。
(2)在postProcessBeforeInitialization中修改了Car对象的属性并返回给用户时,在Main.java中得到名为car的bean的name的属性值也发生了相应的变化。