即興的靈感

          思維是一種藝術; 藝術需要靈感。

          博客好友

          最新評論

          Spring筆記之四(Spring Event)

          Spring 中提供一些Aware相關的接口,BeanFactoryAware、 ApplicationContextAware、ResourceLoaderAware、ServletContextAware等等,其中最常用到的是ApplicationContextAware。實現ApplicationContextAware的Bean,在Bean被初始后,將會被注入 ApplicationContext的實例。ApplicationContextAware 提供了publishEvent()方法,實現Observer(觀察者)設計模式的事件傳播機 ,提供了針對Bean的事件傳播功能。通過Application.publishEvent方法,我們可以將事件通知系統內所有的ApplicationListener。

          Spring事件處理一般過程:

          定義Event類,繼承org.springframework.context.ApplicationEvent.

          編寫發布事件類Publisher,實現org.springframework.context.ApplicationContextAware接口.

          覆蓋方法 setApplicationContext ( ApplicationContext?applicationContext )和發布方法publish(Object obj)

          定義時間監聽類 EventListener,實現 ApplicationListener接口,實現方法 onApplicationEvent ( ApplicationEvent?event ).

          ?1?public?class?MessageEvent?extends ?ApplicationEvent?{
          ?2??????/**

          ?3? ?????*?
          ?4??????*/

          ?5?????private?static?final?long?serialVersionUID?=?1L ;
          ?6?????private
          ?String?msg;
          ?7?????????public
          ?MessageEvent(Object?source,?String?msg)?{
          ?8?????????????super
          (source);
          ?9?????????????this.msg?=
          ?msg;
          10?
          ????????}
          11?
          ????????
          12?????????public
          ?String?getMessage()?{
          13?????????????return
          ?msg;
          14?
          ????????}
          15?
          }




          ?1 ?public?class?Publisher?implements ?ApplicationContextAware?{
          ?2?

          ?3??????private ?ApplicationContext?ctx;
          ?4?

          ?5?????????public?void ?setApplicationContext(ApplicationContext?applicationContext)
          ?6?????????????????throws
          ?BeansException?{
          ?7?????????????this.ctx?=
          ?applicationContext;
          ?8?

          ?9? ????????}
          10?

          11?????????public?void ?publish(String?message)?{
          12?????????????ctx.publishEvent(new?MessageEvent(this
          ,?message));
          13?
          ????????}
          14?
          ????????
          15?????????public?static?void
          ?main(String[]?args)?{
          16?????????????ApplicationContext?ctx?=?new
          ?FileSystemXmlApplicationContext(
          17?????????????????????"src/applicationContext.xml"
          );
          18?

          19?????????????Publisher?pub?=?(Publisher)?ctx.getBean("publisher" );
          20?????????????pub.publish("Hello?World!"
          );
          21?????????????pub.publish("The?quick?brown?fox?jumped?over?the?lazy?dog"
          );
          22?
          ????????}
          23?

          24?}



          1?public?class?MessageEventListener?implements ?ApplicationListener?{
          2?

          3???????public?void ?onApplicationEvent(ApplicationEvent?event)?{
          4????????????if(event?instanceof
          ?MessageEvent)?{
          5????????????????MessageEvent?msgEvt?=
          ?(MessageEvent)event;
          6????????????????System.out.println("Received:?"?+
          ?msgEvt.getMessage());
          7?
          ???????????}
          8?
          ????????}
          9?}


          1?<beans>
          2?????<bean?id="publisher"?class="Publisher"/>
          3?????????<bean?id="messageEventListener"?class="MessageEventListener"/>
          4?</beans>


          在運行期,ApplicationContext會自動在當前的所有Bean中尋找ApplicationListener接口的實現,并將其作為事件接收對象。當Application.publishEvent方法調用時,所有的ApplicationListener接口實現都會被激發,每個ApplicationListener可根據事件的類型判斷是否是自己需要處理的事件,如上面的ActionListener只處理ActionEvent事件。



          ?
          鳳凰涅槃/浴火重生/馬不停蹄/只爭朝夕
          ???? 隱姓埋名/低調華麗/簡單生活/完美人生

          posted on 2007-09-24 22:38 poetguo 閱讀(5210) 評論(4)  編輯  收藏 所屬分類: Spring

          評論

          # re: Spring筆記之四(Spring Event) 2007-09-27 03:01 bayern

          這個例子的配置文件怎么寫啊????  回復  更多評論   

          # re: Spring筆記之四(Spring Event) 2007-09-27 09:03 improviser

          <beans>
          <bean id="publisher" class="Publisher"/>
          <bean id="messageEventListener" class="MessageEventListener"/>
          </beans>  回復  更多評論   

          # re: Spring筆記之四(Spring Event) 2007-09-27 11:33 bayern

          多謝  回復  更多評論   

          # re: Spring筆記之四(Spring Event)[未登錄] 2012-08-28 14:28 a

          請問有多個listerner怎么定義呢?  回復  更多評論   

          主站蜘蛛池模板: 临漳县| 凤冈县| 集贤县| 顺义区| 沙雅县| 广安市| 三门县| 凌云县| 喀喇沁旗| 盐山县| 长顺县| 长岭县| 化隆| 友谊县| 扶风县| 江孜县| 阿荣旗| 海林市| 德州市| 聂拉木县| 汉源县| 甘孜县| 遵义市| 临武县| 金秀| 和平县| 巍山| 泸溪县| 龙江县| 志丹县| 新野县| 时尚| 浪卡子县| 宿松县| 柳河县| 乌海市| 天峨县| 白河县| 左云县| 武汉市| 红原县|