Bean 后置处理器允许在调用初始化方法前后对 Bean 进行额外的处理
在**Spring**容器通过插入一个或多个**BeanPostProcessor**的实现来完成实例化,配置和初始化一个bean之后实现一些自定义逻辑回调方法
配置多个**BeanPostProcessor**接口,通过设置**BeanPostProcessor**实现的Ordered接口提供的**Order**属性来控制这些**BeanPostProcessor**接口的执行顺序,实现**Ordered.getOrder()**方法,返回值越小,执行顺序越靠前
在自定义的的**BeanPostProcessor**接口实现类中,要实现以下的两个抽象方法**BeanPostProcessor.postProcessBeforeInitialization(Object, String)**和**BeanPostProcessor.postProcessAfterInitialization(Object, String)**,注意命名要准确,否则会出现:**“ The type InitHelloWorld must implement the inherited abstract method BeanPostProcessorpostProcessBeforeInitialization(Object, String) ”**之类的错误
案例代码:
Person.java
package com.baklib.custom;public class Person {private String name;private String carType;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getCarType() {return carType;}public void setCarType(String carType) {this.carType = carType;}public void buyCar(){System.out.println(this.getName()+"购买了"+this.getCarType()+"!");}public void init(){System.out.println("进入4S店");}public void destroy(){System.out.println("离开4S店");}}
InitPerson0.java
package com.baklib.custom;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanPostProcessor;import org.springframework.core.Ordered;public class InitPerson0 implements BeanPostProcessor, Ordered {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println(this.getClass()+" BeforeInitialization0: "+beanName+"\tOrder: "+this.getOrder());return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println(this.getClass()+" AfterInitialization0: "+beanName+"\tOrder: "+this.getOrder());return bean;}@Overridepublic int getOrder() {return 5;}}
InitPerson1.java
package com.baklib.custom;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanPostProcessor;import org.springframework.core.Ordered;public class InitPerson1 implements BeanPostProcessor, Ordered {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println(this.getClass()+" BeforeInitialization1: "+beanName+"\tOrder: "+this.getOrder());return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println(this.getClass()+" AfterInitialization1: "+beanName+"\tOrder: "+this.getOrder());return bean;}@Overridepublic int getOrder() {return 3;}}
MainApp.java
package com.baklib.custom;import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp {public static void main(String[] args) {AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");Person person = (Person) context.getBean("person");person.buyCar();context.registerShutdownHook();}}
配置文件 Beans.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="person" class="com.baklib.custom.Person" init-method="init" destroy-method="destroy"><property name="name" value="张三" /><property name="carType" value="小轿车" /></bean><bean class="com.baklib.custom.InitPerson0" /><bean class="com.baklib.custom.InitPerson1" /></beans>
编写完以上代码,就可以运行这个程序,运行结束后,可以在控制台看到下面的信息
class com.baklib.custom.InitPerson1 BeforeInitialization1: person Order: 3class com.baklib.custom.InitPerson0 BeforeInitialization0: person Order: 5进入4S店class com.baklib.custom.InitPerson1 AfterInitialization1: person Order: 3class com.baklib.custom.InitPerson0 AfterInitialization0: person Order: 5张三购买了小轿车!离开4S店
