spring 事件监听机制的三种实现方式

spring 事件监听机制的三种实现方式,无序监听(实现事件方式) ,有序监听(实现smart事件方式), 注解监听

准备一个事件:
@Slf4j
public class OrderEvent extends ApplicationEvent {
public OrderEvent(Object source,String createOrder) {
super(source);
log.debug(“订单:{}下单”,createOrder);
}
}

发布他:

@RestController
public class TestDemo {
@Autowired
ApplicationContext applicationContext;
@GetMapping(“test01”)
public void test01(@RequestParam String msg){
applicationContext.publishEvent(new OrderEvent(“testListener”,msg));
}
}

无序监听方式:
/*

  • 实现事件方式的实现无序监听
    */
    @Component
    public class SmsListener implements ApplicationListener {
    @Override
    public void onApplicationEvent(OrderEvent orderEvent) {

     System.out.println(orderEvent.getSource());
     System.out.println("发送短信");
    

    }
    }


/*

  • 实现事件方式的实现无序监听
    */
    @Component
    public class WxListener implements ApplicationListener {
    @Override
    public void onApplicationEvent(OrderEvent orderEvent) {
    System.out.println(orderEvent.getSource());
    System.out.println(“发送微信”);
    }
    }

有序监听:

/**

  • 有序监听
    */
    @Component
    public class AutoListener implements SmartApplicationListener {

    @Override
    public boolean supportsEventType(Class<? extends ApplicationEvent> aClass) {
    System.out.println(“类型对比”+aClass.toString());
    return aClass == OrderEvent.class;
    }

    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
    System.out.println(“执行事件逻辑”);
    }

    @Override
    public boolean supportsSourceType(@Nullable Class<?> sourceType) {
    System.out.println(“sourceType对比”+sourceType.toString());
    return sourceType == String.class;
    }

    @Override
    public int getOrder() {
    return 1;
    }
    }

注解监听:
/**

  • 注解实现方式
    */
    @Component
    public class AnonaListener {

    @EventListener
    public void test01(OrderEvent orderEvent){
    System.out.println(“注解方式发送短信”+orderEvent.getSource());
    }

    @EventListener
    public void test02(OrderEvent orderEvent){
    System.out.println(“注解方式发送微信”+orderEvent.getSource());
    }
    }

运行结果:
类型对比class org.springframework.context.event.ContextRefreshedEvent
类型对比class org.springframework.context.event.ContextRefreshedEvent
程序启动完毕
2019-06-29 18:24:47.815 INFO 4902 — [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8888 (http) with context path ‘’
类型对比class org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent
类型对比class org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent
2019-06-29 18:24:47.818 INFO 4902 — [ main] com.Application : Started Application in 17.851 seconds (JVM running for 23.512)
类型对比class org.springframework.boot.context.event.ApplicationStartedEvent
类型对比class org.springframework.boot.context.event.ApplicationStartedEvent
类型对比class org.springframework.boot.context.event.ApplicationReadyEvent
类型对比class org.springframework.boot.context.event.ApplicationReadyEvent
2019-06-29 18:25:08.626 INFO 4902 — [nio-8888-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet ‘dispatcherServlet’
2019-06-29 18:25:08.626 INFO 4902 — [nio-8888-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet ‘dispatcherServlet’
2019-06-29 18:25:08.634 INFO 4902 — [nio-8888-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 8 ms
类型对比class com.spring.event.OrderEvent
sourceType对比class java.lang.String
注解方式发送微信testListener
注解方式发送短信testListener
执行事件逻辑
testListener
发送短信
testListener
发送微信
类型对比class org.springframework.web.context.support.ServletRequestHandledEvent
类型对比class org.springframework.web.context.support.ServletRequestHandledEvent
类型对比class org.springframework.context.event.ContextClosedEvent
类型对比class org.springframework.context.event.ContextClosedEvent

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阳十三

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值