gpt4 book ai didi

java - Autowiring 字段上的 org.springframework.beans.factory.BeanCreationException

转载 作者:行者123 更新时间:2023-11-30 02:20:10 25 4
gpt4 key购买 nike

我正在尝试一个与依赖注入(inject)和 Autowiring 相关的简单 spring 框架示例。我面临一个难以解决的错误。使用了以下 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

最佳答案

您错误地定义了 <bean id="e1" class="bean.Engine"> & <bean id="e2" class="bean.Engine">在 XML 中。类(class)应该是 beans.Engine根据 Engine 类中定义的包名称

关于java - Autowiring 字段上的 org.springframework.beans.factory.BeanCreationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47088891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com