上两节,我们仔细地描述了BeanPostProcessor这个接口的作用和存在的意义,并举了2个实例说明了它的作用,这一节我们接假模假样的庖丁解牛一发,这次我们一起看下BeanFactoryPostProcessor这个接口,乍一看,这个接口与BeanPostProcessor就相差了一个Factory,那么这个接口的作用是什么呢,老规矩,我们还是打开源码,也许能看出一点蛛丝马迹:
红色区域标注出:允许自定义去修改应用上下文中的beanDefinnition,来适配底层上下文bean的属性值
这句话的意思其实很好理解,当spring初始化好benaDefinnitionMap之后,提供了一个借口允许我们开发者自定义的去修改beanDefinition中的内容,这也是符合“spring”的开闭原则
我们来对比一下,BeanFactoryPostProcessor与BeanPostProcessor的区别,BeanPostProcessor官方注释是:
工厂钩子允许自定义修改新的bean的实例,区别就是BeanFactoryPostProcessor修改的是BeanFactory中的BeanDefinnition,BeanPostProcessor修改的是当我们初始化Bean的时候,“临时”修改bean的属性
一个是从根本上去修改,一个是临时修改,举例来说:BeanDefinnition是一个名片,当你发现这个名片有问题的时候,你会告诉做这个名片的factory,帮我重新做,这就是BeanFactoryPostProcessor的功能,而BeanPostProcessor只是用笔临时修改了一下属性而已
我们再来看看BeanFactoryPostProcessor里面定义的方法,不看之前,我们可以思考,BeanPostProcessor自定义修改的是Bean,所以我们BeanPostProcessor入参有Bean,那么,我们BeanFactoryPostProcessor修改的BeanDifinition而我们BeanDefinnition在BeanFactory,所以要么传入BeanDefinnitionMap要么传入BeanFactory:
果然传入的就是beanFactory
照例,我们依旧给出代码,代码背景依旧是修改“老师抽烟”这个问题
Teacher.java
<span style="color:#000000;">package org.study.spring.beanfactorypostprocessor;
public class Teacher {
/**
* 老师的姓名
*/
private String name;
/**
* 年龄
*/
private int age;
/**
* 是否抽烟
*/
private boolean smoking;
/**
* 老师教授的课程
*/
private String language;
public Teacher() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSmoking() {
return smoking;
}
public void setSmoking(boolean smoking) {
this.smoking = smoking;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public void teach(){
System.out.println("I am :"+name+" and I will teach you :"+language + " and I "+(smoking?"will":"will not")+" smoking");
}
}</span>
ChangeTeacherBeanFactoryPostProcessor.java
package org.study.spring.beanfactorypostprocessor;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
public class ChangeTeacherBeanFactoryPostProcessor implements BeanFactoryPostProcessor{
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
BeanDefinition beanDefinition = beanFactory.getBeanDefinition("teacher");
MutablePropertyValues mutablePropertyValues = beanDefinition.getPropertyValues();
if(mutablePropertyValues.contains("smoking")){
mutablePropertyValues.add("smoking", false);
}
}
}
bean-post-processor-teacher.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" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<bean id="teacher" class="org.study.spring.beanpostprocessor.demo.Teacher">
<property name="name" value="孔浩"/>
<property name="age" value="32"/>
<property name="smoking" value="true"/>
<property name="language" value="java"/>
</bean>
<bean id="changeTeacher" class="org.study.spring.beanpostprocessor.demo.ChangeTeacherSmokingBeanPostProcessor"/>
</beans>
ChangeTeacherBeanFactoryPostProcessorTest.java
package org.study.spring.beanfactorypostprocessor;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ChangeTeacherBeanFactoryPostProcessorTest{
@Test
public void test2() throws Exception{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beanfactory-post-processor-teacher.xml");
Teacher teacher = applicationContext.getBean("teacher",Teacher.class);
teacher.teach();
}
}
运行结果:
貌似这节结束了,其实还没有,我们再看看看BeanFactoryPostProcessor这个在Spring中的用到的地方吧
还记得我们上节利用BeanPostProcessor去切换生成环境jdbc.properties的代码吧,我们这边就简单的分析一下Spring官方给出的代码吧
打开你身边的项目,随便找一个spring与mybatis或者hibernate整合的xml配置文件,例如spring-mybatis.xml
中间有一段:
我们看看spring是如何利用org.springframework.beans.factory.config.PropertyPlaceholderConfigurer去替换${}等等之类的值的,我们打开继承图
我们发现PropertyPlaceholderConfigurer实现了BeanFactoryPostProcessor接口,我们在PropertyResourceConfigurer.java中找到BeanFactoryPostProcessor接口中postProcessBeanFactory方法的具体实现
,我这边就不具体与大家分享了,有兴趣的大家可以自己去debug一下~
就是这个方法,大家自己有兴趣看下吧~
下一节:与大家一起理解BeanFactory与FactoryBean的这2个面试常常问到的问题~
END~