BeanFactoryPostProcessor: 这个接口是spring对外开放,实现这个接口,可以在bean定义对象没有实例化之前,读取bean定义对象配置元数据,并可以对配置元数据的一些属性修改。也就是说:BeanFactoryPostProcessor是在spring容器加载了bean的定义文件之后,在bean实例化之前执行的。在整个过程中,只调用一次这个接口的实现类。如下定义的bean ,可以对lazy-init、property的属性值进行修改。
<bean id="myTestBean" class="com.test.processor.MyTestBean" init-method="initMethod" lazy-init="false">
<property name="name" value="Tom" />
</bean>
BeanPostProcessor:是spring容器加载bean的定义文件完,且实例化了bean的定义的对象之后执行的。BeanPostProcessor接口有两个方法,一个是postProcessBeforeInitialization,一个是postProcessAfterInitialzation。postProcessBeforeInitialization方法是在对象的构成函数执行完之后,postProcessBeforeInitialization方法是定义的init-method方法之前执行的。当对象实例化完之后就是再次执行postProcessAfterInitialzation方法。 在整个过程中,每实例化一个bean的对象,都会是调到BeanPostProcessor接口的方法。
示例如下:
步骤:
1、 创建两个类,一个是Teacher,一个是student类
2、创建两个类MyBeanFactoryPostProcessor与MyBeanPostProcessor,分别实现BeanFactoryPostProcessor与BeanPostProcessor
3、配置bean文件:bean-postprocessor.xml
4、测试类 MainDemo
1、 创建两个类,一个是Teacher,一个是student类
package cn.lvyjx.test.postProcessor;
public class Teacher {
public String name;
public String phone;
public Teacher(){
System.out.println("Teacher construct!");
}
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("teacher中name值的注入");
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "name="+name+";phone="+phone;
}
/**
* 初始化方法
*/
public void init(){
System.out.println("Teacher-init method");
}
}
package cn.lvyjx.test.postProcessor;
public class Student {
public String name;
public String numberId;
public Student(){
System.out.println("Student construct!");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumberId() {
return numberId;
}
public void setNumberId(String numberId) {
this.numberId = numberId;
}
}
2、创建两个类,分别实现BeanFactoryPostProcessor与BeanPostProcessor
package cn.lvyjx.test.postProcessor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
/**
* 对象实例化前执行
* @author Administrator
*
*/
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor{
public MyBeanFactoryPostProcessor(){
System.out.println("【BeanFactoryPostProcessor】:MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor construct!");
}
@Override
public void postProcessBeanFactory(
ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("【BeanFactoryPostProcessor】:postProcessBeanFactory method!");
//对 teacher对象赋值/修改
BeanDefinition bd = beanFactory.getBeanDefinition("teacher");
bd.getPropertyValues().addPropertyValue("name", "李二");
bd.getPropertyValues().addPropertyValue("phone", "13256487442");
}
}
package cn.lvyjx.test.postProcessor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
public MyBeanPostProcessor(){
System.out.println("【BeanPostProcessor】:MyBeanPostProcessor implements BeanPostProcessor construct!");
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("【BeanPostProcessor-postProcessBeforeInitialization】:beanName="+beanName);
if(bean instanceof Teacher){
Teacher teacher = (Teacher)bean;
System.out.println("【BeanPostProcessor-postProcessBeforeInitialization】bean.toString before:"+teacher.toString());
teacher.setName("李三");
System.out.println("【BeanPostProcessor-postProcessBeforeInitialization】bean.toString after:"+teacher.toString());
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
if(bean instanceof Teacher){
Teacher teacher = (Teacher)bean;
System.out.println("【BeanPostProcessor-postProcessAfterInitialization】bean.toString :"+teacher.toString());
}
System.out.println("【BeanPostProcessor-postProcessAfterInitialization】:beanName="+beanName);
return bean;
}
}
3、配置bean文件:bean-postprocessor.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<bean class="cn.lvyjx.test.postProcessor.MyBeanFactoryPostProcessor"></bean>
<bean class="cn.lvyjx.test.postProcessor.MyBeanPostProcessor" />
<bean id = "teacher" class="cn.lvyjx.test.postProcessor.Teacher" lazy-init="false" init-method="init">
<property name="name" value="李一" />
</bean>
<bean id = "student" class="cn.lvyjx.test.postProcessor.Student" lazy-init="false" >
<property name="name" value="王一" />
</bean>
</beans>
4、测试类 MainDemo
package cn.lvyjx.test.postProcessor;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainDemo {
public static void main(String[] args) {
System.out.println("开始执行");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean-postprocessor.xml");
Teacher teacher = (Teacher)context.getBean("teacher");
System.out.println(teacher.toString());
System.out.println("结束执行");
}
}
5、运行结果
开始执行
【BeanFactoryPostProcessor】:MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor construct!
【BeanFactoryPostProcessor】:postProcessBeanFactory method!
【BeanPostProcessor】:MyBeanPostProcessor implements BeanPostProcessor construct!
Teacher construct!
teacher中name值的注入
【BeanPostProcessor-postProcessBeforeInitialization】:beanName=teacher
【BeanPostProcessor-postProcessBeforeInitialization】bean.toString before:name=李二;phone=13256487442
teacher中name值的注入
【BeanPostProcessor-postProcessBeforeInitialization】bean.toString after:name=李三;phone=13256487442
Teacher-init method
【BeanPostProcessor-postProcessAfterInitialization】bean.toString :name=李三;phone=13256487442
【BeanPostProcessor-postProcessAfterInitialization】:beanName=teacher
Student construct!
【BeanPostProcessor-postProcessBeforeInitialization】:beanName=student
【BeanPostProcessor-postProcessAfterInitialization】:beanName=student
name=李三;phone=13256487442
结束执行