Spring事件通知机制详解

对同一种事件有多种处理方式

不干扰主线(main line)

起源

要讲spring的事件通知机制,就要先了解一下spring中的这些接口和抽象类:

ApplicationEventPublisherAware        接口:用来 publish event

ApplicationEvent                  抽象类,记录了source和初始化时间戳:用来定义Event

ApplicationListener<E extends ApplicationEvent>  :用来监听事件

构建自己的事件机制案例

测试案例

测试入口

package com.meituan.spring.testcase.listener;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.concurrent.TimeUnit;

/**
 * Created by zhangxiaoguang on 16/1/27 下午11:40.
 * -----------------------------
 * Desc:
 */
public class TestPortal {
  public static void main(String[] args) throws InterruptedException {

final ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");

String[] definitionNames = applicationContext.getBeanDefinitionNames();
      System.out.println("==============bean====start=================");
      for (String definitionName : definitionNames) {
        System.out.println("bean----:" + definitionName);
      }
      System.out.println("==============bean====end=================");
      System.out.println();
      final CustomizePublisher customizePublisher = applicationContext.getBean(CustomizePublisher.class);


      Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
              System.out.println("开始吃饭:");

MealEvent lunchEvent = new MealEvent("A吃午饭了", MealEnum.lunch);
              MealEvent breakfastEvent = new MealEvent("B吃早饭了", MealEnum.breakfast);
              MealEvent dinnerEvent = new MealEvent("C吃晚饭了", MealEnum.dinner);
              customizePublisher.publish(lunchEvent);
              TimeUnit.SECONDS.sleep(1l);
              customizePublisher.publish(breakfastEvent);
              TimeUnit.SECONDS.sleep(1l);
              customizePublisher.publish(dinnerEvent);
              TimeUnit.SECONDS.sleep(1l);

System.out.println("他们吃完了!");
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
        }
      });
      thread.setName("meal-thread");
      thread.start();

System.out.println(Thread.currentThread().getName() + " is waiting for ....");
      thread.join();
      System.out.println("Done!!!!!!!!!!!!");
  }
}

TestPortal

测试结果

测试成员

MealListener :MealEvent                  演员

TroubleListener :TroubleEvent         演员

AllAcceptedListener                            演员

MealEnum                                          道具

TestPortal                                           入口

CustomizePublisher                           导演

成员代码

接受全部事件的演员(很负责任啊)

package com.meituan.spring.testcase.listener;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * Created by zhangxiaoguang on 16/1/27 下午11:27.
 * -----------------------------
 * Desc:
 */
@Component
public class AllAcceptedListener implements ApplicationListener<ApplicationEvent> {
  @Override
  public void onApplicationEvent(ApplicationEvent event) {
      System.out.println(">>>>>>>>>>>>>>>>event:" + event);
  }
}

AllAcceptedListener

导演负责分发事件

package com.meituan.spring.testcase.listener;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/2b498cb62385235f8e14acb5cdf760af.html