spring学习历程 之 事件(二)
来源:互联网 发布:网络棋牌判刑 编辑:程序博客网 时间:2024/05/18 01:30
ApplicationContext 中的事件处理是通过ApplicationEvent类和ApplicationListener接口来提供的。如果在上下文中部署一个实现了ApplicationListener接口的bean,
那么每当一个ApplicationEvent发布到ApplicationContext时,这个bean就得到通知。实质上,这是标准的Observer设计模式。
只要在ApplicationContext调用publishEvent()方法可以很方便的实现自定义事件,将一个实现了ApplicationEvent的自定义事件类作为参数就可以了。事件的监听器同步
的接收事件。这意味着publishEvent()方法将被阻塞。直到所有的监听器都处理完事件(可以通过一个ApplicationEventMulticaster的实现提供可选的事件发送策略)。此外,
如果事务context可用,监听器会接收一含有发送者事务context的事件。
下面一个例子:
事件的定义:
public class BlackListEvent extends ApplicationEvent{private String address; private String text;public BlackListEvent(){super(new Object());}public BlackListEvent(String address,String text){super(address);this.address = address;this.text = text;}public String getAddress(){return address;}public String getText(){return text;}}实际的类:
public class EmailBean implements ApplicationContextAware { private List blackList;private ApplicationContext ctx; public void setBlackList(List blackList) { this.blackList = blackList; } public void setApplicationContext(ApplicationContext ctx) { this.ctx = ctx; } public void sendEmail(String address, String text) { if (blackList.contains(address)) { BlackListEvent event = new BlackListEvent(address, text); ctx.publishEvent(event);
<span style="color:#ff0000;">System.out.println("is fail");</span> return; } <span style="color:#ff0000;"> System.out.println("is seccess");</span> }}监听器的定义:
public class BlackListNotifier implements ApplicationListener { private String notificationAddress; public void setNotificationAddress(String notificationAddress) { this.notificationAddress = notificationAddress; } public void onApplicationEvent(ApplicationEvent event) { if (event instanceof BlackListEvent) { <span style="color:#ff0000;">System.out.println("this event is BlackListEvent");.</span> } }}bean的配置:
<pre name="code" class="html"><beans><bean id="emailer" class="cn.cong.spring.bean.test.SpringEmailBean"><property name="blackList"> <list> <value>black@list.org</value> <value>white@list.org</value> <value>john@doe.org</value> </list></property> </bean><bean id="blackListListener" class="cn.cong.spring.bean.test.BlackListNotifier"> <property name="notificationAddress" value="spam@list.org" /></bean> </beans>
测试类:
public class SpringMain {public static void main(String[] arge){ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring.xml"});SpringEmailBean seb = (SpringEmailBean)context.getBean("emailer");seb.sendEmail("black@list.org",null);}}
结果:
this event is BlackListEventis fail
publishEvent()方法加入事件之后,它会去查找所有的监听器的,现在再一个监听器:
public class WhileListListener implements ApplicationListener{public void onApplicationEvent(ApplicationEvent event){if(event instanceof BlackListEvent){System.out.println("this is whileListListener of blackListEvent");}}}
在bean配置
<beans><bean id="emailer" class="cn.cong.spring.bean.test.SpringEmailBean"><property name="blackList"> <list> <value>black@list.org</value> <value>white@list.org</value> <value>john@doe.org</value> </list></property> </bean><bean id="blackListListener" class="cn.cong.spring.bean.test.BlackListNotifier"> <property name="notificationAddress" value="spam@list.org" /></bean> <span style="color:#ff0000;"><bean id="whileListListener" class="cn.cong.spring.bean.test.WhileListListener"/></span></beans>运行测试类结果为:
this event is BlackListEventthis is whileListListener of blackListEventis fail
0 0
- spring学习历程 之 事件(二)
- RxJava学习历程(二)
- openwrt学习历程(二)
- Gradle学习历程(二)
- Spring学习历程 --- AOP基础之代理
- Java 学习历程二之正则表达式
- spring学习之FactoryBean学习(二)
- spring学习之二
- spring学习之二
- spring的学习历程
- asp.net ajax学习历程(二)
- java语言学习历程小结(二)
- Android 学习历程摘要(二)
- Android 学习历程摘要(二)
- MySQL学习历程(二):基本操作
- egret(白鹭引擎) 学习历程(二)
- 记录java的学习历程(二)
- 《编程珠玑》——学习历程之三(三个问题之二)
- hdu2795解题报告
- 非常详细的网页乱码分析和解决办法
- 离职原因怎么写?万万不可写这些
- Git配置和使用(SSH配置)
- SQL Server: Get table primary key and Foreign Key using sql query
- spring学习历程 之 事件(二)
- 编译原理学习笔记之词法分析
- 免费实用的jpg转换成pdf工具
- NoReverseMatch at /polls/2/vote/ 'polls' is not a registered namespace
- 程序员经典面试题之——数组指针与指针数组
- [Android]Native Crash at vdc
- HTTP请求的基本过程
- DelayQueue
- 递归低效以及java.lang.StackOverflowError原因