(殘夢(mèng)追月原創(chuàng),轉(zhuǎn)載注明)
如果僅僅使用Spring的內(nèi)定事件,那顯然是遠(yuǎn)遠(yuǎn)不夠的,幸好,Spring為我們提供了中自定義發(fā)布事件的能力。下面通過例程來展示如何發(fā)布并監(jiān)聽自定義的事件。
在工程中,我們定義一個(gè)Animal類,為受管Bean,它具有一個(gè)Speak方法,我們要做的就是監(jiān)視該方法,當(dāng)用戶調(diào)用該方法時(shí)觸發(fā)AnimalSpeakEvent事件。具體操作如下:
新建名字為IoC_Test3.9的java工程,添加Spring開發(fā)能力后,建立ioc.test包。新建一個(gè)事件類AnimalSpeakEvent,它繼承自ApplicationEvent,重載其默認(rèn)的構(gòu)造方法。再添加一個(gè)String成員animalName和它的Geter方法。添加一個(gè)構(gòu)造方法,此方法有兩個(gè)參數(shù),一個(gè)為source,一個(gè)為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
創(chuàng)建好該事件類后,就應(yīng)該把它再合適的時(shí)候發(fā)布出去。既然它時(shí)一個(gè)“動(dòng)物講話事件”,那么就應(yīng)該再動(dòng)物“講話”的時(shí)候發(fā)布,如何發(fā)布呢?我們知道要發(fā)布一個(gè)事件,就必須要調(diào)用ApplicationContext的publishEvent方法。要在Animal類中獲得ApplicationContext的實(shí)例,就要實(shí)現(xiàn)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
到目前為之,我們已經(jīng)在Animal類中把事件發(fā)布出去了,現(xiàn)在要監(jiān)聽該事件的發(fā)生了,至于監(jiān)聽方法,前面已經(jīng)演示過,直接上代碼:
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("事件監(jiān)聽器" + this.getClass().getSimpleName()+":有一個(gè)動(dòng)物在講話!它的名字是:"+ 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
現(xiàn)在應(yīng)該來測(cè)試一下了,看看我們的自定義的事件是不是會(huì)再動(dòng)物“講話”的時(shí)候發(fā)生,測(cè)試類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 //從容器獲取動(dòng)物實(shí)例
14 Animal animal = (Animal)ac.getBean("Animal");
15
16 //讓動(dòng)物講話
17 System.out.println(animal.speak());
18 }
19 }
20
運(yùn)行該類中的主方法,輸出結(jié)果如下:

可以看到,再動(dòng)物“講話“之前,我們的事件監(jiān)聽器監(jiān)聽到“動(dòng)物講話”,并輸出了講話動(dòng)物的名字。
By:殘夢(mèng)追月
posted on 2008-07-26 10:18
殘夢(mèng)追月 閱讀(2961)
評(píng)論(2) 編輯 收藏 所屬分類:
Spring