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;  
    }  
}

2、创建事件监听器

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

import org.springframework.context.ApplicationListener;  
import org.springframework.stereotype.Component;  
  
@Component  
public class CustomEventListener implements ApplicationListener<CustomEvent> {  
    @Override  
    public void onApplicationEvent(CustomEvent event) {  
        System.out.println("Received custom event - " + event.getMessage());  
    }  
}

注意:@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);  
    }  
}

4、触发事件

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

    @Autowired
    private EventPublisherService eventPublisherService;


    @GetMapping("/event")
    public void someMethod() {
        eventPublisherService.doStuffAndPublishAnEvent("Hello, World!");
    }

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

5、结果请添加图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot实现监听器的过程是通过定义监听器类并注册到Spring容器中来实现的。首先,我们需要创建一个监听器类来监听特定的事件。在SpringBoot中,我们可以通过实现ApplicationListener接口来创建监听器类。然后,我们可以在监听器类中重写onApplicationEvent方法,该方法会在监听到指定事件发生时被调用。 接下来,我们需要将监听器注册到Spring容器中。在SpringBoot中,我们可以通过在监听器类上添加@Component注解来将其注册为一个Bean。这样,Spring容器在启动时会自动扫描并注册这个监听器。 在注册监听器后,当指定的事件发生时,Spring容器会根据监听器的优先级顺序依次调用监听器的onApplicationEvent方法。在这个方法中,我们可以编写我们需要执行的逻辑。 总结起来,SpringBoot实现监听器的步骤如下: 1. 创建一个监听器类,并实现ApplicationListener接口。 2. 在监听器类中重写onApplicationEvent方法,编写监听到事件后的逻辑。 3. 在监听器类上添加@Component注解,将其注册为一个Bean。 4. 在SpringBoot启动时,Spring容器会自动扫描并注册这个监听器。 5. 当指定事件发生时,Spring容器会自动调用监听器的onApplicationEvent方法执行相应的逻辑。 引用参考资料: :《springboot中使用监听器》 :《SpringBoot实现拦截器、过滤器、监听器过程解析》 :《目前只有一个实现类EventPublishingRunListener,它把监听的过程过装成了SpringApplicationEvent事并通过内部属性广播出去,属性名为initialMulticaster,是ApplicationEventMulticaster接口实现类 SimpleApplicstionEventMulticaster.广播出去的事件对象会被SpringApplication中的listeners属性进行处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值