当前位置:首页 > 资讯 > info5 > 正文

Spring bean 生命周期管理

发表于: 2016-02-08   作者:doctor_who2004   来源:转载   浏览:
摘要:  Springbean生命周期管理  Forabeantogetintoausablestateafterinstantiation,itneedstoperformsomeinitialization.Likewise,somecleanupmaybenecessarywhenthebeanisnolongerrequiredandisremovedfromthecontainer. 当我们要准

 Spring bean 生命周期管理


 



 For a bean to get into a usable state after instantiation, it needs to
perform some initialization. Likewise, some clean up may be necessary when
the bean is no longer required and is removed from the container.
 当我们要准备使用一个bean时,它必须得执行一些初始化先。相反,当容器关闭的时候,它也可能想要一些清理工作,其实把这两种行为当作c++中的构造函数和析构函数好了。


 Spring provides us with callback methods for the life cycle of the bean. You can have a method in your bean that runs when the bean has been created, and you can also have a method in your bean that is run when the bean is about to be destroyed.
 spring通过回调方法来管理bean的生命周期。


 Spring's BeanFactory manages the life cycle of beans created through the Spring IoC container. The life cycle of beans consist of callback methods, which can be categorized broadly into the following two groups:


• Post-initialization callback methods
• Pre-destruction callback methods
主要分为两类回调方法:
Post-initialization 和Pre-destruction。
The following figure illustrates the two groups:

Spring bean 生命周期管理_第1张图片








Initialization

It represents a sequence of activities that take place between the bean instantiation and the handover of its reference to the client application:


• The bean container finds the definition of the Spring bean in the
configuration file and creates an instance of the bean
• If any properties are mentioned, populate the properties using setters
• If the Bean class implements the BeanNameAware interface, then call the
setBeanName() method
• If the Bean class implements the BeanFactoryAware interface, then call
the setBeanFactory() method
• If the Bean class implements the ApplicationContextAware interface,
then call the setApplicationContext() method
• If there are any BeanPostProcessors objects associated with the
BeanFactory interface that loaded the bean, then Spring will call the
postProcessBeforeInitialization() method before the properties
for the bean are injected
• If the Bean class implements the InitializingBean interface, then call the
afterPropertiesSet() method once all the bean properties defined in the
configuration file are injected
• If the bean definition in the configuration file contains the init-method
attribute, then call this method after resolving the value for the attribute
to a method name in the Bean class
• The postProcessAfterInitialization() method will be called if there
are any bean post processors attached to the BeanFactory interface that
loads the bean。


Activation

The bean has been initialized and the dependency has been injected. Now the bean is ready to be used by the application.


Destruction

This represents the following sequence of activities:
• If the Bean class implements the DisposableBean interface, then call the
destroy() method when the application no longer needs the bean reference
• If the bean definition in the configuration file contains the destroy-method
attribute, then call this method after resolving the value for the attribute to a method name in the Bean class.




There are two important bean life cycle callback methods that are required at the time of bean initialization and its destruction.
• Initialization callbacks
• Destruction callbacks


Initialization callbacks

There are three ways in which you can achieve the initialization work after all necessary properties on the bean are set by the container:
• Implementing the org.springframework.beans.factory.InitializingBean interface
• Using init-method in the XML configuration
• 利用PostConstruct注解


Destruction callbacks

There are two ways you can do a destruction callback:
• Implementing the org.springframework.beans.factory.DisposableBean
interface
• Using destroy-method in the XML configuration
• 利用PreDestroy注解

Spring bean 生命周期管理

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
Java对象的生命周期,从初始化到销毁如下: Object obj = new Object(); obj.setXXX(); obj.getXXX();
在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的
Spring中Bean的生命周期过程: 1.Spring对Bean进行实例化(相当于程序中的new Xx()) 2.Spring将值
概要: IOC容器中Bean的生命周期 Spring IOC容器可以管理Bean的生命周期,Spring允许在Bean生命周期
1.当调用者通过getBean(beanName)向容器请求某一个Bean时,如果容器注册了org.springframework.bean
Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Sing
Spring Bean 生命周期 任何一个事物都有自己的生命周期,生命的开始、生命中、生命结束。大家最熟悉
在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的
1:实例化: 容器寻找Bean的定义信息并将其实例化。 2:设置属性:使用依赖注入,Spring按照Bean定
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号