2012-10-10 32 views
5

我正在練習Spring,並在嘗試實例化上下文時收到java.lang.ExceptionInInitializerError異常。異常出現在下面,我的代碼在後面。我從以前簡化了我的實驗。java.lang.ExceptionInInitializerError在Spring中創建應用程序上下文時出現異常

例外

Oct 17, 2012 5:54:22 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh 
INFO: Refreshing org[email protected]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 

我的配置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> 

我的測試類。

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); 

} 

} 
+0

你是積極的嗎?你正在使用相同版本的類路徑上的所有Spring JARs? –

+0

編輯您的帖子幷包含_entire_ stacktrace。 –

+0

@mattb我使用spring springsce.org網站上發佈的最新泉水罐。 – cyotee

回答

4

線17不對應context.getBean("userRepository"線,它對應於在這之前,你初始化Spring上下文行。實際上,你也可以通過stacktrace看到它在ClassPathXmlApplicationContext的第83行失敗,其中存在該類的構造函數。

無論如何,當Spring無論何種原因(例如構造函數,resource loading issuesclass loading issues等)無法創建任何bean時,通常會引發此異常。我會建議減少春​​季班級的日誌級別和您自己的圖書館以查看下面發生了什麼。

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

而且還要檢查你的資源文件,無論它們是由您的應用程序和您的所有對象的構造函數訪問,如果他們產生任何錯誤,等等

+0

我可以在哪裏做到這一點? – cyotee

+0

我假設你已經安裝了一個日誌框架,用它在回答中給出的級別來配置它,以查看什麼是中斷。 – melihcelik

0

我面臨同樣的問題。我從項目中移除了所有的彈簧罐。 然後再次將其全部粘貼到項目文件夾&中,將其全部添加到構建路徑中。 它的工作。 不太確定它是如何發生的..

相關問題