Spring事件通知机制详解(2)

/**
 * Created by zhangxiaoguang on 16/1/28 上午1:41.
 * -----------------------------
 * Desc:
 */
@Component
public class CustomizePublisher implements ApplicationEventPublisherAware {

private ApplicationEventPublisher applicationEventPublisher;

public void publish(MealEvent event) {
      applicationEventPublisher.publishEvent(event);
  }

@Override
  public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
      this.applicationEventPublisher = applicationEventPublisher;
  }
}

CustomizePublisher

负责处理吃饭事件的演员

package com.meituan.spring.testcase.listener;

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

/**
 * Created by zhangxiaoguang on 16/1/27 下午11:27.
 * -----------------------------
 * Desc:
 */
@Component
public class MealListener implements ApplicationListener<MealEvent> {
  @Override
  public void onApplicationEvent(MealEvent event) {
      System.out.println(String.format(">>>>>>>>>>>thread:%s,type:%s,event:%s",
            Thread.currentThread().getName(), event.getMealEnum(), event));

dispatchEvent(event);
  }

private void dispatchEvent(MealEvent event) {
      switch (event.getMealEnum()) {
        case breakfast:
            System.out.println(event.getMealEnum() + " to handle!!!");
            break;
        case lunch:
            System.out.println(event.getMealEnum() + " to handle!!!");
            break;
        case dinner:
            System.out.println(event.getMealEnum() + " to handle!!!");
            break;
        default:
            System.out.println(event.getMealEnum() + " error!!!");
            break;
      }
  }
}

MealListener

吃饭消息

package com.meituan.spring.testcase.listener;

import org.springframework.context.ApplicationEvent;

/**
 * Created by zhangxiaoguang on 16/1/27 下午11:24.
 * -----------------------------
 * Desc:吃饭事件
 */
public class MealEvent extends ApplicationEvent {

private MealEnum mealEnum;

/**
    * @param mealContent
    *        吃什么
    * @param mealEnum
    *        早餐还是午餐?
    */
  public MealEvent(String mealContent, MealEnum mealEnum) {
      super(mealContent);
      this.mealEnum = mealEnum;
  }

public MealEnum getMealEnum() {
      return mealEnum;
  }
}

MealEvent

工具

package com.meituan.spring.testcase.listener;

/**
 * Created by zhangxiaoguang on 16/1/27 下午11:29.
 * -----------------------------
 * Desc:
 */
public enum MealEnum {
  breakfast,
  lunch,
  dinner
}

MealEnum

令人厌烦的演员

package com.meituan.spring.testcase.listener;

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

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

TroubleListener

令人厌烦的事件

package com.meituan.spring.testcase.listener;

import org.springframework.context.ApplicationEvent;

/**
 * Created by zhangxiaoguang on 16/1/27 下午11:24.
 * -----------------------------
 * Desc:令人厌烦的事件
 */
public class TroubleEvent extends ApplicationEvent {
  public TroubleEvent(Object source) {
      super(source);
  }
}

TroubleEvent

总结

详细定制 event 类型的,则相关定制的listener会处理对应的消息,其他listener不会管闲事;

制定顶级 event 类型的,ApplicationEvent的,则会处理所有的事件。

Spring中如何配置Hibernate事务

Struts2整合Spring方法及原理

基于 Spring 设计并实现 RESTful Web Services

Spring-3.2.4 + Quartz-2.2.0集成实例

使用 Spring 进行单元测试

运用Spring注解实现Netty服务器端UDP应用程序

Spring 3.x 企业应用开发实战 PDF完整高清扫描版+源代码

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

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