springboot自定义监听器SpringApplicationRunListener的坑

Exception in thread "main" java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.boot.SpringApplicationRunListener : com.itheima.springbootlistener.listener.MySpringApplicationRunListener
    at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:467)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:449)
    at org.springframework.boot.SpringApplication.getRunListeners(SpringApplication.java:437)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298)
    at com.itheima.springbootlistener.SpringbootListenerApplication.main(SpringbootListenerApplication.java:10)
Caused by: java.lang.NoSuchMethodException: com.itheima.springbootlistener.listener.MySpringApplicationRunListener.<init>(org.springframework.boot.SpringApplication, [Ljava.lang.String;)
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.getDeclaredConstructor(Class.java:2178)
    at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:462)
    ... 6 more

解决方案:首先看下SpringApplicationRunListener内部提供的实现类,其中第一个是springboot自己提供的实现类,第二个是我自定义的实现类

springboot自己提供了EventPublishingRunListener的实现类,该实现类中加了一个构造方法,故自定义监听器中加一个构造器就ok了

注意:会有警告,只要去掉@Component注解就可以了

启动:启动成功

 

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring Boot提供了一种自定义事件的机制,可以在应用程序中定义自己的事件,并在需要的时候发布这些事件。自定义事件可以用于在应用程序中实现解耦,使得不同的组件之间可以松散地耦合在一起。下面是一个简单的示例,演示如何在Spring Boot应用程序中定义和发布自定义事件: 1.定义一个自定义事件类,继承自ApplicationEvent类,例如MyEvent: ```java public class MyEvent extends ApplicationEvent { private String message; public MyEvent(Object source, String message) { super(source); this.message = message; } public String getMessage() { return message; } } ``` 2.定义一个事件发布者类,例如MyEventPublisher: ```java @Component public class MyEventPublisher { @Autowired private ApplicationEventPublisher publisher; public void publishEvent(String message) { publisher.publishEvent(new MyEvent(this, message)); } } ``` 3.定义一个事件监听器类,例如MyEventListener: ```java @Component public class MyEventListener { @EventListener public void onApplicationEvent(MyEvent event) { System.out.println("Received spring custom event - " + event.getMessage()); } } ``` 4.在需要发布事件的地方,注入MyEventPublisher并调用publishEvent方法即可: ```java @Autowired private MyEventPublisher publisher; ... publisher.publishEvent("Hello, world!"); ``` 这样,当应用程序发布MyEvent事件时,MyEventListener中的onApplicationEvent方法将被调用,并输出事件的消息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值