Spring的后处理器是Spring对外开发的重要扩展点,允许我们介入到Bean的整个实例化流程中来,以达到动态注册BeanDefinition,动态修改BeanDefinition,以及动态修改Bean的作用。Spring主要有两种后处理器:
Bean工厂后处理器——BeanFactoryPostProcessor
BeanFactoryPostProcessor是一个接口规范,实现了该接口的类只要交由Spring容器管理的话,那么Spring就会回调该接口的方法,用于对BeanDefinition注册和修改的功能
注册&修改
假如现在有User和Student两个Bean,且Student已经注入容器
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("MyBeanFactoryPostProcessor的BeanFactoryPostPostProcessor");
/*修改*/
BeanDefinition student = beanFactory.getBeanDefinition("student");
student.setBeanClassName("com.dong.bean.User");
/*注册*/
RootBeanDefinition definition = new RootBeanDefinition();
definition.setBeanClassName("com.dong.bean.Student");
DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) beanFactory;
defaultListableBeanFactory.registerBeanDefinition("student2",definition);
}
}
配置文件
<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="student" class="com.dong.bean.Student">bean>
<bean id="beanFactoryPostProcessor" class="com.dong.processor.MyBeanFactoryPostProcessor">bean>
beans>
Bean工厂后处理器——BeanDefinitionRegistryPostProcessor
Spring 提供了一个BeanFactoryPostProcessor的子接口BeanDefinitionRegistryPostProcessor专门用于注册BeanDefinition操作
public class MyBeanFactoryPostProcessor2 implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
BeanDefinition beanDefinition = new RootBeanDefinition();
beanDefinition.setBeanClassName("com.dong.bean.Student"); beanDefinitionRegistry.registerBeanDefinition("student3",beanDefinition);
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
}
}
配置文件注入Bean后工厂处理器
<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="student" class="com.dong.bean.Student">bean>
<bean id="beanFactoryPostProcessor2" class="com.dong.processor.MyBeanFactoryPostProcessor2">bean>
beans>
Bean被实例化后,到最终缓存到名为singletonObjects单例池之前,中间会经过Bean的初始化过程,例如:属性的填充、初始方法init的执行等,其中有一个对外进行扩展的点BeanPostProcessor,我们称为Bean后处理。跟上面的 Bean工厂后处理器相似,它也是一个接口,实现了该接口并被容器管理的BeanPostProcessor,会在流程节点上被Spring自动调用。
实现:
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("BeanPostProcessor的before方法...");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("BeanPostProcessor的after方法...");
return bean;
}
}
配置文件配置Bean后处理
<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="student" class="com.dong.bean.Student">bean>
<bean id="beanPostProcessor" class="com.dong.processor.MyBeanPostProcessor">bean>
beans>
public class Test01 {
public static void main(String[] args) {
ApplicationContext appliactionContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) appliactionContext.getBean("student");
System.out.println(student);
}
}
输出结果:
student的无参构造
BeanPostProcessor的before方法…
BeanPostProcessor的after方法…
com.dong.bean.Student@5cb9f472
Spring Bean的生命周期是从 Bean 实例化之后,即通过反射创建出对象之后,到Bean成为一个完整对象,最终存储到单例池中,这个过程被称为Spring Bean的生命周期。Spring Bean的生命周期大体上分为三个阶段
由于Bean的初始化阶段的步骤比较复杂,所以着重研究Bean的初始化阶段
Spring在进行属性注入时,会分为如下几种情况:
注入双向对象引用属性时就会出现循环依赖
循环依赖:多个实体之间相互依赖并形成闭环的情况就叫做"循环依赖",也叫做"循环引用"
循环依赖问题spring已经给出了解决方法:三级缓存
Spring提供了三级缓存存储 完整Bean实例 和 半成品Bean实例 ,用于解决循环引用问题
在DefaultListableBeanFactory的上四级父类DefaultSingletonBeanRegistry中提供如下三个Map
假如,UserService注入了一个UserDao,UserDao又注入了一个UserService,实例化过程如下:
Aware接口是一种框架辅助属性注入的一种思想,其他框架中也可以看到类似的接口。框架具备高度封装性,我们接触到的一般都是业务代码,一个底层功能API不能轻易的获取到,但是这不意味着永远用不到这些对象,如果用到了 ,就可以使用框架提供的类似Aware的接口,让框架给我们注入该对象
总结:处理器的作用,为Bean生命周期各个阶段提供扩展
导入坐标:spring context
创建实体类:Student,实现接口:InitializingBean,BeanFactoryAware,BeanNameAware,ApplicationContextAware
public class Student implements InitializingBean,BeanFactoryAware,BeanNameAware,ApplicationContextAware{
private String sname;
public Student() {
System.out.println("bean的无参构造方法");
}
public void setSname(String sname) {
System.out.println("set方法赋值");
this.sname = sname;
}
public void doinit(){
System.out.println("方法初始化");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("接口的初始化方法");
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("BeanFactoryAware接口");
}
@Override
public void setBeanName(String s) {
System.out.println("BeanNameAware接口");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("ApplicationContextAware接口");
}
}
创建bean后处理类
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("Bean的后处理的postProcessBeforeInitialization方法");
return null;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("Bean的后处理的postProcessAfterInitialization方法");
return null;
}
}
spring主配置文件
<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="student" class="com.dong.bean.Student" init-method="doinit">
<property name="sname" value="张三">property>
bean>
<bean id="beanPostProcessor" class="com.dong.provessor.MyBeanPostProcessor">bean>
beans>
测试:getBean注入的Student
public class Test01 {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) applicationContext.getBean("student");
System.out.println(student);
}
}
输入结果:
bean的无参构造方法
set方法赋值
BeanNameAware接口
BeanFactoryAware接口
ApplicationContextAware接口
Bean的后处理的postProcessBeforeInitialization方法
接口的初始化方法
方法初始化
Bean的后处理的postProcessAfterInitialization方法
com.dong.bean.Student@6536e911