我正在尝试一个与依赖注入和自动装配相关的简单弹簧框架示例。我正面临一个难以解决的错误。使用了以下jar文件:
*commons-logging.jar
*org.springframework.asm-3.0.1.RELEASE.jar
*org.springframework.beans-3.0.1.RELEASE.jar
*org.springframework.context-3.0.1.RELEASE.jar
*org.springframework.core-3.0.1.RELEASE.jar
*org.springframework.expression-3.0.1.RELEASE.jar
以下是代码段:
Engine.java
package beans;
public class Engine {
private String modelyear;
public Engine() {
}
public Engine(String modelyear) {
this.modelyear = modelyear;
}
public String getModelyear() {
return modelyear;
}
public void setModelyear(String modelyear) {
this.modelyear = modelyear;
}
}
Car.java
package beans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class Car {
@Qualifier(value="e1")
@Autowired
private Engine engine;
public void printData(){
System.out.println("Engine Model Year ="+engine.getModelyear());
}
}
spring.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- Activate autowire annotation -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean id="c" class="beans.Car" />
<bean id="e1" class="bean.Engine">
<property name="modelyear" value="2015" />
</bean>
<bean id="e2" class="bean.Engine">
<property name="modelyear" value="2016" />
</bean>
</beans>
Client.java
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import beans.Car;
public class Client {
public static void main(String[] args) {
ApplicationContext ap = new ClassPathXmlApplicationContext("resources/spring.xml");
Car c = (Car)ap.getBean("c");
c.printData();
}
}
执行上面的代码后,我收到如下错误:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'c': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: beans.Engine beans.Car.engine; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [bean.Engine] for bean with name 'e1' defined in class path resource [resources/spring.xml]; nested exception is java.lang.ClassNotFoundException: bean.Engine
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1055)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:562)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:871)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at test.Client.main(Client.java:11)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: beans.Engine beans.Car.engine; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [bean.Engine] for bean with name 'e1' defined in class path resource [resources/spring.xml]; nested exception is java.lang.ClassNotFoundException: bean.Engine
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:507)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:283)
... 13 more
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [bean.Engine] for bean with name 'e1' defined in class path resource [resources/spring.xml]; nested exception is java.lang.ClassNotFoundException: bean.Engine
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1208)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:570)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1277)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:303)
at org.springframework.beans.factory.BeanFactoryUtils.beanNamesForTypeIncludingAncestors(BeanFactoryUtils.java:185)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:810)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:767)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:685)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
... 15 more
Caused by: java.lang.ClassNotFoundException: bean.Engine
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:258)
at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:408)
at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1229)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1200)
... 23 more
答案 0 :(得分:1)
您错误地定义了<bean id="e1" class="bean.Engine">
&amp; <bean id="e2" class="bean.Engine">
在xml中。
根据Engine类
beans.Engine
答案 1 :(得分:1)
我尝试了基于xsd的上下文版本并且您的代码有效:
<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
...