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

          spring中一些aware接口

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

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

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

           ApplicationContextAware接口的定義如下:

          ApplicationContextAware.java

          public interface ApplicationContextAware {

              void setApplicationContext(ApplicationContext context);

          }


           我們這邊示范如何透過實現ApplicationContextAware注入ApplicationContext來實現事件傳播,首先我們的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執行publishEvent()后,會自動尋找實現ApplicationListener接口的對象并通知其發生對應事件,我們實現了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());

              }

          }


           執行結果會如下所示:

          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!


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

          ResourceLoaderAware.java

          public interface ResourceLoaderAware {

              void setResourceLoader(ResourceLoader loader);

          }


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

           基本上,Spring雖然提供了這些Aware相關接口,然而Bean上若實現了這些界面,就算是與Spring發生了依賴,從另一個角度來看,雖然您可以直接在Bean上實現這些接口,但您也可以透過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;

              }

          }


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

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

                

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

          hello.setApplicationContext(context);

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


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

           


          評論

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

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

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


          網站導航:
           
          主站蜘蛛池模板: 涿州市| 博客| 射洪县| 密山市| 桃园县| 德钦县| 津市市| 栾川县| 台中县| 元江| 怀仁县| 即墨市| 浠水县| 江门市| 康乐县| 长岭县| 新密市| 琼海市| 开平市| 桐柏县| 调兵山市| 瓦房店市| 岫岩| 弥勒县| 西平县| 万载县| 布尔津县| 九寨沟县| 肥东县| 平谷区| 蕲春县| 尖扎县| 闽清县| 平南县| 永济市| 仙居县| 吉安市| 清远市| 甘孜县| 定安县| 郧西县|