SpringIOC容器中Bean的生命周期

太过爱你忘了你带给我的痛 2022-05-17 01:04 380阅读 0赞

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的初始化方法和销毁方法

  1. <bean id="person" class="com.atguigu.spring.lifecycle.Person"
  2. init-method="init"
  3. destroy-method="destroy">
  4. <property name="name" value="abcd"></property>
  5. </bean>

关闭容器
ApplicationContext接口中没有关闭容器的方法
所以,使用ApplicationContext接口作为IOC容器的引用,destroy-method将不会起到作用,需要使用ApplicationContext的子接口ConfigurableApplicationContext

发表评论

表情:
评论列表 (有 0 条评论,380人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Bean 生命周期

    Bean 的生命周期 我们知道 Web 容器中的 Servlet 拥有明确的生命周期,Spring 容器中的 Bean 也拥有相似的生命周期。Bean 生命周期由多个特定