javascript
Spring中Bean的生命中期与InitializingBean和DisposableBean接口
Spring提供了一些標(biāo)志接口,用來(lái)改變BeanFactory中的bean的行為。它們包括InitializingBean和DisposableBean。實(shí)現(xiàn)這些接口將會(huì)導(dǎo)致BeanFactory調(diào)用前一個(gè)接口的afterPropertiesSet()方法,調(diào)用后一個(gè)接口destroy()方法,從而使得bean可以在初始化和析構(gòu)后做一些特定的動(dòng)作。
在內(nèi)部,Spring使用BeanPostProcessors?來(lái)處理它能找到的標(biāo)志接口以及調(diào)用適當(dāng)?shù)姆椒āH绻阈枰远x的特性或者其他的Spring沒(méi)有提供的生命周期行為,你可以實(shí)現(xiàn)自己的?BeanPostProcessor。關(guān)于這方面更多的內(nèi)容可以看這里:第?3.7?節(jié)“使用BeanPostprocessors定制bean”。
所有的生命周期的標(biāo)志接口都在下面敘述。在附錄的一節(jié)中,你可以找到相應(yīng)的圖,展示了Spring如何管理bean;那些生命周期的特性如何改變你的bean的本質(zhì)特征以及它們?nèi)绾伪还芾怼?/p>
1. InitializingBean / init-method
實(shí)現(xiàn)org.springframework.beans.factory.InitializingBean?接口允許一個(gè)bean在它的所有必須的屬性被BeanFactory設(shè)置后,來(lái)執(zhí)行初始化的工作。InitializingBean接口僅僅制定了一個(gè)方法:
??? * Invoked by a BeanFactory after it has set all bean properties supplied??? * (and satisfied BeanFactoryAware and ApplicationContextAware).??? * <p>This method allows the bean instance to perform initialization only??? * possible when all bean properties have been set and to throw an??? * exception in the event of misconfiguration.??? * @throws Exception in the event of misconfiguration (such??? * as failure to set an essential property) or if initialization fails.??? */??? void afterPropertiesSet() throws Exception;
注意:通常InitializingBean接口的使用是能夠避免的(而且不鼓勵(lì),因?yàn)闆](méi)有必要把代碼同Spring耦合起來(lái))。Bean的定義支持指定一個(gè)普通的初始化方法。在使用XmlBeanFactory的情況下,可以通過(guò)指定init-method屬性來(lái)完成。舉例來(lái)說(shuō),下面的定義:
<bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/>public class ExampleBean {??? public void init() {??????? // do some initialization work??? }}
同下面的完全一樣:
<bean id="exampleInitBean" class="examples.AnotherExampleBean"/>public class AnotherExampleBean implements InitializingBean {??? public void afterPropertiesSet() {??????? // do some initialization work??? }}
但卻不把代碼耦合于Spring。
2. DisposableBean / destroy-method
實(shí)現(xiàn)org.springframework.beans.factory.DisposableBean接口允許一個(gè)bean,可以在包含它的BeanFactory銷(xiāo)毀的時(shí)候得到一個(gè)回調(diào)。DisposableBean也只指定了一個(gè)方法:
??? /**??? * Invoked by a BeanFactory on destruction of a singleton.??? * @throws Exception in case of shutdown errors.??? * Exceptions will get logged but not rethrown to allow??? * other beans to release their resources too.??? */??? void destroy() throws Exception;
注意:通常DisposableBean接口的使用能夠避免的(而且是不鼓勵(lì)的,因?yàn)樗槐匾貙⒋a耦合于Spring)。?Bean的定義支持指定一個(gè)普通的析構(gòu)方法。在使用XmlBeanFactory使用的情況下,它是通過(guò)destroy-method屬性完成。舉例來(lái)說(shuō),下面的定義:
<bean id="exampleInitBean" class="examples.ExampleBean" destroy-method="destroy"/>public class ExampleBean {??? public void cleanup() {??????? // do some destruction work (like closing connection)??? }}
同下面的完全一樣:
<bean id="exampleInitBean" class="examples.AnotherExampleBean"/>public class AnotherExampleBean implements DisposableBean {??? public void destroy() {??????? // do some destruction work??? }}
但卻不把代碼耦合于Spring。
重要的提示:當(dāng)以portotype模式部署一個(gè)bean的時(shí)候,bean的生命周期將會(huì)有少許的變化。通過(guò)定義,Spring無(wú)法管理一個(gè)non-singleton/prototype bean的整個(gè)生命周期,因?yàn)楫?dāng)它創(chuàng)建之后,它被交給客戶(hù)端而且容器根本不再留意它了。當(dāng)說(shuō)起non-singleton/prototype bean的時(shí)候,你可以把Spring的角色想象成“new”操作符的替代品。從那之后的任何生命周期方面的事情都由客戶(hù)端來(lái)處理。BeanFactory中bean的生命周期將會(huì)在第3.4.1?節(jié)“生命周期接口”一節(jié)中有更詳細(xì)的敘述?.
轉(zhuǎn)載于:https://www.cnblogs.com/jiaozi-li/p/5696279.html
總結(jié)
以上是生活随笔為你收集整理的Spring中Bean的生命中期与InitializingBean和DisposableBean接口的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 做梦梦到家里闹鬼是怎么回事
- 下一篇: JavaScript:JavaScrip