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ù)】
          主站蜘蛛池模板: 阿勒泰市| 贞丰县| 衡阳县| 中阳县| 根河市| 剑阁县| 南召县| 察哈| 易门县| 屯昌县| 孝义市| 菏泽市| 开江县| 固镇县| 防城港市| 本溪市| 广宗县| 望江县| 江西省| 莲花县| 朝阳县| 宽城| 凤山县| 昌乐县| 衡山县| 洛浦县| 汕头市| 青岛市| 无极县| 融水| 神农架林区| 漠河县| 崇州市| 柳河县| 龙州县| 乡城县| 什邡市| 日土县| 开鲁县| 浮山县| 闵行区|