Spring容器中Bean的生命周期

Spring初始化bean或销毁bean时,有时需要做一些处理工作,因此spring可以在创建和销毁bean 的时候调用bean的两个生命周期方法

<bean id="xxx" class="..." init-method="init" destory-method="destory"></bean>

当bean被载入到容器的时候调用init

当bean从容器中删除的时候调用destory(scope=singleton有效)

Spring容器中Bean的生命周期

1.instantiate bean对象实例化

2.populate properties 封装属性

3.如果Bean实现BeanNameAware执行setBeanName

4.如果Bean实现BeanFactoryAware或者ApplicationContextAware设置工厂setBeanFactory或者上下文对象setApplicationContext

5.如果存在类实现BeanPostProcessor(后处理Bean),执行postProcessBeforeInitialization

6.如果Bean实现InitializingBean执行afterPropertiesSet

7.调用<bean init-method="init">指定初始化方法init

8.如果存在类实现BeanPostProcessor(处理Bean),执行postProcessAfterInitialization

9.执行业务处理

10.如果Bean实现DisposableBean执行destroy

11.调用<bean destroy-method="customerDestory">指定销毁方法customerDestory

原文地址:https://www.cnblogs.com/yangHS/p/11375585.html