SpringIOC容器中Bean的生命周期
SpringIOC容器
可以管理Bean的生命周期
Spring允许在Bean生命周期的特定点,执行定制的任务。
SpringIOC容器中,Bean的生命周期如下:
1、通过构造器或工厂方法创建Bean实例:调用构造器
2、为Bean的属性设置值和对其他Bean的引用:调用setter
3、将Bean实例传递给Bean后置处理器的
postProcessBeforeInitialization方法
4、调用Bean的初始化方法:init-method
5、将Bean实例传递给Bean后置处理器的
postProcessAfterInitialization方法
6、Bean可以使用了
7、当容器关闭时, 调用Bean的销毁方法:destroy-method
Bean的初始化和销毁方法
可以通过bean节点的init-method和destroy-method来配置Bean的初始化方法和销毁方法
<bean id="person" class="com.atguigu.spring.lifecycle.Person"
init-method="init"
destroy-method="destroy">
<property name="name" value="abcd"></property>
</bean>
关闭容器
ApplicationContext接口中没有关闭容器的方法
所以,使用ApplicationContext接口作为IOC容器的引用,destroy-method将不会起到作用,需要使用ApplicationContext的子接口ConfigurableApplicationContext
还没有评论,来说两句吧...