zoukankan      html  css  js  c++  java
  • spring-ApplicationContext的事件传播(转)

    ApplicationContext中的事件处理是通过ApplicationEvent类和ApplicationListener接口来提供的,通过ApplicationContext的publishEvent()方法发布到ApplicationListener; 在这里包含三个角色:被发布的事件,事件的监听者和事件发布者。 

    事件发布者在发布事件的时候通知事件的监听者。 

    下面我们围绕这三个角色进行分析: 首先是被发布的事件:在Spring中,这个角色继承了ApplicationEvent类。 再看监听者,监听者实现了ApplicationListener接口。 而事件的发布者则实现了ApplicationContextAware接口,并调用publishEvent方法发布事件。

    在这里,事件将作为参数传递到这个方法中。

    ApplicationContext具有发布事件的能力。这是因为该接口继承了ApplicationEventPublisher接口。Spring中与事件有关的接口和类主要包括ApplicationEvent、ApplicationListener。定义一个事件的类需要继承ApplicationEvent或者ApplicationContextEvent抽象类,

    该抽象类中只有一个构造函数,并且带有一个Object类型的参数作为事件源,并且该事件源不能为null,因此我们需要在自己的构造函数中执行super(Object)。

     1 public class UserEvent extends ApplicationEvent
     2 
     3 {
     4 
     5   private String eventContent;
     6 
     7   public String getEventContent(){
     8 
     9      return eventContent;
    10 
    11   }
    12 
    13   public void setEventContent(String eventContent){
    14 
    15      this.eventContent = eventContent;
    16 
    17   }
    18 
    19   public UserEvent(Object source,String eventContent){
    20 
    21      super(source);
    22 
    23      this.eventContent = eventContent;
    24 
    25   }
    26 
    27 }

    针对一种事件,可能需要特定的监听器,因此,监听器需要实现ApplicationListener接口。当监听器接收到一个事件的时候,就会执行它的 onApplicationEvent()方法。由于SpringIoC中的事件模型是一种简单的、粗粒度的监听模型,当有一个事件到达时,所有的监听器都会接收到,

    并且作出响应,如果希望只针对某些类型进行监听,需要在代码中进行控制。

     1 public class UserListener implements ApplicationListener
     2 
     3 {
     4 
     5   public void onApplicationEvent(ApplicationEvent event){
     6 
     7      if(event instanceof UserEvent){ //只对UserEvent类型进行处理
     8 
     9         UserEvent ue = (UserEvent)event;
    10 
    11         String result = ue.getEventContent();
    12 
    13         System.out.println("Event Content:"+result);
    14 
    15      }
    16 
    17   }
    18 
    19 }

    对于发布事件,我们可以实现ApplicationContextAware或者ApplicationEventPublisherAware接口

     1 public class UserBiz implements ApplicationContextAware
     2 
     3 {
     4 
     5 private ApplicationContext applicationContext;
     6 
     7 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
     8 
     9 {
    10 
    11    this.applicationContext = applicationContext; 
    12 
    13 }
    14 
    15 public void service(String thing)
    16 
    17 {
    18 
    19    UserEvent event = new UserEvent(this,thing);
    20 
    21    event.setEventContent("I shoud "+thing);
    22 
    23     applicationContext.publishEvent(event);
    24 
    25 }  
    26 
    27 }

    至此便完成了事件的发布,当ApplicationContext接收到事件后,事件的广播是Spring内部给我们做的,不需要了解具体的细节。其实在Spring读取配置文件之后,利用反射,将所有实现ApplicationListener的Bean找出来,注册为容器的事件监听器。当接收到事件的时候,

    Spring会逐个调用事件监听器。剩下要做的就是在配置文件中配置监听器

  • 相关阅读:
    【POJ 3162】 Walking Race (树形DP-求树上最长路径问题,+单调队列)
    【POJ 2152】 Fire (树形DP)
    【POJ 1741】 Tree (树的点分治)
    【POJ 2486】 Apple Tree (树形DP)
    【HDU 3810】 Magina (01背包,优先队列优化,并查集)
    【SGU 390】Tickets (数位DP)
    【SPOJ 2319】 BIGSEQ
    【SPOJ 1182】 SORTBIT
    【HDU 5456】 Matches Puzzle Game (数位DP)
    【HDU 3652】 B-number (数位DP)
  • 原文地址:https://www.cnblogs.com/hangzhi/p/9811182.html
Copyright © 2011-2022 走看看