当前位置: 首页 > 知识库问答 >
问题:

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

马正初
2023-03-14

当我尝试使用循环类的Application Context创建bean时,我面临以下错误!Circle类实现了Shape接口,其中包含一个方法绘制()

配置:

我正在学习Spring,并在eclipse中设置了一个Java项目,其中包含所有必需的jar(Spring和Apache公共日志记录)。我在src文件夹的类路径中有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.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$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() + ")");      
    }
}

点.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 Stefan
Jars ---------
Commons-loging-1.1.3
spring-expression-4.0.4.RELEASE
spring-expression-4.0.4.RELEASE-javadoc
spring-framework-bom-4.0.4.RELEASE-源
spring-framework-bom-4.0.4.RELEASE
spring-framework-bom-4.0.4.RELEASE-javadoc
spring-instrument-4.0.4.RELEASE-源
spring-instrument-4.0.4.RELEASE
spring-instrument-4.0.4.RELEASE-javadoc
spring-instrument-tomcat-4.0.4.RELEASE-源
spring-instrument-tomcat-4.0.4.RELEASE
spring-context-4.0.4.RELEASE
spring-context-4.0.4.RELEASE-javadoc
spring-context-4.0.4.RELEASE-源
spring-context-support-4.0.4.RELEASE
spring-context-support-4.0.4.RELEASE-javadoc
spring-context-support-4.0.4.RELEASE-源
spring-core-4.0.4.RELEASE
spring-core-4.0.4.RELEASE-javadoc
spring-core-4.0.4.RELEASE-源
spring-expression-4.0.4.RELEASE
spring-aop-4.0.4.RELEASE-javadoc
spring-aop-4.0.4.RELEASE-源
spring-aop-4.0.4.RELEASE
spring-aspects-4.0.4.RELEASE-javadoc
spring-aspects-4.0.4.RELEASE-源
spring-aspects-4.0.4.RELEASE
spring-beans-4.0.4.RELEASE-javadoc
spring-beans-4.0.4.RELEASE
spring-beans-4.0.4.RELEASE
spring-build-src-4.0.4.RELEASE-javadoc spring-jms-4.0.4。RELEASE
spring-jms-4.0.4。RELEASE-javadoc
spring-jms-4.0.4。RELEASE
spring-Message-4.0.4。RELEASE-javadoc
spring-Message-4.0.4。RELEASE-源
spring-orm-4.0.4。RELEASE
spring-orm-4.0.4。RELEASE-javadoc
spring-orm-4.0.4。RELEASE-源
spring-oxm-4.0.4。RELEASE
spring-oxm-4.0.4。RELEASE-源
spring-test-4.0.4。RELEASE
spring-test-4.0.4。RELEASE
Spring测试。RELERELEASE-javadoc
spring-web-4.0.4。RELEASE-源
spring-webmvc-4.0.4。RELEASE
spring-webmvc-4.0.4。RELEASE-源
spring-webmvc-portlet-4.0.4。RELEASE
spring-webmvc-portlet-4.0.4。RELEASE-javadoc
spring-webmvc-portlet-4.0.4。RELEASE-源
spring-webock-4.0.4。RELEASE
spring-webock-4.0.4。RELEASE
spring-webock-4.0.4。释放源

所有这些加上其他默认的系统库都被引用。

共有1个答案

松国兴
2023-03-14

出现此问题的原因是

PropertyEditorRegistrySupport.class.getClassLoader()

isnull。根据JavaDoc,如果类属性编辑注册支持已由引导类加载器而不是系统类加载器加载,则会发生这种情况:

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

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

 类似资料: