Bean后处理器是一种特殊的 Bean,这种特殊的Bean不对外提供服务,它主要负责对容器中的其他Bean执行后处理。Bean后处理器必须实现BeanPostProcessor接口,该接口有两个方法:
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException
这两个方法分别会在目标Bean初始化之前、初始化之后被回调,这两个方法用于对容器中的Bean实列进行增强处理。
Bean后处理器代码如下:
package com.custle.spring.ioc;
import com.custle.spring.impl.Chinese;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
* Created by admin on 2018/3/2.
*/
public class MyBeanPostProcessor implements BeanPostProcessor {
/**
* 对容器中的Bean实列进行后处理
* @param bean 需要进行后处理的原Bean实列
* @param beanName 需要进行后处理的Bean实列的名字
* @return 返回处理完成后的Bean
* @throws BeansException
*/
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("Bean 处理器在初始化之前对 " + beanName + " 进行增强处理。。。");
//返回增强处理之后的Bean
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("Bean 后处理器在初始化之后对 "+ beanName + " 进行增强处理。。。");
if(bean instanceof Chinese){
//修改chinese的值
Chinese chinese = (Chinese)bean;
System.out.println(chinese.sayHello("java"));
return chinese;
}
return bean;
}
}
<bean id="chian" class="com.custle.spring.impl.Chinese" />
<bean id="myBeanPostProcessor" class="com.custle.spring.ioc.MyBeanPostProcessor"/>
定义Chinese:
package com.custle.spring.impl;
import com.custle.spring.inter.Person;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
/**
* Created by admin on 2018/3/1.
*/
public class Chinese implements Person,InitializingBean {
@Override
public String sayHello(String name) {
return name + ",您好!";
}
@Override
public String sayGoodBye(String name) {
return name + ",再见!";
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("正在执行初始化方法 afterPropertiesSet()");
}
}
测试程序:
package com.custle.spring.test;
import com.custle.spring.inter.Person;
import com.custle.spring.ioc.MyBeanPostProcessor;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
/**
* Created by admin on 2018/3/2.
*/
public class BeanTest {
public static void main(String[] args) {
//CLASSPATH路径下的xml创建Resource对象
ClassPathResource resource = new ClassPathResource("applicationContext-spring.xml");
//以Resource对象作为参数,创建BeanFactory实列
XmlBeanFactory factory = new XmlBeanFactory(resource);
MyBeanPostProcessor myBeanPostProcessor = factory.getBean("myBeanPostProcessor", MyBeanPostProcessor.class);
//注册BeanPostProcessor实列
factory.addBeanPostProcessor(myBeanPostProcessor);
System.out.println("程序已经实列化BeanFactory...");
Person p = factory.getBean("chian" , Person.class);
System.out.println("程序中已经完成了 chinese bean 的实列化");
}
}
控制台输出:
程序已经实列化BeanFactory...
Bean 处理器在初始化之前对 chian 进行增强处理。。。
正在执行初始化方法 afterPropertiesSet()
Bean 后处理器在初始化之后对 chian 进行增强处理。。。
java,您好!
程序中已经完成了 chinese bean 的实列化
需要注意的是:容器中一旦注册了Bean后处理器,Bean后处理器就会自动启动,在容器中每个Bean创建时自动工作,加入Bean后处理器需要完成的工作。Bean后处理器两个方法的回调时机如下:
实现BeanPostProstProcessor接口的Bean后处理器可对Bean进行任何操作,如果采用BeanFactory作为Spring容器时,必须手动注册BeanPostProcessor;如果采用ApplicationConext作为Spring容器,则无需手动注册Bean后处理器。ApplicationContext可以自动检测到容器中的Bean处理器,自动注册。