JAVA—咖啡館

          ——歡迎訪問rogerfan的博客,常來《JAVA——咖啡館》坐坐,喝杯濃香的咖啡,彼此探討一下JAVA技術,交流工作經驗,分享JAVA帶來的快樂!本網站部分轉載文章,如果有版權問題請與我聯系。

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            447 Posts :: 145 Stories :: 368 Comments :: 0 Trackbacks

          對于singleton bean,Spring容器知道bean何時實例化結束,何時銷毀,Spring可以管理實例化結束之后,和銷毀之前的行為,管理bean的生命周期行為主要未如下兩個時機:

          Bean全部依賴注入之后
          Bean即將銷毀之前

          (1)依賴關系注入后的行為實現:
                    有兩種方法:A.編寫init方法  B.實現InitializingBean接口

                   afterPropertiesSet和init同時出現,前者先于后者執行,使用init方法,需要對配置文件加入init-method屬性

          public void init(){
                  System.out.println(
          "in init"); 
              }


           
          public void afterPropertiesSet() throws Exception {
               System.out.println(
          "in afterPropertiesSet");  
                  
              }

           

          <bean id="chinese" class="Bean.lifecycle.Chinese" init-method="init" destroy-method="close">
              
          <property name="axe">
                
          <ref local="axe"/>
              
          </property>
            
          </bean>

           

           (2)bean銷毀之前的行為

             有兩種方法:A.編寫close方法  B.實現DisposableBean接口

          destroy和close同時出現,前者先于后者執行,使用close方法,需要對配置文件加入destroy-method屬性

           

              public void close(){
                  System.out.println(
          "in close"); 
              }

              
          public void destroy() throws Exception {
                  System.out.println(
          "in destroy"); 
                  
              }

          <bean id="chinese" class="Bean.lifecycle.Chinese" init-method="init" destroy-method="close">
              
          <property name="axe">
                
          <ref local="axe"/>
              
          </property>
            
          </bean>

           

          如果需要在bean創建之時和創建之后進行監控,則需要實現BeanPostProcessor接口

          其中有兩個方法:postProcessBeforeInitialization和postProcessAfterInitialization

          這兩個方法和init方法的順序是:postProcessBeforeInitialization-->init-->postProcessAfterInitialization


          posted on 2007-09-11 20:47 rogerfan 閱讀(694) 評論(0)  編輯  收藏 所屬分類: 【Java知識】【開源技術】
          主站蜘蛛池模板: 石楼县| 合山市| 中西区| 沛县| 黎城县| 台北市| 新乡县| 渭源县| 定结县| 乐清市| 文昌市| 左贡县| 三都| 阿尔山市| 通山县| 沈阳市| 永丰县| 绥中县| 沁水县| 通城县| 潞城市| 苗栗市| 云浮市| 淄博市| 南华县| 万盛区| 闵行区| 中江县| 高青县| 昆明市| 抚远县| 宜宾县| 台北县| 阿荣旗| 临夏市| 巴青县| 德钦县| 澳门| 鲁甸县| 松溪县| 册亨县|