君子博学而日参省乎己 则知明而行无过矣

博客园 首页 新随笔 联系 订阅 管理
事件实现依赖ApplicationEvent抽象类和ApplicationListener接口,applicationContext发布(publishEvent)了事件以后,ApplicationListener的onApplicationEvent监听之: 
Java代码如下: 
01package com.uqee.spring.applicationContext;
02 
03import org.apache.commons.logging.Log;
04import org.apache.commons.logging.LogFactory;
05import org.springframework.context.ApplicationEvent;
06 
07public 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}

01package com.uqee.spring.applicationContext;
02 
03import org.springframework.context.ApplicationEvent;
04import org.springframework.context.ApplicationListener;
05 
06public 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}

01package com.uqee.spring.applicationContext;
02 
03import java.util.List;
04 
05import org.springframework.beans.BeansException;
06import org.springframework.context.ApplicationContext;
07import org.springframework.context.ApplicationContextAware;
08 
09public 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>ncg2.0@163.com</value>
05             <value>niechanggang@gmail.com</value>
06             <value>373045912@qq.com</value>
07          </list>
08    </property>
09</bean>
10 
11<bean id="emailNotifier" class="com.uqee.spring.applicationContext.EmailNotifier">
12     <property name="notificationAddress">
13         <value>qingwa@163.com</value>
14     </property>
15</bean>

测试类如下: 
1ApplicationContext applicationContext = newClassPathXmlApplicationContext("applicationContext5.xml");
2EmailBean emailer = (EmailBean)applicationContext.getBean("emailer");
3emailer.sendEmail("373045912@qq.com""邮件头""邮件正文");

打印结果如下: 
我已收到通知:373045912@qq.com要发邮件了。。
posted on 2012-08-06 23:06  刺猬的温驯  阅读(348)  评论(0编辑  收藏  举报