即興的靈感

          思維是一種藝術(shù); 藝術(shù)需要靈感。

          博客好友

          最新評(píng)論

          Spring筆記之四(Spring Event)

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

          Spring事件處理一般過程:

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

          編寫發(fā)布事件類Publisher,實(shí)現(xiàn)org.springframework.context.ApplicationContextAware接口.

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

          定義時(shí)間監(jiān)聽類 EventListener,實(shí)現(xiàn) ApplicationListener接口,實(shí)現(xiàn)方法 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>


          在運(yùn)行期,ApplicationContext會(huì)自動(dòng)在當(dāng)前的所有Bean中尋找ApplicationListener接口的實(shí)現(xiàn),并將其作為事件接收對(duì)象。當(dāng)Application.publishEvent方法調(diào)用時(shí),所有的ApplicationListener接口實(shí)現(xiàn)都會(huì)被激發(fā),每個(gè)ApplicationListener可根據(jù)事件的類型判斷是否是自己需要處理的事件,如上面的ActionListener只處理ActionEvent事件。



          ?
          鳳凰涅槃/浴火重生/馬不停蹄/只爭(zhēng)朝夕
          ???? 隱姓埋名/低調(diào)華麗/簡(jiǎn)單生活/完美人生

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

          評(píng)論

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

          這個(gè)例子的配置文件怎么寫啊????  回復(fù)  更多評(píng)論   

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

          <beans>
          <bean id="publisher" class="Publisher"/>
          <bean id="messageEventListener" class="MessageEventListener"/>
          </beans>  回復(fù)  更多評(píng)論   

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

          多謝  回復(fù)  更多評(píng)論   

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

          請(qǐng)問有多個(gè)listerner怎么定義呢?  回復(fù)  更多評(píng)論   

          主站蜘蛛池模板: 砀山县| 六安市| 浦北县| 江津市| 卢湾区| 酒泉市| 济源市| 宾川县| 舞阳县| 连云港市| 日喀则市| 东阿县| 马关县| 三河市| 美姑县| 扬中市| 东乡族自治县| 曲麻莱县| 克山县| 兰西县| 都昌县| 威海市| 东明县| 滕州市| 上犹县| 望江县| 巩义市| 正定县| 麟游县| 平远县| 靖安县| 安新县| 英山县| 平潭县| 安吉县| 大新县| 北川| 潜山县| 罗平县| 西乌珠穆沁旗| 栾城县|