创建 bean 时出错:bean 实例化失败;嵌套异常是 java.lang.ExceptionInInitializerError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23598748/
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-08-14 00:14:33  来源:igfitidea点击:

Error while creating bean: Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError

javaspring

提问by user3626602

I am facing the following error when I try to create bean using Application Context for the circle class! Circle class implements the Shapeinterface which contains a method draw().

当我尝试使用应用程序上下文为圆类创建 bean 时,我面临以下错误!Circle 类实现了Shape包含方法的接口draw()

Configuration:

配置:

I am learning spring and have setup a Java Project in eclipse with all the required jars (Spring and Apache commons logging). I have the spring.xml in my classpath in the src folder. Have also tried downloading the latest jars (Spring 4.0.4 release, previously worked with 4.0.0) but now is not working. Cannot think of any solution to fix this.

我正在学习 spring 并在 eclipse 中设置了一个 Java 项目,其中包含所有必需的 jars(Spring 和 Apache 公共日志记录)。我的 src 文件夹中的类路径中有 spring.xml。还尝试下载最新的 jars(Spring 4.0.4 版本,以前使用 4.0.0)但现在不起作用。想不出任何解决方案来解决这个问题。

Error:

错误:

May 11, 2014 6:20:50 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@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.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

圆环.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

点.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>

Please let me know if anything else is needed. Someone please help !

如果需要其他任何东西,请告诉我。有人请帮忙!

Sorry about the wrong placing!!

抱歉放错了地方!!

@Andrei Stefan
List of Jars ---------
commons-logging-1.1.3
spring-aop-4.0.4.RELEASE
spring-aop-4.0.4.RELEASE-javadoc
spring-aop-4.0.4.RELEASE-sources
spring-aspects-4.0.4.RELEASE
spring-aspects-4.0.4.RELEASE-javadoc
spring-aspects-4.0.4.RELEASE-sources
spring-beans-4.0.4.RELEASE
spring-beans-4.0.4.RELEASE-javadoc
spring-beans-4.0.4.RELEASE-sources
spring-build-src-4.0.4.RELEASE
spring-context-4.0.4.RELEASE
spring-context-4.0.4.RELEASE-javadoc
spring-context-4.0.4.RELEASE-sources
spring-context-support-4.0.4.RELEASE
spring-context-support-4.0.4.RELEASE-javadoc
spring-context-support-4.0.4.RELEASE-sources
spring-core-4.0.4.RELEASE
spring-core-4.0.4.RELEASE-javadoc
spring-core-4.0.4.RELEASE-sources
spring-expression-4.0.4.RELEASE
spring-expression-4.0.4.RELEASE-javadoc
spring-expression-4.0.4.RELEASE-sources
spring-framework-bom-4.0.4.RELEASE
spring-framework-bom-4.0.4.RELEASE-javadoc
spring-framework-bom-4.0.4.RELEASE-sources
spring-instrument-4.0.4.RELEASE
spring-instrument-4.0.4.RELEASE-javadoc
spring-instrument-4.0.4.RELEASE-sources
spring-instrument-tomcat-4.0.4.RELEASE
spring-instrument-tomcat-4.0.4.RELEASE-javadoc
spring-instrument-tomcat-4.0.4.RELEASE-sources
spring-jdbc-4.0.4.RELEASE
spring-jdbc-4.0.4.RELEASE-javadoc
spring-jdbc-4.0.4.RELEASE-sources
spring-jms-4.0.4.RELEASE
spring-jms-4.0.4.RELEASE-javadoc
spring-jms-4.0.4.RELEASE-sources
spring-messaging-4.0.4.RELEASE
spring-messaging-4.0.4.RELEASE-javadoc
spring-messaging-4.0.4.RELEASE-sources
spring-orm-4.0.4.RELEASE
spring-orm-4.0.4.RELEASE-javadoc
spring-orm-4.0.4.RELEASE-sources
spring-oxm-4.0.4.RELEASE
spring-oxm-4.0.4.RELEASE-javadoc
spring-oxm-4.0.4.RELEASE-sources
spring-test-4.0.4.RELEASE
spring-test-4.0.4.RELEASE-javadoc
spring-test-4.0.4.RELEASE-sources
spring-tx-4.0.4.RELEASE
spring-tx-4.0.4.RELEASE-javadoc
spring-tx-4.0.4.RELEASE-sources
spring-web-4.0.4.RELEASE
spring-web-4.0.4.RELEASE-javadoc
spring-web-4.0.4.RELEASE-sources
spring-webmvc-4.0.4.RELEASE
spring-webmvc-4.0.4.RELEASE-javadoc
spring-webmvc-4.0.4.RELEASE-sources
spring-webmvc-portlet-4.0.4.RELEASE
spring-webmvc-portlet-4.0.4.RELEASE-javadoc
spring-webmvc-portlet-4.0.4.RELEASE-sources
spring-websocket-4.0.4.RELEASE
spring-websocket-4.0.4.RELEASE-javadoc
spring-websocket-4.0.4.RELEASE-sources

