zoukankan      html  css  js  c++  java
  • 深入浅出Spring事件机制

    事件实现依赖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要发邮件了。。
  • 相关阅读:
    第七章 路由 68 路由-前端路由和后端路由的概念
    第六章 组件 67 使用ref获取DOM元素和组件引用
    第六章 组件 65-66 组件案例-发表评论功能的实现
    第六章 组件 63 组件传值-父组件向子组件传值和data与props的区别
    第六章 组件 62 组件-组件定义方式的复习
    第六章 组件 61 动画-小球动画flag标识符的作用分析
    第六章 组件 60 组件切换-应用切换动画和mode方式
    第六章 组件 59 组件切换-使用Vue提供的component元素实现组件切换
    vscode代码格式化 空格的配置
    vim配置C++开发环境 win10
  • 原文地址:https://www.cnblogs.com/chenying99/p/2625900.html
Copyright © 2011-2022 走看看