JAVA—咖啡館

          ——?dú)g迎訪問rogerfan的博客,常來《JAVA——咖啡館》坐坐,喝杯濃香的咖啡,彼此探討一下JAVA技術(shù),交流工作經(jīng)驗(yàn),分享JAVA帶來的快樂!本網(wǎng)站部分轉(zhuǎn)載文章,如果有版權(quán)問題請(qǐng)與我聯(lián)系。

          BlogJava 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
            447 Posts :: 145 Stories :: 368 Comments :: 0 Trackbacks

          公告

           

          Locations of visitors to this page
          點(diǎn)擊這里給我發(fā)消息 點(diǎn)擊這里給我發(fā)消息

          常用鏈接

          留言簿(17)

          隨筆分類(542)

          隨筆檔案(438)

          文章分類(182)

          文章檔案(142)

          新聞分類

          ※→ 【JAVA文檔】

          ※→ 【親人博客】

          ※→ 【休閑娛樂】

          ※→ 【友情鏈接】

          ※→ 【學(xué)習(xí)網(wǎng)站】

          ※→ 【服務(wù)網(wǎng)站】

          ※→ 【著名網(wǎng)站】

          ※→ 【阿里博客】

          最新隨筆

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          對(duì)于singleton bean,Spring容器知道bean何時(shí)實(shí)例化結(jié)束,何時(shí)銷毀,Spring可以管理實(shí)例化結(jié)束之后,和銷毀之前的行為,管理bean的生命周期行為主要未如下兩個(gè)時(shí)機(jī):

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

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

                   afterPropertiesSet和init同時(shí)出現(xiàn),前者先于后者執(zhí)行,使用init方法,需要對(duì)配置文件加入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.實(shí)現(xiàn)DisposableBean接口

          destroy和close同時(shí)出現(xiàn),前者先于后者執(zhí)行,使用close方法,需要對(duì)配置文件加入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創(chuàng)建之時(shí)和創(chuàng)建之后進(jìn)行監(jiān)控,則需要實(shí)現(xiàn)BeanPostProcessor接口

          其中有兩個(gè)方法:postProcessBeforeInitialization和postProcessAfterInitialization

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


          posted on 2007-09-11 20:47 rogerfan 閱讀(694) 評(píng)論(0)  編輯  收藏 所屬分類: 【Java知識(shí)】【開源技術(shù)】
          主站蜘蛛池模板: 武隆县| 于都县| 贞丰县| 吉隆县| 和田市| 汕尾市| 潞西市| 开阳县| 铁岭市| 万安县| 阿克陶县| 庄浪县| 界首市| 马龙县| 昭觉县| 奉化市| 安福县| 白银市| 若羌县| 白水县| 伊宁市| 小金县| 阿勒泰市| 永善县| 浦城县| 曲阳县| 清水县| 嘉祥县| 栾城县| 天全县| 宜黄县| 吴堡县| 济南市| 军事| 郎溪县| 青神县| 都安| 卢龙县| 铜山县| 高密市| 贺州市|