bean的生命周期
- 如果有BeanFactoryPostProcessor实现类,调用postProcessBeanFactory方法
- bean的构造实例化
- 调用set方法设置bean的属性,填充属性
- 如果bean实现了BeanNameAware接口,调用BeanNameAware的setBeanName()方法,此处传递的是spring配置文件中bean的id值
- 如果bean实现了BeanFactoryAware接口,调用BeanFactoryAware的setBeanFactory()方法,传递的是spring工厂本身,可以用该方式来获取其他bean
- 如果bean实现了ApplicationContextAware接口,调用setApplicationContext()方法,与上述类似
- 如果bean关联了BeanPostProcessor接口,调用BeanPostProcessor的postProcessBeforeInitialization()方法,被用作bean内容的修改
- 如果bean中有@PostConstruct注解标注的方法,则调用该方法
- 如果bean实现InitializingBean接口,调用InitializingBean的afterPropertiesSet()方法,
- 调用自定义的初始化方法(init-method属性指定该方法)
- 如果bean关联了BeanPostProcessor接口,调用BeanPostProcessor类的postProcessAfterInitialization()方法
此时容器初始化完成
容器进行销毁时
- 如果bean中有@PreDestroy标注的方法,则调用该方法
- 如果bean实现了DisposableBean接口,调用destory()方法
- 调用自定义的销毁方法(destory-method属性指定该方法)
示例验证
java
public class Person implements BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean, ApplicationContextAware {
private String name;
private int age;
public Person(){
System.out.println("构造器");
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("set属性");
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("BeanFactoryAware#setBeanFactory");
}
@Override
public void setBeanName(String name) {
System.out.println("BeanNameAware#setBeanName");
}
@Override
public void destroy() throws Exception {
System.out.println("DisposableBean#destroy");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBean#afterPropertiesSet");
}
public void myInit() {
System.out.println("init-method");
}
public void myDestory() {
System.out.println("destory-method");
}
@PostConstruct
public void postConstruct() {
System.out.println("@PostConstruct注解");
}
@PreDestroy
public void myPreDestroy() {
System.out.println("@PreDestroy");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("ApplicationContextAware#setApplicationContext");
}
}
@Component
class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if("person".equals(beanName)){
System.out.println("BeanPostProcessor#postProcessBeforeInitialization");
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if("person".equals(beanName)){
System.out.println("BeanPostProcessor#postProcessAfterInitialization");
}
return bean;
}
}
@Component
class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("BeanFactoryPostProcessor#postProcessBeanFactory");
}
}
java
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-all-lifecycle.xml");
System.out.println("---容器初始化成功");
Person person = applicationContext.getBean("person", Person.class);
applicationContext.close();
}
}
---输出结果
BeanFactoryPostProcessor#postProcessBeanFactory
构造器
set属性
BeanNameAware#setBeanName
BeanFactoryAware#setBeanFactory
ApplicationContextAware#setApplicationContext
BeanPostProcessor#postProcessBeforeInitialization
@PostConstruct注解
InitializingBean#afterPropertiesSet
init-method
BeanPostProcessor#postProcessAfterInitialization
---容器初始化成功
@PreDestroy
DisposableBean#destroy
destory-method
spring bean factory负责管理在spring容器中被创建的bean的生命周期,有两组回调(初始化之后调用的回调和销毁之前调用的回调),框架内提供了四种方式来管理bean的生命周期事件
- InitializingBean和DisposableBean回调接口
- 针对特殊行为的xxxAware接口
- Bean的配置文件中的自定义init()和destory()
- @PostConstruct和@PostDestroy注解方式
bean的后置处理器
spring提供了两种后置处理器
- Bean后置处理器 对容器中Bean进行后处理,对Bean进行额外加强
- 容器后置处理器 对IOC容器进行处理,增强容器功能
Bean后置处理器
Bean后置处理器是一种特殊的Bean,这种特殊的Bean并不对外服务,主要负责对容器中的其他Bean执行后处理,例如容器中的目标Bean生成代理等。Bean后处理器会在Bean实例创建成功后,为Bean实例进行进一步的增强处理。实现BeanPostProcessor接口,实现postProcessAfterInitialization和postProcessBeforeInitialization方法。
java
public class MyProcessor implements BeanPostProcessor {
/**
* 初始化之前
* @param o
* @param s
* @return
* @throws BeansException
*/
@Override
public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
if(o instanceof Connection){
System.out.println("初始化之前");
}
return o;
}
/**
* 初始化之后
* @param o
* @param s
* @return
* @throws BeansException
*/
@Override
public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
if(o instanceof Connection){
System.out.println("初始化之后");
}
return o;
}
}
这里处理完之后一定要将bean返回回去,否则后续无法获取到bean
注:如果使用BeanFactory作为Spring容器,则必须手动注册Bean后置处理器,程序必须获取Bean后置处理器实例,然后手动注册。
java
BeanPostProcessor bp = (BeanPostProcessor)beanFactory.getBean("bp");
beanFactory.addBeanPostProcessor(bp);
Person p = (Person)beanFactory.getBean("person");
容器后置处理器
容器后置处理器负责容器本身,实现BeanFactoryPostProcessor接口,实现接口的postProcessBeanFactory方法对Spring容器进行处理,可以对Spring容器进行自定义扩展,
在BeanFactory标准初始化之后调用,即所有的BeanDefinition已经保存加载到beanFactory中,但是bean的实例还未创建
java
public interface BeanFactoryPostProcessor {
void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
}
本文由mdnice多平台发布