深入浅出Spring事件机制

事件实现依赖ApplicationEvent抽象类和ApplicationListener接口,applicationContext发布(publishEvent)了事件以后,ApplicationListener的onApplicationEvent监听之: 
Java代码如下 : 
01 package com.uqee.spring.applicationContext;
02  
03 import org.apache.commons.logging.Log;
04 import org.apache.commons.logging.LogFactory;
05 import org.springframework.context.ApplicationEvent;
06  
07 public class EmailEvent extends ApplicationEvent
08 {
09     private String address;
10     private String text;
11     private String email;
12     public String getEmail()
13     {
14         return email;
15     }
16  
17     public void setEmail(String email)
18     {
19         this.email = email;
20     }
21  
22     private Log logger = LogFactory.getLog(EmailEvent.class);
23  
24     public EmailEvent(Object source)
25     {
26         super(source);
27         // TODO Auto-generated constructor stub
28     }
29      
30     public EmailEvent(String email,String address,String text)
31     {
32         super(email);
33         this.email = email;
34         this.address = address;
35         this.text = text;
36     }
37      
38     public void printInfo()
39     {
40 //      System.out.println("Send this Email, address:"+address+" text:");
41         logger.info("Send this Email, address:"+address+" text:"+text);
42     }
43  
44     /**
45      *
46      */
47     private static final long serialVersionUID = 1667085258090884727L;
48  
49 }

01 package com.uqee.spring.applicationContext;
02  
03 import org.springframework.context.ApplicationEvent;
04 import org.springframework.context.ApplicationListener;
05  
06 public class EmailNotifier implements ApplicationListener
07 {
08  
09     private String notificationAddress;
10      
11      
12     public String getNotificationAddress()
13     {
14         return notificationAddress;
15     }
16  
17  
18     public void setNotificationAddress(String notificationAddress)
19     {
20         this.notificationAddress = notificationAddress;
21     }
22  
23  
24     @Override
25     public void onApplicationEvent(ApplicationEvent event)
26     {
27         // TODO Auto-generated method stub
28         if(event instanceof EmailEvent)
29         {
30             //notifier apppriate person
31             EmailEvent emailEvent = (EmailEvent)event;
32             System.out.println("我已收到通知:"+emailEvent.getEmail()+"要发邮件了。。");
33         }
34     }
35  
36 }

01 package com.uqee.spring.applicationContext;
02  
03 import java.util.List;
04  
05 import org.springframework.beans.BeansException;
06 import org.springframework.context.ApplicationContext;
07 import org.springframework.context.ApplicationContextAware;
08  
09 public class EmailBean implements ApplicationContextAware
10 {
11      
12     private ApplicationContext ctx = null;
13     public ApplicationContext getCtx()
14     {
15         return ctx;
16     }
17  
18     private List backList;
19  
20     public List getBackList()
21     {
22         return backList;
23     }
24  
25     public void setBackList(List backList)
26     {
27         this.backList = backList;
28     }
29  
30     @Override
31     public void setApplicationContext(ApplicationContext applicationContext)
32             throws BeansException
33     {
34         // TODO Auto-generated method stub
35         this.ctx = applicationContext;
36     }
37      
38     public void sendEmail(String email,String title,String text)
39     {
40         if(backList.contains(email))
41         {
42             EmailEvent evt = new EmailEvent(email,title, text);
43             ctx.publishEvent(evt);
44             return ;
45         }
46     }
47 }

xml配置如下 : 
01 <bean id="emailer" class="com.uqee.spring.applicationContext.EmailBean">
02     <property name="backList">
03           <list>
04              <value>[email protected]</value>
05              <value>[email protected]</value>
06              <value>[email protected]</value>
07           </list>
08     </property>
09 </bean>
10  
11 <bean id="emailNotifier" class="com.uqee.spring.applicationContext.EmailNotifier">
12      <property name="notificationAddress">
13          <value>[email protected]</value>
14      </property>
15 </bean>

测试类如下 : 
1 ApplicationContext applicationContext = newClassPathXmlApplicationContext("applicationContext5.xml");
2 EmailBean emailer = (EmailBean)applicationContext.getBean("emailer");
3 emailer.sendEmail("[email protected]""邮件头""邮件正文");

打印结果如下: 
我已收到通知:[email protected]要发邮件了。。

你可能感兴趣的:(spring)