@Autowired注释在spring-Getting BeanCreationException和java.lang.NoSuchMethodError

时间:2017-03-11 12:03:27

标签: spring dependency-injection inversion-of-control autowired

我写了一个小代码来检查Spring中的@Autowired注释,这是我的代码。

       package beans;

        //import org.springframework.beans.factory.annotation.AutoWired;
        import org.springframework.beans.factory.annotation.Autowired;
        //import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
        import org.springframework.beans.factory.annotation.Qualifier;
        //import org.springframework.beans.factory.annotation.*;

        public class Car {

            @Autowired
            @Qualifier(value="e1")
            private Engine engine;

            //no need to have setters or constructors here
            public void printData()
            {

                System.out.println("Engine model year: " +engine.getModelyear());

            }

        }



        package beans;

        public class Engine {
            private String modelyear;
            //generate setter and getter

            public void setModelyear(String modelyear) {
                this.modelyear = modelyear;
            }

            public String getModelyear() {
                return modelyear;
            }

        }

    package test;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import beans.Car;
    //import beans.Test;
    public class Client {

        /**
         * @param args
         */
        public static void main(String[] args) {
        ApplicationContext ap = new ClassPathXmlApplicationContext("resource/spring.xml");
        Car c=(Car)ap.getBean("c");
        c.printData();

        }

    }

<!--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="engine" class="beans.Engine">
            <property name="modelyear" value="2015"/>
        </bean> 
        <bean id="e1" class="beans.Engine">
            <property name="modelyear" value="2016"/>
        </bean>

        <bean id="c" class="beans.Car">
        </bean>
</beans>

当我尝试运行Client.java类时,我收到以下错误: 有人可以建议我为什么要面对这个问题吗?

2017年3月11日上午10:19:24 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息:刷新org.springframework.context.support.ClassPathXmlApplicationContext@70d18a80:显示名称[org.springframework.context.support.ClassPathXmlApplicationContext@70d18a80];启动日期[2017年3月11日星期六10:19:24 IST 2017];上下文层次结构的根 2017年3月11日上午10:19:24 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息:从类路径资源[resource / spring.xml]加载XML bean定义 2017年3月11日上午10:19:25 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory INFO:应用程序上下文的Bean工厂[org.springframework.context.support.ClassPathXmlApplicationContext@70d18a80]:org.springframework.beans.factory.support.DefaultListableBeanFactory@18d1cf9e 2017年3月11日上午10:19:25 org.springframework.context.support.AbstractApplicationContext $ BeanPostProcessorChecker postProcessAfterInitialization 信息:Bean&#39; org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor&#39;不适合所有BeanPostProcessors处理(例如:不符合自动代理条件) 2017年3月11日上午10:19:25 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 信息:在org.springframework.beans.factory.support.DefaultListableBeanFactory@18d1cf9e中预先实例化单例:定义bean [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor,engine,e1,c];工厂层级的根 2017年3月11日上午10:19:25 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons 信息:在org.springframework.beans.factory.support.DefaultListableBeanFactory@18d1cf9e中销毁单例:定义bean [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor,engine,e1,c];工厂层级的根 线程&#34; main&#34;中的例外情况org.springframework.beans.factory.BeanCreationException:使用名称&#39; c创建bean时出错:字段自动装配失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private beans.Engine beans.Car.engine;嵌套异常是java.lang.NoSuchMethodError:org.springframework.beans.factory.config.ConfigurableListableBeanFactory.resolveDependency(Lorg / springframework / beans / factory / config / DependencyDescriptor; Ljava / lang / String; Ljava / util / Set; Lorg / springframework /豆类/的TypeConverter)Ljava /郎/对象; 引起:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private beans.Engine beans.Car.engine;嵌套异常是java.lang.NoSuchMethodError:org.springframework.beans.factory.config.ConfigurableListableBeanFactory.resolveDependency(Lorg / springframework / beans / factory / config / DependencyDescriptor; Ljava / lang / String; Ljava / util / Set; Lorg / springframework /豆类/的TypeConverter)Ljava /郎/对象; 引起:java.lang.NoSuchMethodError:org.springframework.beans.factory.config.ConfigurableListableBeanFactory.resolveDependency(Lorg / springframework / beans / factory / config / DependencyDescriptor; Ljava / lang / String; Ljava / util / Set; Lorg / springframework /豆类/的TypeConverter)Ljava /郎/对象;     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredElement.inject(AutowiredAnnotationBeanPostProcessor.java:361)     在org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:61)     在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:228)     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:414)     在org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:249)     在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:155)     在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:246)     在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:291)     在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)     在org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:122)     在org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:66)     在test.Client.main(Client.java:13)

我正在使用的罐子是:

List of jars used and reference libraries while creating the project

0 个答案:

没有答案