zoukankan      html  css  js  c++  java
  • spring里的applicationlisener

    事件实现依赖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>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>

     

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

     

    打印结果如下:
    我已收到通知:373045912@qq.com要发邮件了。。
  • 相关阅读:
    如何解决C#异常:必须先将当前线程设置为单线程单元(STA)模式,然后才能进行OLE调用,请确保你的Main函数已在其上标记了STAThreadAttribute
    go多态
    go泛型
    protoc工具使用
    grpc protobuf协议
    grpc根据proto文件自动生成go源码
    go安装grpc
    go protobuf
    go读取http.Request中body的内容
    go数据库操作
  • 原文地址:https://www.cnblogs.com/chenzhao/p/2749409.html
Copyright © 2011-2022 走看看