spring event事件(二)@EventListener注解

一、介绍

1、简介

springboot还提供了一个 @EventListener 注解来实现注解式监听。

这是监听器的简化写法,不需要再继承ApplicationListener。

2、指定事件

(1)可以在注解中指定

@EventListener(MyEvent.class)

@EventListener({MyEvent.class, ContextRefreshedEvent.class})

(2)也可以直接在入参中声明

@EventListener
public void contextRefreshedEventListener(ContextRefreshedEvent event){}


@EventListener
public void myEventListener(MyLogEvent event) {}

二、原理

三、使用

如前面的多个事件监听,使用@EventListener 注解可以有两种写法:

@Slf4j
@Component
public class MyTask {
    @EventListener
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ContextRefreshedEvent) {
            log.info("监听到 ContextRefreshedEvent...");
        }
        if (event instanceof MyEvent) {
            log.info("监听到 MyEvent...");
            MyEvent myEvent = (MyEvent) event;
            System.out.println("时间:" + myEvent.getTime() + " 信息:" + myEvent.getMsg());
        }
    }
}
@Slf4j
@Component
public class MyTask {
    @EventListener
    public void myEventListener(MyEvent event) {
        log.info("监听到 MyEvent...");
        MyEvent myEvent = (MyEvent) event;
        System.out.println("时间:" + myEvent.getTime() + " 信息:" + myEvent.getMsg());
    }

    @EventListener
    public void contextRefreshedEventListener(ContextRefreshedEvent event) {
        log.info("监听到 ContextRefreshedEvent...");
    }
}

 四、demo

下面监听内置事件、自定义事件和自定义批量事

Spring框架提供了一系列注解用于处理事件机制,其中`@EventListener`和`@TransactionalEventListener`都是在特定场景下使用的。下面是对这两个注解的详细介绍: ### @EventListener `@EventListener`是一个用于监听特定类型的事件(例如来自`ApplicationEventPublisher`、`Message`、`ContextRefreshedEvent`等)的注解。它可以应用于方法上,当指定事件发生时,该方法会被触发执行。这是Spring事件驱动架构的基本组成部分之一。 #### 使用示例: ```java import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Component; @Component public class MyEventListener implements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent event) { // 在这里执行事件相关的操作 System.out.println("应用程序已经刷新并准备好接收数据"); } } ``` ### @TransactionalEventListener `@TransactionalEventListener`与`@EventListener`类似,但是它还增加了事务管理的功能。这个注解允许你在监听事件的同时保证交易的原子性,即要么所有的操作都成功完成,要么都不执行。 #### 主要特点: - **事务管理**:当你在一个方法中使用了`@TransactionalEventListener`,Spring将自动在这个方法运行时创建一个新的事务。如果方法正常结束,则事务提交;如果方法抛出异常,则事务回滚。 - **易于整合**:这种方式简化了在处理事件时需要手动管理事务的过程,使得应用在处理异步事件时仍能保持一致性。 #### 使用示例: ```java import org.springframework.transaction.annotation.Transactional; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; @Component public class TransactionalEventListener { @EventListener @Transactional public void handleEvent(MyCustomEvent event) { // 这里执行事件处理,并假设存在一些业务逻辑操作 System.out.println("处理自定义事件:" + event.getMessage()); } } ``` 注意:这里的`MyCustomEvent`应该是实际自定义事件的类实例。 ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

w_t_y_y

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

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

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

打赏作者

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

抵扣说明:

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

余额充值