SpringBoot中@EventListener使用

本文介绍了在SpringBoot中如何使用@EventListener注解来监听事件。如果不设置value,可以通过参数形式监听一个事件;若设置value,则可以监听一个或多个事件。需要注意的是,监听多个事件时,方法不能有参数,否则会引发异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、不设置value值,采用参数形式创建事件监听,只能监听一个事件

@Component
public class MyEvenntListener {

    @EventListener
    public void doMyEvent(MyEvent01 myEvent01){
        System.out.println("myEvent01");
    }
}

2、设置value值,可以设置一个事件或者多个事件

监听一个事件

@Component
public class MyEvenntListener {

    @EventListener({MyEvent01.class})
    public void doMyEvent(){
        System.out.println("myEvent01");
    }
}

监听多个事件

@Component
public class MyEvenntListener {

    @EventListener({MyEvent01.class,ApplicationEvent.class})
    public void doMyEvent(){
        System.out.println("myEvent01");
    }
}

注意,如果监听多个事件,那个监听方法不能有参数,否则报错:

Caused by: java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_281]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_281]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_281]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_281]
at org.springframework.context.event.ApplicationListenerMethodAdapter.doInvoke(ApplicationListenerMethodAdapter.java:305) ~[spring-context-5.2.13.RELEASE.jar:5.2.13.RELEASE]
… 23 common frames omitted

如果需要监听多个事件,那么就用@EventListener去设置多个事件,不能在监听方法里面配置多个事件参数,否则也会报错。

org.springframework.beans.factory.BeanInitializationException: Failed to process @EventListener annotation on bean with name ‘myEvenntListener’; nested exception is java.lang.IllegalStateException: Maximum one parameter is allowed for event listener method: public void com.don.eventdemo.config.MyEvenntListener.doMyEvent(com.don.eventdemo.event.MyEvent01,org.springframework.boot.web.context.WebServerInitializedEvent)
at org.springframework.context.event.EventListenerMethodProcessor.afterSingletonsInstantiated(EventListenerMethodProcessor.java:157) ~[spring-context-5.3.6.jar:5.3.6]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:963) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.6.jar:5.3.6]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.6.jar:5.3.6]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144) ~[spring-boot-2.4.5.jar:2.4.5]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:782) [spring-boot-2.4.5.jar:2.4.5]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:774) [spring-boot-2.4.5.jar:2.4.5]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:439) [spring-boot-2.4.5.jar:2.4.5]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:339) [spring-boot-2.4.5.jar:2.4.5]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1340) [spring-boot-2.4.5.jar:2.4.5]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1329) [spring-boot-2.4.5.jar:2.4.5]
at com.don.eventdemo.EventDemoApplication.main(EventDemoApplication.java:10) [classes/:na]
Caused by: java.lang.IllegalStateException: Maximum one parameter is allowed for event listener method: public void com.don.eventdemo.config.MyEvenntListener.doMyEvent(com.don.eventdemo.event.MyEvent01,org.springframework.boot.web.context.WebServerInitializedEvent)
at org.springframework.context.event.ApplicationListenerMethodAdapter.resolveDeclaredEventTypes(ApplicationListenerMethodAdapter.java:127) ~[spring-context-5.3.6.jar:5.3.6]
at org.springframework.context.event.ApplicationListenerMethodAdapter.(ApplicationListenerMethodAdapter.java:117) ~[spring-context-5.3.6.jar:5.3.6]
at org.springframework.context.event.DefaultEventListenerFactory.createApplicationListener(DefaultEventListenerFactory.java:56) ~[spring-context-5.3.6.jar:5.3.6]
at org.springframework.context.event.EventListenerMethodProcessor.processBean(EventListenerMethodProcessor.java:200) ~[spring-context-5.3.6.jar:5.3.6]
at org.springframework.context.event.EventListenerMethodProcessor.afterSingletonsInstantiated(EventListenerMethodProcessor.java:154) ~[spring-context-5.3.6.jar:5.3.6]
… 11 common frames omitted

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值