• springboot实现监听


    1、新建ApplicationEvent

    在Spring Boot中实现监听器(Listener)的一种常见方式是使用Spring Boot的事件监听机制。

    下面是一个简单的步骤说明,帮助你实现一个自定义的监听器:

    创建事件:首先,你需要创建一个事件类。这个类将继承ApplicationEvent类,并添加一些自定义的数据。

    import org.springframework.context.ApplicationEvent;  
      
    public class CustomEvent extends ApplicationEvent {  
        private String message;  
      
        public CustomEvent(Object source, String message) {  
            super(source);  
            this.message = message;  
        }  
      
        public String getMessage() {  
            return message;  
        }  
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2、创建事件监听器

    你需要创建一个事件监听器类,该类将实现ApplicationListener接口,并指定需要监听的事件类型。

    import org.springframework.context.ApplicationListener;  
    import org.springframework.stereotype.Component;  
      
    @Component  
    public class CustomEventListener implements ApplicationListener {  
        @Override  
        public void onApplicationEvent(CustomEvent event) {  
            System.out.println("Received custom event - " + event.getMessage());  
        }  
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    注意:@Component注解用于将此类作为Spring Bean注册到应用程序上下文中。

    3、发布事件

    要触发事件,你需要创建一个事件发布者。在Spring应用中,你可以通过ApplicationEventPublisher接口来发布事件。

    import org.springframework.beans.factory.annotation.Autowired;  
    import org.springframework.context.ApplicationEventPublisher;  
    import org.springframework.stereotype.Service;  
      
    @Service  
    public class EventPublisherService {  
        @Autowired  
        private ApplicationEventPublisher applicationEventPublisher;  
      
        public void doStuffAndPublishAnEvent(final String message) {  
            System.out.println("Publishing custom event. ");  
            CustomEvent customEvent = new CustomEvent(this, message);  
            applicationEventPublisher.publishEvent(customEvent);  
        }  
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    4、触发事件

    你可以在需要的地方调用EventPublisherService的doStuffAndPublishAnEvent方法来触发事件。例如,你可以在一个Controller或者Service中调用这个方法。

        @Autowired
        private EventPublisherService eventPublisherService;
    
    
        @GetMapping("/event")
        public void someMethod() {
            eventPublisherService.doStuffAndPublishAnEvent("Hello, World!");
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    当你调用doStuffAndPublishAnEvent方法时,它将发布一个CustomEvent事件,你的CustomEventListener将接收到这个事件,并执行相应的操作。

    5、结果请添加图片描述

  • 相关阅读:
    面试官:介绍一下 Redis 三种集群模式
    SpringMVC <url-pattern/>解读
    Leetcode 203.移除链表元素
    【日常需求】一次使用EasyExcel而引发的问题与思考~
    echarts-雷达图和仪表图
    更改Windows上的远程桌面的侦听端口
    编译器一日一练(DIY系列之总结)
    Dubbo 实战 - Mock 调用
    微电网两阶段鲁棒优化问题(Matlab代码实现)
    Mybatis-plus工具学习笔记(1)---[基本概述,入门案例搭建,通用service接口使用]
  • 原文地址:https://blog.csdn.net/qq_36151389/article/details/133129249