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注解就可以了

启动:启动成功

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值