一、概述
spring中有许多扩展点,开发人员可在此基础上扩展和增强,BeanPostProcessor、和BeanFactoryPostProcessor是两个比较重大的扩展点,可以对Bean进行增强(apo)、修改Bean的定义信息、或添加新的Bean定义,下面通过一个小的案例,观察下各自运行的时机。
二、案例实战
1、MyTestStudent类(实现InitializingBean接口):
public class MyTestStudent implements InitializingBean {
/**
* 学生编码
*/
private Integer stuCode;
/**
* 学生名称
*/
private String stuName;
public MyTestStudent() {
System.out.println("---MyTestStudent构造方法被调用");
}
public Integer getStuCode() {
System.out.println("---MyTestStudent#getStuCode方法被调用");
return stuCode;
}
public void setStuCode(Integer stuCode) {
System.out.println("---MyTestStudent#setStuCode方法被调用");
this.stuCode = stuCode;
}
public String getStuName() {
System.out.println("---MyTestStudent#getStuName方法被调用");
return stuName;
}
public void setStuName(String stuName) {
System.out.println("---MyTestStudent#setStuName方法被调用");
this.stuName = stuName;
}
@Override
public void afterPropertiesSet() {
System.out.println("---MyTestStudent#afterPropertiesSet方法被调用");
}
}
2、MyTestBeanPostProcessor类(实现BeanPostProcessor接口):
public class MyTestBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("---MyTestBeanPostProcessor#postProcessBeforeInitialization方法被执行,当前对象:" + beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("---MyTestBeanPostProcessor#postProcessAfterInitialization方法被执行,当前对象:" + beanName);
return bean;
}
}
3、
MyTestBeanFactoryPostProcessor类(实现BeanFactoryPostProcessor接口):
public class MyTestBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("---MyTestBeanFactoryPostProcessor#postProcessBeanFactory法被执行");
}
}
4、myLifeCycle.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: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="myTestStudent" class="com.druid.demo.springcycle.MyTestStudent">
<property name="stuCode" value="101"/>
<property name="stuName" value="张三"/>
</bean>
<bean id="myTestBeanPostProcessor" class="com.druid.demo.springcycle.MyTestBeanPostProcessor"/>
<bean id="myTestBeanFactoryPostProcessor" class="com.druid.demo.springcycle.MyTestBeanFactoryPostProcessor"/>
</beans>
5、MyMainTest测试类:
public class MyMainTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("myLifeCycle.xml");
}
}
6、运行结果输出:

1、解析xml配置文件,加载bean定义信息(BeanDefinition)
2、调用BeanFactoryPostProcessor#postProcessBeanFactory方法,可动态新增bean定义信息(BeanDefinition),
或者修改bean定义信息
3、调用MyTestStudent构造方法
4、为MyTestStudent类设置属性信息(调用两个set方法)
5、调用BeanPostProcessor#postProcessBeforeInitialization方法,进行bean初始化前增强
6、调用MyTestStudent#afterPropertiesSet方法,执行初始化逻辑
7、调用BeanPostProcessor#postProcessAfterInitialization方法,进行bean初始化后增强(aop一般在此实现)
三、总结
关于spring另外一个扩展点:
ImportBeanDefinitionRegistrar,可参见另外一篇文章:
spring扩展点之
ImportBeanDefinitionRegistrar
© 版权声明
文章版权归作者所有,未经允许请勿转载。如内容涉嫌侵权,请在本页底部进入<联系我们>进行举报投诉!
THE END















- 最新
- 最热
只看作者