java.lang.ExceptionInInitializerError 在Spring中创建应用程序上下文时出现异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12827178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:22:29  来源:igfitidea点击:

java.lang.ExceptionInInitializerError Exception when creating Application Context in Spring

javaspringexceptionapplicationcontext

提问by Robert Greathouse

I am practicing with Spring, and am getting a java.lang.ExceptionInInitializerError exception when I try to instantiate the context. The Exception appears below, with my code following it. I have simplified my experiment from before.

我正在练习 Spring,并且在尝试实例化上下文时收到 java.lang.ExceptionInInitializerError 异常。异常出现在下面,我的代码紧随其后。我已经简化了我之前的实验。

The Exception

例外

Oct 17, 2012 5:54:22 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@570c16b7: startup date [Wed Oct 17 17:54:22 CDT 2012]; root of context hierarchy
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.springframework.context.support.AbstractRefreshableApplicationContext.createBeanFactory(AbstractRefreshableApplicationContext.java:195)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:128)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:535)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:449)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at helloworld.HelloWorldTest.main(HelloWorldTest.java:13)
Caused by: java.lang.NullPointerException
at org.springframework.beans.factory.support.DefaultListableBeanFactory.<clinit>(DefaultListableBeanFactory.java:105)
... 7 more

My configuration XML

我的配置 XML

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

<bean id="messageContainer" class="helloworld.MessageContainer">
    <property name="message" value="Hello World">
    </property>
</bean>

<bean id="messageOutputService" class="helloworld.MessageOutputService">
</bean>

My test class.

我的测试班。

package helloworld;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloWorldTest {

/**
 * @param args
 */
public static void main(String[] args)
{
    ApplicationContext context = new ClassPathXmlApplicationContext("HelloWorldTest-context.xml");

    MessageContainer message = context.getBean(MessageContainer.class);

    MessageOutputService service = context.getBean(MessageOutputService.class);

    service.outputMessageToConsole(message);

}

}

采纳答案by melihcelik

Line 17 does not correspond to the context.getBean("userRepository"line, it corresponds to the line before that where you initialize the Spring context. And actually you can also see that through stacktrace, it says it failed on Line 83 of ClassPathXmlApplicationContextwhere exists the constructor of that class.

第 17context.getBean("userRepository"行不对应于行,它对应于初始化 Spring 上下文之前的行。实际上,您还可以通过堆栈跟踪看到,它说它在ClassPathXmlApplicationContext该类的构造函数所在的第 83 行失败。

Anyway, this exception is generally thrown when Spring can not create any bean for whatever reason (Exceptions in constructors, resource loading issues, class loading issues, etc.). I would suggest decreasing the log level for spring classes and for your own libraries to see what's going on underneath.

无论如何,当 Spring 由于某种原因(构造函数中的异常、资源加载问题类加载问题等)无法创建任何 bean 时,通常会抛出此异常。我建议降低 spring 类和你自己的库的日志级别,看看下面发生了什么。

org.springframework=TRACE
com.gamemanagertest=TRACE
com.gamemanagertest=TRACE

And also check your resource files whether they're accessible by your application and constructors of all your objects if they produce any errors, etc.

并且还要检查您的资源文件,如果它们产生任何错误等,您的应用程序和所有对象的构造函数是否可以访问它们。

回答by Sanket Birhamane

I was facing the same issue. I removed all the spring jars from the project. Then again pasted all of them in a project folder & added all of them to build path. And it worked. Not very sure of how it happened..

我面临同样的问题。我从项目中删除了所有弹簧罐。然后再次将它们全部粘贴到项目文件夹中并将它们全部添加到构建路径中。它奏效了。不太确定它是怎么发生的..