运行程序时,出现错误:应用程序运行失败 [关闭]
Posted
技术标签:
【中文标题】运行程序时,出现错误:应用程序运行失败 [关闭]【英文标题】:While run the program, i got a error that : Application run failed [closed] 【发布时间】:2021-12-16 03:05:57 【问题描述】:Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-11-01 16:20:38.322 ERROR 15820 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentController' defined in file [D:\Bhuvana\springwithH2DB\springwithH2DB\target\classes\com\example\h2Database\springwithH2DB\StudentController.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.h2Database.springwithH2DB.StudentController]: Constructor threw exception; nested exception is java.lang.ClassCastException: class org.jboss.logging.Log4j2Logger cannot be cast to class org.apache.logging.log4j.Logger (org.jboss.logging.Log4j2Logger and org.apache.logging.log4j.Logger are in unnamed module of loader 'app')
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1334) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1232) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.12.jar:5.3.12]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.12.jar:5.3.12]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.6.jar:2.5.6]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-2.5.6.jar:2.5.6]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[spring-boot-2.5.6.jar:2.5.6]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) ~[spring-boot-2.5.6.jar:2.5.6]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-2.5.6.jar:2.5.6]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) ~[spring-boot-2.5.6.jar:2.5.6]
at com.example.h2Database.springwithH2DB.SpringwithH2DbApplication.main(SpringwithH2DbApplication.java:11) ~[classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.h2Database.springwithH2DB.StudentController]: Constructor threw exception; nested exception is java.lang.ClassCastException: class org.jboss.logging.Log4j2Logger cannot be cast to class org.apache.logging.log4j.Logger (org.jboss.logging.Log4j2Logger and org.apache.logging.log4j.Logger are in unnamed module of loader 'app')
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:224) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1326) ~[spring-beans-5.3.12.jar:5.3.12]
... 17 common frames omitted
Caused by: java.lang.ClassCastException: class org.jboss.logging.Log4j2Logger cannot be cast to class org.apache.logging.log4j.Logger (org.jboss.logging.Log4j2Logger and org.apache.logging.log4j.Logger are in unnamed module of loader 'app')
at com.example.h2Database.springwithH2DB.StudentController.<init>(StudentController.java:17) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) ~[na:na]
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:211) ~[spring-beans-5.3.12.jar:5.3.12]
... 19 common frames omitted
【问题讨论】:
【参考方案1】:不读就无法成功编程。
查看堆栈跟踪。你有一个控制器依赖的问题。关于log4j
和log4j2
存在混淆。确保您已正确设置依赖项。我建议使用log4j2
。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentController' defined in file [D:\Bhuvana\springwithH2DB\springwithH2DB\target\classes\com\example\h2Database\springwithH2DB\StudentController.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.h2Database.springwithH2DB.StudentController]: Constructor threw exception; nested exception is java.lang.ClassCastException: class org.jboss.logging.Log4j2Logger cannot be cast to class org.apache.logging.log4j.Logger (org.jboss.logging.Log4j2Logger and org.apache.logging.log4j.Logger are in unnamed module of loader 'app')
【讨论】:
【参考方案2】:正如@duffymo 提到的,这是处理日志问题的导入类的问题。
我建议使用原生 Log4j2,绕过 SLF4J 或 apache commons。
导入
import org.apache.logging.log4j.Logger;
创建记录器对象
Private static final logger logger = org.apache.logging.log4j.LogManager.getLogger (ProductService.class);
对象实现
logger.error ("message");
logger.info ("message");
etc.
【讨论】:
以上是关于运行程序时,出现错误:应用程序运行失败 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章
在虚拟函数周围使用#ifdef预处理器会导致与库链接的程序出现运行时错误[关闭]
电脑出现应用程序正常初始化(0xc0000022)失败,怎么回事?