posts - 12, comments - 8, trackbacks - 0, articles - 5
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          spring中一些aware接口

          Posted on 2010-11-24 11:14 楊羅羅 閱讀(7668) 評論(1)  編輯  收藏 所屬分類: java.spring

          Spring中提供一些Aware相關接口,像是BeanFactoryAware、 ApplicationContextAware、ResourceLoaderAware、ServletContextAware等等,實現(xiàn)這些 Aware接口的Bean在被初始之后,可以取得一些相對應的資源,例如實現(xiàn)BeanFactoryAware的Bean在初始后,Spring容器將會注入BeanFactory的實例,而實現(xiàn)ApplicationContextAware的Bean,在Bean被初始后,將會被注入 ApplicationContext的實例等等。

           Bean取得BeanFactory、ApplicationContextAware的實例目的是什么,一般的目的就是要取得一些檔案資源的存取、相 關訊息資源或是那些被注入的實例所提供的機制,例如ApplicationContextAware提供了publishEvent()方法,可以支持基于Observer模式的事件傳播機制。

           ApplicationContextAware接口的定義如下:

          ApplicationContextAware.java

          public interface ApplicationContextAware {

              void setApplicationContext(ApplicationContext context);

          }


           我們這邊示范如何透過實現(xiàn)ApplicationContextAware注入ApplicationContext來實現(xiàn)事件傳播,首先我們的HelloBean如下:

          HelloBean.java

          package onlyfun.caterpillar;

           

          import org.springframework.context.*;

           

          public class HelloBean implements ApplicationContextAware {

              private ApplicationContext applicationContext;

              private String helloWord = "Hello!World!";

            

              public void setApplicationContext(ApplicationContext context) {

                  this.applicationContext = context;

              }

            

              public void setHelloWord(String helloWord) {

                  this.helloWord = helloWord;

              }

            

              public String getHelloWord() {

                  applicationContext.publishEvent(

                         new PropertyGettedEvent("[" + helloWord + "] is getted"));

                  return helloWord;

              }

          }


           ApplicationContext會由Spring容器注入,publishEvent()方法需要一個繼承ApplicationEvent的對象,我們的PropertyGettedEvent繼承了ApplicationEvent,如下:

          PropertyGettedEvent.java

          package onlyfun.caterpillar;

           

          import org.springframework.context.*;

           

          public class PropertyGettedEvent extends ApplicationEvent {

              public PropertyGettedEvent(Object source) {

                  super(source);

              }

          }


           當ApplicationContext執(zhí)行publishEvent()后,會自動尋找實現(xiàn)ApplicationListener接口的對象并通知其發(fā)生對應事件,我們實現(xiàn)了PropertyGettedListener如下:

          PrppertyGettedListener.java

          package onlyfun.caterpillar;

           

          import org.springframework.context.*;

           

          public class PropertyGettedListener implements ApplicationListener {

              public void onApplicationEvent(ApplicationEvent event) {

                  System.out.println(event.getSource().toString());  

              }

          }


           Listener必須被實例化,這我們可以在Bean定義檔中加以定義:

          <?xml version="1.0" encoding="UTF-8"?>

          <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd">

          <beans>

              <bean id="propertyGetterListener" class="onlyfun.caterpillar.PropertyGettedListener"/>

           

              <bean id="helloBean" class="onlyfun.caterpillar.HelloBean">

                  <property name="helloWord"><value>Hello!Justin!</value></property>

              </bean>

          </beans>


           我們寫一個測試程序來測測事件傳播的運行:

          Test.java

          package onlyfun.caterpillar;

           

          import org.springframework.context.*;

          import org.springframework.context.support.*;

           

          public class Test {

              public static void main(String[] args) {

                  ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");

                

                  HelloBean hello = (HelloBean) context.getBean("helloBean");

                  System.out.println(hello.getHelloWord());

              }

          }


           執(zhí)行結果會如下所示:

          log4j:WARN No appenders could be found for logger

          (org.springframework.beans.factory.xml.XmlBeanDefinitionReader).

          log4j:WARN Please initialize the log4j system properly.

          org.springframework.context.support.ClassPathXmlApplicationContext:

          displayName=[org.springframework.context.support.ClassPathXmlApplicationContext;

          hashCode=33219526]; startup date=[Fri Oct 29 10:56:35 CST 2004];

          root of ApplicationContext hierarchy

          [Hello!Justin!] is getted

          Hello!Justin!


           以上是以實現(xiàn)事件傳播來看看實現(xiàn)Aware接口取得對應對象后,可以進行的動作,同樣的,您也可以實現(xiàn)ResourceLoaderAware接口:

          ResourceLoaderAware.java

          public interface ResourceLoaderAware {

              void setResourceLoader(ResourceLoader loader);

          }


           實現(xiàn)ResourceLoader的Bean就可以取得ResourceLoader的實例,如此就可以使用它的getResource()方法,這對于必須存取檔案資源的Bean相當有用。

           基本上,Spring雖然提供了這些Aware相關接口,然而Bean上若實現(xiàn)了這些界面,就算是與Spring發(fā)生了依賴,從另一個角度來看,雖然您可以直接在Bean上實現(xiàn)這些接口,但您也可以透過setter來完成依賴注入,例如:

          HelloBean.java

          package onlyfun.caterpillar;

           

          import org.springframework.context.*;

           

          public class HelloBean {

              private ApplicationContext applicationContext;

              private String helloWord = "Hello!World!";

            

              public void setApplicationContext(ApplicationContext context) {

                  this.applicationContext = context;

              }

            

              public void setHelloWord(String helloWord) {

                  this.helloWord = helloWord;

              }

            

              public String getHelloWord() {

                  applicationContext.publishEvent(new PropertyGettedEvent("[" + helloWord + "] is getted"));

                  return helloWord;

              }

          }


           注意這次我們并沒有實現(xiàn)ApplicationContextAware,我們在程序中可以自行注入ApplicationContext實例:

          ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");

                

          HelloBean hello = (HelloBean) context.getBean("helloBean");

          hello.setApplicationContext(context);

          System.out.println(hello.getHelloWord());


           就Bean而言,降低了對Spring的依賴,可以比較容易從現(xiàn)有的框架中脫離。

           


          評論

          # re: spring中一些aware接口[未登錄]  回復  更多評論   

          2013-01-07 14:02 by winter
          好文章,學習了。

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 富蕴县| 沧源| 吴旗县| 凯里市| 大姚县| 吉安市| 合川市| 陆川县| 长武县| 南木林县| 呼和浩特市| 弥勒县| 绥滨县| 驻马店市| 昔阳县| 洪泽县| 奈曼旗| 棋牌| 阿坝县| 洪湖市| 香河县| 合水县| 清镇市| 赣榆县| 思南县| 嘉定区| 临漳县| 丁青县| 建德市| 和平县| 深州市| 邹平县| 清涧县| 东丽区| 蓬莱市| 潜江市| 嘉祥县| 垫江县| 波密县| 定结县| 定边县|