<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 431,  comments - 344,  trackbacks - 0

    Spring提供了一些標志接口,用來改變BeanFactory中的bean的行為。它們包括InitializingBeanDisposableBean。實現這些接口將會導致BeanFactory調用前一個接口的afterPropertiesSet()方法,調用后一個接口destroy()方法,從而使得bean可以在初始化和析構后做一些特定的動作。

    在內部,Spring使用BeanPostProcessors 來處理它能找到的標志接口以及調用適當的方法。如果你需要自定義的特性或者其他的Spring沒有提供的生命周期行為,你可以實現自己的 BeanPostProcessor。關于這方面更多的內容可以看這里: 3.7 “使用BeanPostprocessors定制bean”。

    所有的生命周期的標志接口都在下面敘述。在附錄的一節中,你可以找到相應的圖,展示了Spring如何管理bean;那些生命周期的特性如何改變你的bean的本質特征以及它們如何被管理。

    1. InitializingBean / init-method

    實現org.springframework.beans.factory.InitializingBean 接口允許一個bean在它的所有必須的屬性被BeanFactory設置后,來執行初始化的工作。InitializingBean接口僅僅制定了一個方法:

        * 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接口的使用是能夠避免的(而且不鼓勵,因為沒有必要把代碼同Spring耦合起來)。Bean的定義支持指定一個普通的初始化方法。在使用XmlBeanFactory的情況下,可以通過指定init-method屬性來完成。舉例來說,下面的定義:

    <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

    實現org.springframework.beans.factory.DisposableBean接口允許一個bean,可以在包含它的BeanFactory銷毀的時候得到一個回調。DisposableBean也只指定了一個方法:

        /**    * 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接口的使用能夠避免的(而且是不鼓勵的,因為它不必要地將代碼耦合于Spring)。 Bean的定義支持指定一個普通的析構方法。在使用XmlBeanFactory使用的情況下,它是通過 destroy-method屬性完成。舉例來說,下面的定義:

    <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。

    重要的提示:當以portotype模式部署一個bean的時候,bean的生命周期將會有少許的變化。通過定義,Spring無法管理一個non-singleton/prototype bean的整個生命周期,因為當它創建之后,它被交給客戶端而且容器根本不再留意它了。當說起non-singleton/prototype bean的時候,你可以把Spring的角色想象成“new”操作符的替代品。從那之后的任何生命周期方面的事情都由客戶端來處理。BeanFactorybean的生命周期將會在第 3.4.1 “生命周期接口”一節中有更詳細的敘述 .

    posted on 2009-03-14 22:22 周銳 閱讀(8461) 評論(0)  編輯  收藏 所屬分類: Spring
    主站蜘蛛池模板: 久久99精品免费视频| 午夜亚洲福利在线老司机| 91成人免费在线视频| 青青青国产免费一夜七次郎| 亚洲最大在线观看| 无码av免费毛片一区二区| 中文字幕在线亚洲精品| 国产亚洲精品免费视频播放| 亚洲精品二区国产综合野狼| 亚洲一卡一卡二新区无人区| 久久免费香蕉视频| 国产做床爱无遮挡免费视频| 亚洲性色高清完整版在线观看| 67194熟妇在线永久免费观看| 亚洲乱码一二三四区国产| 成av免费大片黄在线观看| 亚洲精品乱码久久久久66| 波多野结衣免费一区视频| 日韩亚洲国产综合久久久| 一个人看的www在线免费视频| 九九九精品成人免费视频| 日韩欧美亚洲中文乱码| 无码区日韩特区永久免费系列| 亚洲视频一区二区三区四区| 手机看片久久国产免费| 两个人看的www视频免费完整版| 久久久久久亚洲精品| 9久9久女女免费精品视频在线观看| 亚洲日本一区二区| 久久精品无码一区二区三区免费 | 成人无码区免费A∨直播| 亚洲国产精品无码中文字| 动漫黄网站免费永久在线观看| 亚洲乱人伦中文字幕无码| 国产一卡2卡3卡4卡2021免费观看| 亚洲精品无码高潮喷水A片软| 国产成人精品日本亚洲专区| 久久久久久精品免费免费自慰| 亚洲人成电影亚洲人成9999网| 成人免费无码大片A毛片抽搐| 久久免费香蕉视频|