即興的靈感

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

          博客好友

          最新評論

          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 閱讀(5201) 評論(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怎么定義呢?  回復  更多評論   

          主站蜘蛛池模板: 巴南区| 卫辉市| 荆门市| 章丘市| 丰城市| 翼城县| 万盛区| 二手房| 东莞市| 巴楚县| 山东省| 平利县| 彰化市| 本溪市| 清苑县| 三门县| 铁岭市| 锦州市| 奇台县| 尼木县| 平塘县| 巫溪县| 泰宁县| 交口县| 留坝县| 灵川县| 濉溪县| 永平县| 凉城县| 钟山县| 通许县| 板桥市| 玉林市| 阜新市| 临湘市| 日土县| 张家川| 托克托县| 邓州市| 沈丘县| 甘泉县|