spring---ApplicationListener,ApplicationEvent

(殘夢追月原創,轉載註明)

    如果僅僅使用Spring的內定事件,那顯然是遠遠不夠的,幸好,Spring爲我們提供了中自定義發佈事件的能力。下面通過例程來展示如何發佈並監聽自定義的事件。

在工程中,我們定義一個Animal類,爲受管Bean,它具有一個Speak方法,我們要做的就是監視該方法,當用戶調用該方法時觸發AnimalSpeakEvent事件。具體操作如下:

新建名字爲IoC_Test3.9的java工程,添加Spring開發能力後,建立ioc.test包。新建一個事件類AnimalSpeakEvent,它繼承自ApplicationEvent,重載其默認的構造方法。再添加一個String成員animalName和它的Geter方法。添加一個構造方法,此方法有兩個參數,一個爲source,一個爲animalName。代碼如下:

 1 package ioc.test;
 2 
 3 import org.springframework.context.ApplicationEvent;
 4 
 5 public class AnimalSpeakEvent extends ApplicationEvent {
 6 
 7     private static final long serialVersionUID = 1L;
 8 
 9     private String animalName;
10 
11     public AnimalSpeakEvent(Object source) {
12         super(source);
13     }
14 
15     public AnimalSpeakEvent(Object source,String animalName) {
16         super(source);
17         this.animalName = animalName;
18     }
19 
20     public String getAnimalName() {
21         return animalName;
22     }
23 
24 }
25 

創建好該事件類後,就應該把它再合適的時候發佈出去。既然它時一個“動物講話事件”,那麼就應該再動物“講話”的時候發佈,如何發佈呢?我們知道要發佈一個事件,就必須要調用ApplicationContextpublishEvent方法。要在Animal類中獲得ApplicationContext的實例,就要實現ApplicationContextAware接口,代碼如下:

 

 1 package ioc.test;
 2 
 3 //import省略
 4 
 5 public class Animal implements ApplicationContextAware {
 6 
 7     private ApplicationContext ac;
 8 
 9     private String name;
10 
11     private int age;
12 
13     public String speak(){
14 
15          ac.publishEvent(new AnimalSpeakEvent(this,this.name));
16         return " 我的名字是;"+this.name+",我的年齡是:"+this.age;
17    }
18 
19 
20    public void setApplicationContext(ApplicationContext arg0) throws BeansException {
21     this.ac = arg0;
22    }
23 
24 //Getet和Seter省略
25 
26 }
27 

到目前爲之,我們已經在Animal類中把事件發佈出去了,現在要監聽該事件的發生了,至於監聽方法,前面已經演示過,直接上代碼:

 

 

 1 ackage ioc.test;
 2 
 3 import org.springframework.context.ApplicationEvent;
 4 import org.springframework.context.ApplicationListener;
 5 
 6 public class AnimalEventListener implements ApplicationListener {
 7 
 8     public void onApplicationEvent(ApplicationEvent event) {
 9         if (event instanceof AnimalSpeakEvent) {
10             AnimalSpeakEvent a = (AnimalSpeakEvent) event;
11                 System.out.println("事件監聽器" + this.getClass().getSimpleName()+":有一個動物在講話!它的名字是:"+ a.getAnimalName());
12         }
13     }
14 }
15 

配置好Bean,爲AnimalBean注入值,配置文件如下:

 

 

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans  …………>
 3 
 4     <bean id="Listener" class="ioc.test.AnimalEventListener" />
 5 
 6     <bean id="Animal" class="ioc.test.Animal">
 7       <property name="name" value="老虎" />
 8       <property name="age" value="5" />
 9     </bean>
10 
11 </beans>
12 

現在應該來測試一下了,看看我們的自定義的事件是不是會再動物“講話”的時候發生,測試類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(
11                 "applicationContext.xml");
12 
13         //從容器獲取動物實例
14         Animal animal = (Animal)ac.getBean("Animal");
15 
16         //讓動物講話
17         System.out.println(animal.speak());                
18     }
19 }
20 

運行該類中的主方法,輸出結果如下: 


可以看到,再動物“講話“之前,我們的事件監聽器監聽到“動物講話”,並輸出了講話動物的名字。

 


ApplicationContextAware  

2008-11-23 20:26:01|  分類: 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的依賴,可以比較容易從現有的框架中脫離。








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章