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

                  1、初始化回調接口InitializingBean   要對某個受管Bean進行預處理里時,可以實現Spring定義的初始化回調接口InitializingBean,它定義了一個方法如下:

          1 void afterPropertiesSet() throws Exception
                開發者可以在受管Bean中實現該接口,再在此方法中對受管Bean進行預處理。

          注意:應該盡量避免使用這種方法,因為這種方法會將代碼與Spring耦合起來。推薦使用下面的使用init-method方法。

          2、析構回調接口DisposableBean    同上面一樣,要對受管Bean進行后處理,該Bean可以實現析構回調接口DisposableBean,它定義的方法如下:

          1 void destroy() throws Exception
           

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

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

          新建Java工程,名字為IoC_Test3.6,為其添加Spring開發能力后,新建一ioc.test包,添加一個Animal類,該類實現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()方法正在運行!");
          17     }
          18 
          19     public void destroy() throws Exception {
          20         System.out.println("析構接口destroy()方法正在運行!");
          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         //注冊容器關閉鉤子,才能關掉容器,調用析構方法
          12         ac.registerShutdownHook();
          13         Animal animal = (Animal)ac.getBean("animal");
          14         System.out.println(animal.speak());    
          15     
          16     }    
          17 }
          18 
           

          運行工程,結果如下:

           

          以看到,Spring IoC容器自動的調用了兩個接口的相關方法對bean進行了預處理和后處理。

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


          By:殘夢追月
          posted on 2008-07-28 14:24 殘夢追月 閱讀(1474) 評論(0)  編輯  收藏 所屬分類: Spring
          主站蜘蛛池模板: 蒙自县| 尚义县| 昭觉县| 宁远县| 浮山县| 桂阳县| 泾川县| 乌兰县| 蓝田县| 西宁市| 来宾市| 平江县| 井研县| 绍兴县| 额济纳旗| 玉树县| 武陟县| 元氏县| 彭山县| 兴国县| 滦平县| 台江县| 辽中县| 封开县| 平定县| 泉州市| 牟定县| 共和县| 辽中县| 青田县| 芦山县| 江安县| 科尔| 兴和县| 突泉县| 车险| 巫山县| 香格里拉县| 交城县| 乌鲁木齐市| 梁河县|