zoukankan      html  css  js  c++  java
  • spring中事件机制

    一、一些概念

     事件是可以被控件识别的操作,如按下确定按钮,选择某个单选按钮或者复选框。每一种控件有自己可以识别的事件,如窗体的加载、单击、双击等事件,编辑框(文本框)的文本改变事,等等。事件有系统事件和用户事件。系统事件由系统激发,如时间每隔24小时,银行储户的存款日期增加一天。用户事件由用户激发,如用户点击按钮,在文本框中显示特定的文本。事件驱动控件执行某项功能。

     

     触发事件的对象称为事件发送者;接收事件的对象称为事件接收者。

     事件模型一般用到了观察者模式,下面简单介绍下:

    观察着模式

     

    • SUBJECT
      • 目标知道它的观察者。可以有任意多个观察者观察同一个目标。
      • 提供注册和删除观察者对象的接口。
    • Observer(观察者)
      • 为那些在目标发生改变时需获得通知的对象定义一个更新接口。
    • ConcreteSubject(具体目标)
      • 将有关状态存入各ConcreteObserver对象。
      • 当它的状态发生改变时,向它的各个观察者发出通知。
    • ConcreteObserver(具体观察者)
      • 维护一个指向ConcreteSubject对象的引用。
      • 存储有关状态,这些状态应与目标的状态保持一致。
      • 实现Observer的更新接口以使自身状态与目标的状态保持一致。

    二、spring事件机制

    首先上图一张:

    spring_event

    • ApplicationListener 就是我们的 Observer,需要到容器中注册。他要关心他所关心的ApplicationEvent 。一般有如下代码:if (event instanceof BlackListEvent) {}
    • ApplicationEventMulticaster是我们的SUBJECT一个代理。他会管理我们的 ApplicationListener 。
    • ApplicationEvent 是事件,它就是媒介,充当介质的作用。

    在spring中,容器管理所有的 bean。是ApplicationEvent 驱动的,一个ApplicationEvent  publish了,观察这个事件的监听者就会送到通知。

    具体核心代码如下: ApplicationEventMulticaster 会遍历所有的 监听器,再启动一个线程调用监听器的onApplicationEvent方法。

    1. public void multicastEvent(final ApplicationEvent event) {  
    2.     for (Iterator it = getApplicationListeners().iterator(); it.hasNext();) {  
    3.         final ApplicationListener listener = (ApplicationListener) it.next();  
    4.         getTaskExecutor().execute(new Runnable() {  
    5.             public void run() {  
    6.                 listener.onApplicationEvent(event);  
    7.             }  
    8.         });  
    9.     }  
    10. }  
     

     

    三、实例代码

              场景:程序发现一个有问题的用户,再会把这个的用户记录下来,采取 事件模型就这样设计

     

    •  
      • 发现有问题的用户 
      • 把问题记录下来 
      • 媒介也就是事件

     

      1、先来一个事件

       
      1. package spring;  
      2. import org.springframework.context.ApplicationEvent;  
      3. public class BlackListEvent extends ApplicationEvent {  
      4.     private static final long serialVersionUID = 1000L;  
      5.     private String            address;  
      6.     public String getAddress() {  
      7.         return address;  
      8.     }  
      9.     public BlackListEvent(String address) {  
      10.         super(address);  
      11.         this.address = address;  
      12.     }  
      13. }  
       

      2、一个监听器

      1. package spring;  
      2. import org.springframework.context.ApplicationEvent;  
      3. import org.springframework.context.ApplicationListener;  
      4. public class BlackListNotifier implements ApplicationListener {  
      5.     public void onApplicationEvent(ApplicationEvent event) {  
      6.         System.out.println(event);  
      7.         if (event instanceof BlackListEvent) {  
      8.             System.out.println(((BlackListEvent) event).getAddress());  
      9.             //TODO 处理这个邮件地址  
      10.         }  
      11.     }  
      12. }  
       

      3、发现有问题的用户,并且publish

      1. package spring;  
      2. import java.util.List;  
      3. import org.springframework.beans.BeansException;  
      4. import org.springframework.context.ApplicationContext;  
      5. import org.springframework.context.ApplicationContextAware;  
      6. public class EmailBean implements ApplicationContextAware {  
      7.     private List<String>       blackList;  
      8.     private ApplicationContext ctx;  
      9.     public void setBlackList(List<String> blackList) {  
      10.         this.blackList = blackList;  
      11.     }  
      12.     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {  
      13.         this.ctx = applicationContext;  
      14.     }  
      15.     public void sendEmail(String address) {  
      16.         if (blackList.contains(address)) {  
      17.             BlackListEvent event = new BlackListEvent(address);  
      18.             ctx.publishEvent(event);  
      19.             return;  
      20.         }  
      21.     }  
      22. }  
       

      4、配置文件

      1. <?xml version="1.0" encoding="UTF-8"?>  
      2. <beans xmlns="http://www.springframework.org/schema/beans"  
      3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      4.     xsi:schemaLocation="http://www.springframework.org/schema/beans      
      5.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"  
      6.     default-autowire="byName">  
      7.       
      8.     <bean id="emailer" class="spring.EmailBean">  
      9.         <property name="blackList">  
      10.             <list>  
      11.                 <value>black@list.org</value>  
      12.                 <value>white@list.org</value>  
      13.                 <value>john@doe.org</value>  
      14.             </list>  
      15.         </property>  
      16.     </bean>  
      17.     <bean id="blackListListener" class="spring.BlackListNotifier" />  
      18. </beans>  
       

      5、调用方法

      1. package spring;  
      2. import org.springframework.context.ApplicationContext;  
      3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
      4. public class Test {  
      5.     public static void main(String[] args) {  
      6.         ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");  
      7.         EmailBean email = (EmailBean) ctx.getBean("emailer");  
      8.         email.sendEmail("black@list.org");  
      9.     }  
      10. }  
  • 相关阅读:
    DGA聚类 使用DBScan
    http://bdsmdvdtube.com/ 罪恶无处不在
    leetcode 202. Happy Number
    广州Uber优步司机奖励政策(1月4日~1月10日)
    武汉ber优步司机奖励政策(1月4日~1月10日)
    厦门Uber优步司机奖励政策(1月4日~1月10日)
    上海Uber优步司机奖励政策(1月4日~1月10日)
    青岛Uber优步司机奖励政策(1月4日~1月10日)
    深圳Uber优步司机奖励政策(1月4日~1月10日)
    成都Uber优步司机奖励政策(1月10日)
  • 原文地址:https://www.cnblogs.com/chenying99/p/2671191.html
Copyright © 2011-2022 走看看