2014-05-11 65 views
1

當我嘗試使用應用程序上下文爲該圓類創建bean時,我正面臨以下錯誤! Circle類實現包含方法draw()Shape接口。創建bean時出錯:bean實例化失敗;嵌套的異常是java.lang.ExceptionInInitializerError

配置:

我學習春天和已經建立了Java項目在Eclipse中所有需要的jar(Spring和Apache通用日誌)。我在src文件夾的classpath中有spring.xml。也嘗試下載最新的jar(Spring 4.0.4發佈版,以前使用4.0.0),但現在不起作用。想不到任何解決方案來解決這個問題。

錯誤:

May 11, 2014 6:20:50 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 
INFO: Refreshing org[email protected]90832e: startup date [Sun May 11 18:20:50 EDT 2014]; root of context hierarchy 
May 11, 2014 6:20:50 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
INFO: Loading XML bean definitions from class path resource [spring.xml] 
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor': Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1076) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1021) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) 
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:88) 
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:609) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) 
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) 
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) 
    at com.springApplication.DrawingApplication.main(DrawingApplication.java:16) 
Caused by: java.lang.ExceptionInInitializerError 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1071) 
    ... 13 more 
Caused by: java.lang.NullPointerException 
    at org.springframework.beans.PropertyEditorRegistrySupport.<clinit>(PropertyEditorRegistrySupport.java:92) 
    ... 14 more 

DrawingApplication.java

package com.springApplication; 

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


public class DrawingApplication { 

    public static void main(String[] args) { 

     //The beanFactory reads from an XML file  
     //BeanFactory context = new XmlBeanFactory(new FileSystemResource("spring.xml")); 

     //Application Context provides -- Event notification and more than Bean Factory 
     ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); 

     //Here we pass the id of the bean from the XML given in the above line 
     Shape shape = (Shape)context.getBean("circle"); 

     //Calling the draw method through object local object created using Spring 
     shape.draw(); 
    } 

} 

Circle.java

package com.springApplication; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Qualifier; 

public class Circle implements Shape{ 

    private Point center; 

    public Point getCenter() { 
     return center; 
    } 

    @Autowired 
    @Qualifier("circleRelated") 
    public void setCenter(Point center) { 
     this.center = center; 
    } 

    @Override 
    public void draw() { 
     System.out.println("Drawing a Circle"); 
     System.out.println("Center Point is: (" + center.getX() + ", " + center.getY() + ")");  
    } 
} 

Point.java

package com.springApplication; 

public class Point { 

    private int x; 
    private int y; 

    public int getX() { 
     return x; 
    } 
    public void setX(int x) { 
     this.x = x; 
    } 
    public int getY() { 
     return y; 
    } 
    public void setY(int y) { 
     this.y = y; 
    } 
}  

spring.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:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

<bean id="pointA" class="com.springApplication.Point"> 
    <qualifier value="circleRelated" /> 
    <property name="x" value="0"/> 
    <property name="y" value="0"/> 
</bean> 

<bean id="PointB" class="com.springApplication.Point"> 
    <property name="x" value="0"/> 
    <property name="y" value="20"/> 
</bean> 

<bean id="PointC" class="com.springApplication.Point"> 
    <property name="x" value="-20"/> 
    <property name="y" value="0"/> 
</bean> 

<bean id="circle" class="com.springApplication.Circle"> 
</bean> 

<!-- <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> --> 
<context:annotation-config/> 
</beans> 

請讓我知道是否有其他需要。有人請幫忙!

抱歉關於錯誤的放置!

