BeanFactoryPostProcessor
在spring容器加载了bean的定义文件之后,在bean实例化之前执行的。接口方法入参是ConfigurrableListableBeanFactory,使用该参数,可以获取到相关bean的定义信息。
spring中,有内置的一些BeanFactoryPostProcessor实现类,常用的有:
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
org.springframework.beans.factory.config.PropertyOverrideConfigurer
org.springframework.beans.factory.config.CustomEditorConfigurer:用来注册自定义的属性编辑器
BeanPostProcessor
是在spring容器加载了bean的定义文件并且实例化bean之后执行的。BeanPostProcessor的执行顺序是在BeanFactoryPostProcessor之后。
spring中,有内置的一些BeanPostProcessor实现类,例如:
org.springframework.context.annotation.CommonAnnotationBeanPostProcessor:支持@Resource注解的注入
org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor:支持@Required注解的注入
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor:支持@Autowired注解的注入
org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor:支持@PersistenceUnit和@PersistenceContext注解的注入
org.springframework.context.support.ApplicationContextAwareProcessor:用来为bean注入ApplicationContext等容器对象
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.0.RELEASE</version> </dependency>
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName"><bean id="myTestBean" class="processpost.MyTestBean" init-method="initMethod">
<property name="name" value="原始名字" />
<property name="remark" value="原始备注" />
</bean><bean id="myBeanPostProcessor" class="processpost.MyBeanPostProcessor" />
<bean id="myBeanFactoryPostProcessor" class="processpost.MyBeanFactoryPostProcessor" />
</beans>
案例代码
package processpost;
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 MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("=============调用MyBeanFactoryPostProcessor的postProcessBeanFactory============");
BeanDefinition bd = beanFactory.getBeanDefinition("myTestBean");
System.out.println("属性值:"+bd.getPropertyValues().toString());
MutablePropertyValues pv = bd.getPropertyValues();
if(pv.contains("remark")){
pv.addPropertyValue("remark","修改后备注");
}
bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
}
}
package processpost;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("BeanPostProcessor,对象" + beanName + "调用初始化方法之前的数据: " + bean.toString());
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("BeanPostProcessor,对象" + beanName + "调用初始化方法之后的数据:" + bean.toString());
return bean;
}
}
package processpost;
import org.springframework.beans.factory.InitializingBean;
public class MyTestBean implements InitializingBean {
private String name;
private String remark;
public MyTestBean(){
System.out.println("======================MyTestBean构造函数执行了============================");
}
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("调用setName方法");
this.name = name;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
System.out.println("调用了setRemark方法");
this.remark = remark;
}
@Override
public String toString() {
return "MyTestBean{" +
"name='" + name + '\'' +
", remark='" + remark + '\'' +
'}';
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("调用afterPropertiesSet方法");
this.remark = "在初始化方法中修改之后的描述信息";
}
public void initMethod() {
System.out.println("调用initMethod方法");
}
}
package processpost;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PostProcessTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("postprocess.xml");
MyTestBean myTestBean = (MyTestBean) applicationContext.getBean("myTestBean");
System.out.println(myTestBean.toString());
}
}
执行结果:
从上面的结果可以看出,BeanFactoryPostProcessor在bean实例化之前执行,之后实例化bean(调用构造函数,并调用set方法注入属性值),然后在调用两个初始化方法前后,执行了BeanPostProcessor。初始化方法的执行顺序是,先执行afterPropertiesSet,再执行init-method。也符合Bean的生命周期里的几大步骤:实例化->属性赋值->初始化->销毁