深入浅出Spring事件机制
转载
事件实现依赖ApplicationEvent抽象类和ApplicationListener接口,applicationContext发布(publishEvent)了事件以后,ApplicationListener的onApplicationEvent监听之:
Java代码如下:
01 | package com.uqee.spring.applicationContext; |
03 | import org.apache.commons.logging.Log; |
04 | import org.apache.commons.logging.LogFactory; |
05 | import org.springframework.context.ApplicationEvent; |
07 | public class EmailEvent extends ApplicationEvent |
09 | private String address; |
10 | private String text; |
11 | private String email; |
12 | public String getEmail() |
14 | return email; |
17 | public void setEmail(String email) |
19 | this .email = email; |
22 | private Log logger = LogFactory.getLog(EmailEvent. class ); |
24 | public EmailEvent(Object source) |
26 | super (source); |
27 | // TODO Auto-generated constructor stub |
30 | public EmailEvent(String email,String address,String text) |
32 | super (email); |
33 | this .email = email; |
34 | this .address = address; |
35 | this .text = text; |
38 | public void printInfo() |
40 | // System.out.println("Send this Email, address:"+address+" text:"); |
41 | logger.info( "Send this Email, address:" +address+ " text:" +text); |
47 | private static final long serialVersionUID = 1667085258090884727L; |
01 | package com.uqee.spring.applicationContext; |
03 | import org.springframework.context.ApplicationEvent; |
04 | import org.springframework.context.ApplicationListener; |
06 | public class EmailNotifier implements ApplicationListener |
09 | private String notificationAddress; |
12 | public String getNotificationAddress() |
14 | return notificationAddress; |
18 | public void setNotificationAddress(String notificationAddress) |
20 | this .notificationAddress = notificationAddress; |
25 | public void onApplicationEvent(ApplicationEvent event) |
27 | // TODO Auto-generated method stub |
28 | if (event instanceof EmailEvent) |
30 | //notifier apppriate person |
31 | EmailEvent emailEvent = (EmailEvent)event; |
32 | System.out.println( "我已收到通知:" +emailEvent.getEmail()+ "要发邮件了。。" ); |
01 | package com.uqee.spring.applicationContext; |
03 | import java.util.List; |
05 | import org.springframework.beans.BeansException; |
06 | import org.springframework.context.ApplicationContext; |
07 | import org.springframework.context.ApplicationContextAware; |
09 | public class EmailBean implements ApplicationContextAware |
12 | private ApplicationContext ctx = null ; |
13 | public ApplicationContext getCtx() |
15 | return ctx; |
18 | private List backList; |
20 | public List getBackList() |
22 | return backList; |
25 | public void setBackList(List backList) |
27 | this .backList = backList; |
31 | public void setApplicationContext(ApplicationContext applicationContext) |
32 | throws BeansException |
34 | // TODO Auto-generated method stub |
35 | this .ctx = applicationContext; |
38 | public void sendEmail(String email,String title,String text) |
40 | if (backList.contains(email)) |
42 | EmailEvent evt = new EmailEvent(email,title, text); |
43 | ctx.publishEvent(evt); |
xml配置如下:
01 | < bean id = "emailer" class = "com.uqee.spring.applicationContext.EmailBean" > |
02 | < property name = "backList" > |
04 | < value >ncg2.0@163.com</ value > |
05 | < value >niechanggang@gmail.com</ value > |
06 | < value >373045912@qq.com</ value > |
07 | </ list > |
08 | </ property > |
09 | </ bean > |
11 | < bean id = "emailNotifier" class = "com.uqee.spring.applicationContext.EmailNotifier" > |
12 | < property name = "notificationAddress" > |
13 | < value >qingwa@163.com</ value > |
14 | </ property > |
15 | </ bean > |
测试类如下:
view source
print?
1 | ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "applicationContext5.xml" ); |
2 | EmailBean emailer = (EmailBean)applicationContext.getBean( "emailer" ); |
3 | emailer.sendEmail( "373045912@qq.com" , "邮件头" , "邮件正文" ); |
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。