@Andrei Stefan
罐子列表---------
commons-logging-1.1.3
spring-aop-4.0.4.RELEASE
spring-aop-4.0.4.RELEASE-javadoc
spring-aop-4.0.4。 RELEASE-sources
spring-aspects-4.0.4.RELEASE
spring-aspects-4.0.4.RELEASE-javadoc
spring-aspects-4.0.4.RELEASE-sources
spring-beans-4.0.4.RELEASE
spring-beans-4.0.4 .RELEASE-javadoc
spring-beans-4.0.4.RELEASE-sources
spring-build-src-4.0.4.RELEASE
spring-context-4.0.4.RELEASE
spring-context-4.0.4.RELEASE-javadoc
spring- context- 4.0.4.RELEASE-sources
spring-context-support-4.0.4.RELEASE
spring-context-support-4.0.4.RELEASE-javadoc
spring-context-support-4.0.4.RELEASE-sources
spring-core-4.0.4.RELEASE
spring-core-4.0.4.RELEASE-javadoc
spring-core-4.0.4.RELEASE-sources
spring-expression-4.0.4.RELEASE
spring-expression-4.0.4.RELEASE- javadoc
spring-expression-4.0.4.RELEASE-sources
spring-framework-bom-4.0.4.RELEASE
spring-framework-bom-4.0.4.RELEASE-javadoc
spring-framework-bom-4.0.4.RELEASE-sources
spring -instrument-4.0.4.RELEASE
spring-instrument-4.0.4.RELEASE-javadoc
spring-instrument-4.0.4.RELEASE-sources
spring-instrument-tomcat-4.0.4.RELEASE
spring-instrument-tomcat-4.0.4 .RELEASE-javadoc
spring-instrument-tomcat-4.0.4.RELEASE-sources
spring-jdbc-4.0.4.RELEASE
spring-jdbc-4.0.4.RELEASE-javadoc
spring-jdbc-4.0.4.RELEASE-sources
spring-jms-4.0.4.RELEASE
spring-jms-4.0.4.RELEASE-javadoc
spring-jms-4.0.4.RELEASE-sources
spring-messaging-4.0.4。 RELEASE
spring-messaging-4.0.4.RELEASE-javadoc
spring-messaging-4.0.4.RELEASE-sources
spring-orm-4.0.4.RELEASE
spring-orm-4.0.4.RELEASE-javadoc
spring-orm-4.0.4 .RELEASE-sources
spring-oxm-4.0.4.RELEASE
spring-oxm-4.0.4.RELEASE-javadoc
spring-oxm-4.0.4.RELEASE-sources
spring-test-4.0.4.RELEASE
spring-test-4.0。 4.RELEASE-javadoc
spring-test-4.0.4.RELEASE-sources
spring-tx-4.0.4.RELEASE
spring-tx-4.0.4.RELEASE-javadoc
spring-tx-4.0.4.RELEASE-sources
spring-web-4.0.4.RELEASE
spring-web-4.0.4.RELEASE-javadoc
spring-web-4.0.4.RELEASE-sources
spring-webmvc-4.0.4.RELEASE
spring-webmvc-4.0.4.RELEASE- javadoc
spring-webmvc-4.0.4.RELEASE-sources
spring-webmvc-portlet-4.0.4.RELEASE
spring-webmvc-portlet-4.0.4.RELEASE-javadoc
spring-webmvc-portlet-4.0.4.RELEASE-sources
spring -websocket-4.0.4.RELEASE
spring-websocket-4.0.4.RELEASE-javadoc
spring-websocket-4.0.4.RELEASE-sources

All of these plus other default system libraries are referenced.

所有这些以及其他默认系统库都被引用。

采纳答案by Christoph Leuzinger

The problem occurs because

出现问题是因为

PropertyEditorRegistrySupport.class.getClassLoader()

is null. According to JavaDoc, this happens if the class PropertyEditorRegistrySupporthas been loaded by the bootstrap class loader instead of the system class loader:

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.

一些实现可能使用 null 来表示引导类加载器。如果此类由引导类加载器加载,则此方法将在此类实现中返回 null。

Perhaps your Spring libraries are on the endorsed class path?

也许您的 Spring 库位于认可的类路径上?