SpringBoot中使用监听器

1.定义一个事件

/**
 * 定义事件
 * @author hrui
 * @date 2024/7/25 12:46
 */
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.通过ApplicationEventPublisher 发布事件

@Service
public class EventPublisherService {
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    public void publishEvent(String message) {
        CustomEvent event = new CustomEvent(this, message);
        applicationEventPublisher.publishEvent(event);
    }
}

3.通过监听器订阅事件

/**
 * 监听事件,订阅事件
 * @author hrui
 * @date 2024/7/25 12:49
 */
@Component
public class CustomEventListener {


    @Async
    //@EventListener
    //@TransactionalEventListener 有事务的监听
    @EventListener(condition = "#event.message == 'hello'") //SPEL(Spring表达式)条件监听 对象也可以用== 判断
    //@EventListener(condition = "#event.message .equals('hello') ")
    //@EventListener(condition = "#event.message.endsWith("123") ") //很多方法可以选择
    public void handleCustomEvent(CustomEvent event) {
        System.out.println(Thread.currentThread().getName());
        System.out.println("Received event - " + event.getMessage());
    }
}

第三步简单实用了@EventListener注解  如果不用就需要这么做

去实现ApplicationListerner<处理的事件>  需不需要异步自己看着办

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

@Component
public class CustomEventListener implements ApplicationListener<CustomEvent> {

    @Override
    public void onApplicationEvent(CustomEvent event) {
        // 条件判断
        if ("hello".equals(event.getMessage())) {
            // 异步处理
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName());
                System.out.println("Received event - " + event.getMessage());
            }).start();
        }
    }
}

/**
 * @author hrui
 * @date 2024/7/25 12:51
 */
@RestController
public class ListenerController {

    @Autowired
    private EventPublisherService eventPublisherService;

    @GetMapping("/listener")
    public String listener(String message)
    {
        eventPublisherService.publishEvent(message);
        return "listener";
    }
}

  • 11
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot使用监听器可以通过实现`ApplicationListener`接口来实现。下面是一个简单的示例: 首先,创建一个自定义监听器类,实现`ApplicationListener`接口,并指定监听的事件类型。例如,我们创建一个监听器类`MyEventListener`来监听`ApplicationStartedEvent`事件: ```java import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.context.ApplicationListener; public class MyEventListener implements ApplicationListener<ApplicationStartedEvent> { @Override public void onApplicationEvent(ApplicationStartedEvent event) { System.out.println("应用程序已启动!"); // 在这里可以添加自定义的逻辑 } } ``` 然后,在Spring Boot应用程序的主类注册监听器。可以使用`SpringApplication.addListeners()`方法来添加监听器: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication app = new SpringApplication(MyApplication.class); app.addListeners(new MyEventListener()); // 注册自定义监听器 app.run(args); } } ``` 这样,在应用程序启动时,`MyEventListener`的`onApplicationEvent()`方法就会被调用。 除了`ApplicationStartedEvent`,Spring Boot还提供了其他一些常用的事件,例如`ApplicationReadyEvent`、`ContextRefreshedEvent`等,你可以选择适合你需求的事件类型来监听。 希望以上信息能对你有帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

hrui0706

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

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

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

打赏作者

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

抵扣说明:

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

余额充值