隨筆-42  評論-349  文章-4  trackbacks-0
           

                  1、初始化回調(diào)接口InitializingBean   要對某個(gè)受管Bean進(jìn)行預(yù)處理里時(shí),可以實(shí)現(xiàn)Spring定義的初始化回調(diào)接口InitializingBean,它定義了一個(gè)方法如下:

          1 void afterPropertiesSet() throws Exception
                開發(fā)者可以在受管Bean中實(shí)現(xiàn)該接口,再在此方法中對受管Bean進(jìn)行預(yù)處理。

          注意:應(yīng)該盡量避免使用這種方法,因?yàn)檫@種方法會將代碼與Spring耦合起來。推薦使用下面的使用init-method方法。

          2、析構(gòu)回調(diào)接口DisposableBean    同上面一樣,要對受管Bean進(jìn)行后處理,該Bean可以實(shí)現(xiàn)析構(gòu)回調(diào)接口DisposableBean,它定義的方法如下:

          1 void destroy() throws Exception
           

          為了降低與Spring的耦合度,這種方法也不推薦。

          下面的例子(例程3.6)簡要的展示如何使用這兩個(gè)接口。

          新建Java工程,名字為IoC_Test3.6,為其添加Spring開發(fā)能力后,新建一ioc.test包,添加一個(gè)Animal類,該類實(shí)現(xiàn)InitializingBean接口和DisposableBean接口,再為其添加nameage成員,GeterSeter方法,speak方法。完成后代碼如下:

           1 package ioc.test;
           2 
           3 import org.springframework.beans.factory.DisposableBean;
           4 import org.springframework.beans.factory.InitializingBean;
           5 
           6 public class Animal implements InitializingBean, DisposableBean {
           7 
           8     private String name;
           9     private int age;
          10 
          11     public String speak(){
          12         return "我的名字:"+this.name+"我的年齡:"+this.age;
          13     }
          14 
          15     public void afterPropertiesSet() throws Exception {
          16         System.out.println("初始化接口afterPropertiesSet()方法正在運(yùn)行!");
          17     }
          18 
          19     public void destroy() throws Exception {
          20         System.out.println("析構(gòu)接口destroy()方法正在運(yùn)行!");
          21     }
          22 
          23 //Geter和Seter省略
          24 
          25 }
          26 

          在配置文件中配置受管Bean,代碼如下:

           1 <?xml version="1.0" encoding="UTF-8"?>
           2 <beans
           3     xmlns="http://www.springframework.org/schema/beans"
           4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           5 
           6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
           7     <bean id="animal" class="ioc.test.Animal">
           8 
           9       <property name="age"  value="5"></property>
          10       <property name="name" value="豬"></property>
          11 
          12     </bean>
          13 
          14 </beans>
          15 

          新建含有主方法的TestMain類,添加測試代碼,如下:
           1 package ioc.test;
           2 
           3 import org.springframework.context.support.AbstractApplicationContext;
           4 import org.springframework.context.support.ClassPathXmlApplicationContext;
           5 
           6 public class TestMain {
           7 
           8     public static void main(String[] args) {
           9 
          10         AbstractApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
          11         //注冊容器關(guān)閉鉤子,才能關(guān)掉容器,調(diào)用析構(gòu)方法
          12         ac.registerShutdownHook();
          13         Animal animal = (Animal)ac.getBean("animal");
          14         System.out.println(animal.speak());    
          15     
          16     }    
          17 }
          18 
           

          運(yùn)行工程,結(jié)果如下:

           

          以看到,Spring IoC容器自動的調(diào)用了兩個(gè)接口的相關(guān)方法對bean進(jìn)行了預(yù)處理和后處理。

          注意:由于后處理是在Bean被銷毀之前調(diào)用,所有要在MyEclipse中看到后處理方法的輸出,必須先注冊容器的關(guān)閉鉤子,在主方法退出時(shí)關(guān)掉容器,這樣其管理的Bean就會被銷毀。當(dāng)然也可以直接調(diào)用容器的close方法來顯示的關(guān)閉容器。


          By:殘夢追月
          posted on 2008-07-28 14:24 殘夢追月 閱讀(1476) 評論(0)  編輯  收藏 所屬分類: Spring
          主站蜘蛛池模板: 绍兴县| 兴安县| 蓬溪县| 南京市| 桐乡市| 汝城县| 子长县| 盐亭县| 射洪县| 屏山县| 南城县| 盐源县| 陆河县| 新巴尔虎右旗| 阳江市| 黄骅市| 花莲市| 上栗县| 芮城县| 徐水县| 济宁市| 方正县| 大竹县| 科尔| 凤城市| 修武县| 夏河县| 广平县| 浑源县| 乌兰浩特市| 达州市| 赤水市| 曲麻莱县| 锡林浩特市| 浦北县| 原阳县| 亳州市| 大田县| 汾阳市| 综艺| 镇原县|