当我尝试使用应用程序上下文为圆类创建bean时,我遇到以下错误! Circle类实现Shape
接口,该接口包含方法draw()
。
配置:
我正在学习spring并在eclipse中使用所有必需的jar(Spring和Apache commons logging)设置了Java Project。我在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.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() + ")");
}
}
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 Stefan
罐子清单---------
共享记录-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来源
弹簧豆,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
弹簧上下文支撑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
弹簧表达-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
弹簧仪器Tomcat的4.0.4.RELEASE-的Javadoc
弹簧仪器Tomcat的4.0.4.RELEASE来源
弹簧JDBC-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
弹簧消息传递4.0.4.RELEASE来源
弹簧ORM-4.0.4.RELEASE
弹簧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
弹簧-TX-4.0.4.RELEASE来源
弹簧网络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来源
弹簧的WebSocket-4.0.4.RELEASE
弹簧的WebSocket-4.0.4.RELEASE-的Javadoc
弹簧的WebSocket-4.0.4.RELEASE来源
引用了所有这些以及其他默认系统库。
答案 0 :(得分:4)
问题出现是因为
PropertyEditorRegistrySupport.class.getClassLoader()
是null
。根据JavaDoc,如果类PropertyEditorRegistrySupport
已由引导类加载器而不是系统类加载器加载,则会发生这种情况:
某些实现可能使用null来表示引导类加载器。 如果此类由引导程序类加载器加载,则此方法将在此类实现中返回null。
也许您的Spring库位于认可的类路径上?