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注解就可以了
启动:启动成功