简体   繁体   中英

org.springframework.beans.factory.BeanCreationException on autowired fields

I was trying a simple spring framework example related to dependency injection and autowiring. I am facing an error which is hard to resolve. Following jar files were used:

*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

Following are the code snippets:

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();      
    }
}

After executing the code above, I am getting error as follows:

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

You have wrongly defined the <bean id="e1" class="bean.Engine"> & <bean id="e2" class="bean.Engine"> in the xml. The class should be beans.Engine as per the package name defined on the Engine class

I tried xsd based version of context and your code worked:

<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 />

...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM