您的位置:首页 > 编程语言 > Java开发

Spring的事件发布机制

2015-05-31 21:34 591 查看
一:Spring的事件发布

ApplicationContext提供了针对Bean的事件传播功能,其中的主角是publishEvent()方法,通过这个方法可以将事件通知给系统内的监听器(需实现ApplicationListener接口)。

ApplicationContext这个接口,是Spring的上下文,通常获取Bean就需要这个接口,这个接口并不是直接继承于BeanFactory,其中最著名的是直接继承了ApplicationPublisher接口,这个接口查看源码可以发现:只有一个方法,那就是主角voidpublishEvent(ApplicationEventevent);

Spring提供的基于Aware相关的接口有ApplicationContextAware,ResourceloaderAware,ServletContextAware(注意:Struts2也有这个接口,注意区分),最常用的就这三个,而Spring的事件发布机制需要用到ApplicationContextAware接口。

实现了ApplicationContextAware的Bean,在Bean初始化时将会被注入ApplicationContext实例(因为这个接口里有set(ApplictationContextctx)方法)

二:有了以上基础,看示例代码:

1.首先创建事件类TradeEvent

packagenet.wang.test; importorg.springframework.context.ApplicationEvent; /** *事件Event *@authorLiuRuoWang */ publicclassTradeEventextendsApplicationEvent{ publicTradeEvent(Objectsource){ super(source); System.out.println("事件:TradeEventevent!!"); } } 事件必须继承Spring提供的ApplicationEvent抽象类2.然后编写事件的发布者HelloWorld:packagenet.wang.test;

importorg.springframework.context.ApplicationEventPublisher;
importorg.springframework.context.ApplicationEventPublisherAware;

/**
*事件的发布者
*@authorLiuRuoWang
*/
publicclassHelloWorldimplementsApplicationEventPublisherAware{

privateStringword;

publicvoidsetWord(Stringword){
this.word=word;
}

privateApplicationEventPublishertradeEventPublisher;

publicvoidsetApplicationEventPublisher(ApplicationEventPublisherapplicationEventPublisher){
this.tradeEventPublisher=applicationEventPublisher;
}

publicvoidsay(){
System.out.println("say:"+this.word);
TradeEventtradeEvent=newTradeEvent(newString("HelloWorld!"));
this.tradeEventPublisher.publishEvent(tradeEvent);
}

}其中在say()方法里发布了事件3.最后编写事件的接收者EventReceiver:packagenet.wang.test;

importorg.springframework.context.ApplicationListener;

/**
*事件的接收者
*@authorLiuRuoWang
*/
publicclassEventReceiverimplementsApplicationListener<TradeEvent>{

publicvoidonApplicationEvent(TradeEventevent){
System.out.println("监听到的事件:"+event.getSource());
}
}事件的接收者其实是一个监听器,必须实现ApplicationListener,注意把事件TradeEvent直接写到泛型中4.applicationContext.xml:<?xmlversion="1.0"encoding="GBK"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<beanname="helloWrold"class="net.wang.test.HelloWorld">
<propertyname="word"value="Word!"/>
</bean>

<beanname="eventReceiver"class="net.wang.test.EventReceiver"/>

</beans>
注意把事件的接收者写入配置文件中

5.测试Test:

packagenet.wang.test;

importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;

publicclassTest{
publicstaticvoidmain(String[]args){
ApplicationContextctx=newClassPathXmlApplicationContext("applicationContext.xml");
HelloWorldh=(HelloWorld)ctx.getBean("helloWrold");
h.say();
}
}
6.结果显示:






结果中已经显示监听到的事件,说明成功。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: