【Java】自定义SpringApplicationRunListener实例化出现异常

题目描述

自定义SpringApplicationRunListener实例化出现异常:非法参数异常
Spring Boot 2.1.6
JDK 1.8.0_192

题目来源及自己的思路

我跟着教程写自定义的SpringApplicationRunListener对象,尝试一下该接口下的7个Listener方法。
我跟着代码走走走,好像是看不懂的样子,哈哈哈哈……
是在createSpringFactoriesInstances函数的ClassUtils.forName(name, classLoader);出现问题
但可惜我看不懂,只知道应该是装载类,但我写的MySpringApplicationRunListener写了有参构造器.

【Java】自定义SpringApplicationRunListener实例化出现异常

相关代码

public class MySpringApplicationRunListener implements SpringApplicationRunListener {

public MySpringApplicationRunListener(SpringApplication application, String[] args) {

}

@Override*7

starting/environmentPrepared/contextPrepared/contextLoaded/started/running/failed

你期待的结果是什么?实际看到的错误信息又是什么?

Exception in thread "main" java.lang.IllegalArgumentException:

Cannot instantiate interface org.springframework.context.ApplicationListener :

com.inbreeze.springboot.listener.MySpringApplicationRunListener

Caused by: java.lang.IllegalArgumentException:

class com.inbreeze.springboot.listener.MySpringApplicationRunListener

is not assignable to interface

org.springframework.context.ApplicationListener

Exception in thread "main" java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.context.ApplicationListener : com.inbreeze.springboot.listener.MySpringApplicationRunListener

at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:437)

at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:419)

at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:412)

at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:269)

at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:249)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202)

at com.inbreeze.springboot.SpringBoot07SourcecodeApplication.main(SpringBoot07SourcecodeApplication.java:10)

Caused by: java.lang.IllegalArgumentException: class com.inbreeze.springboot.listener.MySpringApplicationRunListener is not assignable to interface org.springframework.context.ApplicationListener

at org.springframework.util.Assert.assignableCheckFailed(Assert.java:673)

at org.springframework.util.Assert.isAssignable(Assert.java:604)

at org.springframework.util.Assert.isAssignable(Assert.java:635)

at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:431)

... 7 more

Process finished with exit code 1

现在的问题是,我不知道该如何正确写一个自定义的SpringApplicationRunListener完全实现类
我翻阅了Spring.io current/reference 并没有相关实例【至于又没类似的讲解,我还真没找到】

求救,大佬们5555

回答

为了回答你的问题,特意注册了账号,不过感觉你的问题应该自己解决了吧。

把spring.factories发出来,估计是你的加载类不对,应该是:
org.springframework.boot.SpringApplicationRunListener=\
...

以上是 【Java】自定义SpringApplicationRunListener实例化出现异常 的全部内容, 来源链接: utcz.com/a/90687.html

回到顶部