@Andrei斯特凡罐的
名單---------
共享記錄-1.1.3
彈簧AOP-4.0.4.RELEASE
彈簧AOP-4.0.4 .RELEASE-的Javadoc
彈簧AOP-4.0.4.RELEASE來源
彈簧方面-4.0.4.RELEASE
彈簧方面-4.0.4.RELEASE-的Javadoc
彈簧方面-4.0.4 .RELEASE-sources
spring-beans-4.0.4.RELEASE
彈簧豆-4.0.4.RELEASE-的Javadoc
彈簧豆-4.0.4.RELEASE來源
彈簧積聚SRC-4.0.4.RELEASE
彈簧上下文4.0.4.RELEASE
彈簧上下文4.0.4.RELEASE-的Javadoc
彈簧上下文4.0.4.RELEASE來源
彈簧上下文支撐4.0.4.RELEASE
彈簧上下文支持-4.0.4。 RELEASE-javadoc
spring-context-support-4.0.4.RELEASE-sources
spring-core-4.0.4.RELEASE
spring-core-4.0.4.RE租賃的Javadoc
彈簧芯4.0.4.RELEASE來源
彈簧表達-4.0.4.RELEASE
彈簧表達-4.0.4.RELEASE-的Javadoc
彈簧表達-4.0.4。RELEASE-源
彈簧框架-BOM-4.0.4.RELEASE
彈簧框架-BOM-4.0.4.RELEASE-的Javadoc
彈簧框架-BOM-4.0.4.RELEASE來源
彈簧儀器4.0.4.RELEASE
彈簧儀器4.0.4.RELEASE-的Javadoc
彈簧儀器4.0.4.RELEASE來源
彈簧儀器Tomcat的4.0.4.RELEASE
彈簧instrument-tomcat-4.0.4.RELEASE-javadoc
spring-instrument-tomcat-4.0.4.RELEASE-sources
spring-j DBC-4.0.4.RELEASE
彈簧JDBC-4.0.4.RELEASE-的Javadoc
彈簧JDBC-4.0.4.RELEASE來源
彈簧JMS-4.0.4.RELEASE
彈簧JMS- 4.0.4.RELEASE-的Javadoc
彈簧JMS-4.0.4.RELEASE來源
彈簧消息傳遞4.0.4.RELEASE
彈簧消息傳遞4.0.4.RELEASE-的Javadoc
彈簧messaging- 4.0.4.RELEASE-sources
spring-orm-4.0.4.RELEASE
spring-orm-4.0.4.RELEASE -javadoc
彈簧ORM-4.0.4.RELEASE來源
彈簧OXM-4.0.4.RELEASE
彈簧OXM-4.0.4.RELEASE-的Javadoc
彈簧OXM-4.0.4.RELEASE來源
彈簧 - 測試 - 4.0.4.RELEASE
彈簧 - 測試 - 4.0.4.RELEASE-的Javadoc
彈簧 - 測試 - 4.0.4.RELEASE來源
彈簧-TX-4.0.4.RELEASE
彈簧-tx-4.0.4.RELEASE-javadoc
spring-tx-4.0.4.RELEASE-sources
spring-web-4.0.4.RELEASE
彈簧網絡4.0.4.RELEASE-的Javadoc
彈簧網絡4.0.4.RELEASE來源
彈簧webmvc-4.0.4.RELEASE
彈簧webmvc-4.0.4.RELEASE-的Javadoc
彈簧webmvc-4.0.4.RELEASE來源
彈簧webmvc的portlet-4.0.4.RELEASE
彈簧webmvc的portlet-4.0.4.RELEASE-的Javadoc
彈簧webmvc的portlet-4.0。 4.RELEASE來源
spring-websocket-4.0.4.RELEASE
spring-websocket-4.0.4.RELEASE-javadoc
spring-websoc ket-4.0.4.RELEASE-sources

所有這些加上其他默認系統庫都被引用。發生

+0

從stacktrace的底部開始 - 我在那裏看到一個NullPointerException(除非有更多的你沒有包含)。 – Jason

+0

真正的問題是在org.springframework.beans.PropertyEditorRegistrySupport中拋出的NPE。 ';即在這個類的靜態初始化器中。配置錯誤?問題是沒有與該NPE相關的消息,因此很難說出那裏發生了什麼。 – fge

+0

你在classpath中有什麼罐子?請張貼清單。 –

回答

4

該問題的原因

PropertyEditorRegistrySupport.class.getClassLoader() 

null。根據JavaDoc中,發生這種情況如果類PropertyEditorRegistrySupport已經由引導類加載器,而不是系統類加載器加載:

Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class was loaded by the bootstrap class loader.

也許你的Spring庫是認可類路徑?

+0

是的,我所有的Spring庫都在我的類路徑中。你能否介紹一下什麼是引導類加載器和系統加載器?通過蝕創建的類路徑文件看起來像: \t \t \t \t user3626602

+0

非常感謝Christoph。我找到了解決方案。在eclipse中添加庫時,它會要求您選擇將用戶庫添加到引導類加載器。我取消選中複選框,我的代碼現在完美運行。感謝提示! – user3626602

+1

很高興爲您提供幫助。不過,我已經向Spring提出了一個問題,看看他們是否願意考慮這種情況:https://jira.spring.io/browse/SPR-11780 –

相關問題