• SpringBoot事件发布监听


    一、好处

    模块解耦、异步、消息广播、触发某一事件

    二、实现

    1.要监听的事件

    1. import org.springframework.context.ApplicationEvent;
    2. /**
    3. * @author :cxp
    4. * @description :要监听的事件
    5. * @date :2022/8/1 16:09
    6. */
    7. public class TestEvent extends ApplicationEvent {
    8. private static final long serialVersionUID = -2866878925912581424L;
    9. public TestEvent(Object source) {
    10. super(source);
    11. }
    12. }

    2.事件发布

    1. import cn.hutool.core.util.StrUtil;
    2. import org.springframework.context.ApplicationContext;
    3. import org.springframework.stereotype.Component;
    4. import javax.annotation.Resource;
    5. /**
    6. * @author :cxp
    7. * @description :发布者
    8. * @date :2022/8/1 16:11
    9. */
    10. @Component
    11. public class TestPublisher {
    12. @Resource
    13. private ApplicationContext applicationContext;
    14. public void publishEvent(String message) {
    15. if (StrUtil.isNotBlank(message)) {
    16. System.out.println("发布器所在线程:" + Thread.currentThread().getName());
    17. applicationContext.publishEvent(new TestEvent(message));
    18. }
    19. }
    20. }

    3.订阅者

    1. import org.springframework.context.event.EventListener;
    2. import org.springframework.stereotype.Component;
    3. /**
    4. * @author :cxp
    5. * @description :监听者1
    6. * @date :2022/8/1 16:10
    7. */
    8. @Component
    9. public class TestListener1 {
    10. @EventListener
    11. public void listen1(TestEvent testEvent) {
    12. System.out.println("TestListener1");
    13. System.out.println("所在线程:" + Thread.currentThread().getName());
    14. System.out.println("事件:" + testEvent);
    15. System.out.println("事件的数据:" + testEvent.getSource());
    16. }
    17. }
    18. *****************************************************************************************
    19. import org.springframework.context.event.EventListener;
    20. import org.springframework.scheduling.annotation.Async;
    21. import org.springframework.stereotype.Component;
    22. /**
    23. * @author :cxp
    24. * @description :监听者2(异步执行,*项目中记得加@EnableAsync注解)
    25. * @date :2022/8/1 16:10
    26. */
    27. @Component
    28. @Async("asyncExecutor")
    29. public class TestListener2 {
    30. // 支持SPEL表达式
    31. @EventListener
    32. public void listen1(TestEvent testEvent) {
    33. System.out.println("TestListener2");
    34. System.out.println("所在线程:" + Thread.currentThread().getName());
    35. System.out.println("事件:" + testEvent);
    36. System.out.println("事件的数据:" + testEvent.getSource());
    37. }
    38. }

    4.触发事件

    1. @Slf4j
    2. @RestController
    3. @RequestMapping("/event")
    4. public class TestController {
    5. @Resource
    6. private TestPublisher testPublisher;
    7. @GetMapping("/testEvent")
    8. public ResultData testEvent(String message) {
    9. log.info("[message]={}", message);
    10. testPublisher.publishEvent(message);
    11. return ResultData.success(message);
    12. }
    13. }
  • 相关阅读:
    SS8837T智能门锁驱动马达-门锁电机驱动解决方案
    312.戳气球
    刷题记录:牛客NC19782Tree
    orb-slam2 从单目开始的简单学习(7):Optimizer
    推荐几款简单易用的协作化项目管理工具
    Python知识点(史上最全)
    解密Elasticsearch:深入探究这款搜索和分析引擎
    数字时钟制作
    Android 组件逻辑漏洞漫谈
    Android12 am命令的使用及实现流程分析
  • 原文地址:https://blog.csdn.net/weixin_43155301/article/details/126117878