生活随笔
收集整理的這篇文章主要介紹了
Spring的ApplicationEvent的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Spring的ApplicationEvent的使用
??? Spring 3.0中提供了很多類似*Aware的類,其中ApplicationContextAware接口可以實現我們在初始化bean的時候給bean注入ApplicationConxt(Spring上下文對象)對象。ApplicationContextAware接口提供了publishEvent方法,實現了Observe(觀察者)設計模式的傳播機制,實現了對bean的傳播。通過ApplicationContextAware我們可以把系統中所有ApplicationEvent傳播給系統中所有的ApplicationListener。因此,我們只需要構造好我們自己的ApplicationEvent和ApplicationListener,就可以在系統中實現相應的監聽器。
?? 下面以增加學生的示例來演示如何構造Spring的監聽器,StudentAddEvent是監聽的事件對象,StudentAddListener是事件的監聽器(負責處理接收到的監聽事件),StudentAddBean負責觸發StudentAddEvent事件。具體步驟如下:
1.? 定義StudentAddEvent監聽事件
新建StudentAddEvent類,實現抽象類
org.springframework.context.ApplicationEvent
StudentAddEvent類中需要實現自己的構造函數,具體代碼如下:
[java] ?view plaincopy
package ?com.trs.spring.event;?? ?? import ?org.springframework.context.ApplicationEvent;?? ?? ? ? ?? public ? class ?StudentAddEvent? extends ?ApplicationEvent?{?? ?? ????? ? ?? ????private ? static ? final ? long ?serialVersionUID?=?20L;?? ?? ????? ? ?? ????private ?String?m_sStudentName;?? ?? ????? ? ?? ????public ?StudentAddEvent(Object?source,?String?_sStudentName)?{?? ????????super (source);?? ????????this .m_sStudentName?=?_sStudentName;?? ????}?? ?? ????? ? ? ? ?? ????public ?String?getStudentName()?{?? ????????return ?m_sStudentName;?? ????}?? ?? }??
?
2.? 定義StudentAddListener監聽器
新建StudentAddListener類,實現接口
org.springframework.context.ApplicationListener
中的onApplicationEvent 方法,在該方法中只處理StudentAddEvent類型的ApplicationEvent事件,代碼如下:
[java] ?view plaincopy
package ?com.trs.spring.event;?? ?? import ?org.springframework.context.ApplicationEvent;?? import ?org.springframework.context.ApplicationListener;?? ?? public ? class ?StudentAddListener? implements ?ApplicationListener?{?? ?? ????? ? ? ? ? ? ?? ????public ? void ?onApplicationEvent(ApplicationEvent?_event)?{?? ?????????? ????????if ?(!(_event? instanceof ?StudentAddEvent))?{?? ????????????return ;?? ????????}?? ?? ?????????? ????????StudentAddEvent?studentAddEvent?=?(StudentAddEvent)?_event;?? ????????System.out.println("增加了學生:::" ?+?studentAddEvent.getStudentName());?? ????}?? ?? }??
?
3.? 定義StudentAddBean觸發StudentAddEvent事件
新建StudentAddBean類,實現接口
org.springframework.context.ApplicationContextAware
中的setApplicationContext 方法,在構造bean的時候注入Spring的上下文對象,以便通過Spring上下文對象的publishEvent方法來觸發StudentAddEvent事件,具體代碼如下:
[java] ?view plaincopy
package ?com.trs.spring.event;?? ?? import ?org.springframework.beans.BeansException;?? import ?org.springframework.context.ApplicationContext;?? import ?org.springframework.context.ApplicationContextAware;?? import ?org.springframework.context.support.ClassPathXmlApplicationContext;?? ?? public ? class ?StudentAddBean? implements ?ApplicationContextAware?{?? ????? ? ?? ????private ?ApplicationContext?m_applicationContext?=? null ;?? ?? ????? ? ? ? ? ? ?? ????public ? void ?setApplicationContext(ApplicationContext?_applicationContext)?? ????????????throws ?BeansException?{?? ????????this .m_applicationContext?=?_applicationContext;?? ?? ????}?? ?? ????? ? ? ? ?? ????public ? void ?addStudent(String?_sStudentName)?{?? ?????????? ????????StudentAddEvent?aStudentEvent?=?new ?StudentAddEvent(?? ????????????????m_applicationContext,?_sStudentName);?? ?????????? ????????m_applicationContext.publishEvent(aStudentEvent);?? ????}?? ?? ????? ? ?? ????public ? static ? void ?main(String[]?args)?{?? ????????String[]?xmlConfig?=?new ?String[]?{? "applicationContext.xml" ?};?? ?????????? ????????ApplicationContext?context?=?new ?ClassPathXmlApplicationContext(?? ????????????????xmlConfig);?? ????????StudentAddBean?studentBean?=?(StudentAddBean)?context?? ????????????????.getBean("StudentAddBean" );?? ????????studentBean.addStudent("我是第一個學生" );?? ????????studentBean.addStudent("第二個學生已經添加" );?? ?? ????}?? ?? }??
?
4.? applicationContext.xml配置文件
<bean id="StudentAddBean" class="com.trs.spring.event.StudentAddBean"></bean>
<bean id="StudentAddListener" class="com.trs.spring.event.StudentAddListener"></bean>
?
5.? 說明
ApplicationContext在運行期會自動檢測到所有實現了ApplicationListener的bean對象,并將其作為事件接收對象。當ApplicationContext的publishEvent方法被觸發時,每個實現了ApplicationListener接口的bean都會收到ApplicationEvent對象,每個ApplicationListener可根據事件類型只接收處理自己感興趣的事件,比如上面的StudentAddListener只接收StudentAddEvent事件。
6.? 執行StudentAddBean的main函數,結果如下:
增加了學生:::我是第一個學生
增加了學生:::第二個學生已經添加
?
7.? 測試工程下載地址:
下載地址: http://download.csdn.net/detail/wgw335363240/4022181
來源:?< http://blog.csdn.net/wgw335363240/article/details/7202320#>
總結
以上是生活随笔 為你收集整理的Spring的ApplicationEvent的使用 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。