@Qualifier赢得了解决豆子问题的工作

时间:2017-06-13 09:50:42

标签: java spring

我正在使用Spring 4和Java 8.我正在使用@Qualifier来解决Spring bean中的歧义问题,但是当我尝试运行应用程序时,它会向我显示出同样模糊问题的错误。

Car.Java

package beans;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.beans.factory.annotation.Qualifier;

public class Car {
    @Autowired
    @Qualifier("e1")
    private Engine engine;

    public void printData() {

        System.out.println("Car Model Year "+engine.getModelyear());
    }
}

Engine.java

package beans;

public class Engine {
    private String modelyear;

    public String getModelyear() {
        return modelyear;
    }

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

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"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" >
   <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"></bean>
   <bean id="e1" class="beans.Engine">
   <property name="modelyear" value="2017"/>
   </bean>
   <bean id="e2" class="beans.Engine">
   <property name="modelyear" value="2018"/>
   </bean>
    <bean id="c" class="beans.Car">

   </bean>

</beans>

我的测试课程是:

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

3 个答案:

答案 0 :(得分:1)

=添加到===声明

query.setInteger("LipidId", LipidId);

答案 1 :(得分:0)

您正在混合xml和非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.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">
    <context:annotation-config />

到你的xml文件。

或者您可以在xml中手动设置bean

<bean id="c" class="beans.Car">
    <property name="engine" ref="e1"/>
</bean>

但是你需要修改你的Car类:

private Engine engine;

public void setEngine(Engine engine) {
    this.engine = engine;
}

答案 2 :(得分:0)

实际上,您的问题如下: 你已经在spring xml中定义了AutowiredAnnotationBeanPostProcessor来处理@Autowired注释,但你的@quualifier注释没有任何post bean处理器。这就是你的@qualifier被忽略的原因.Hence bean模糊。

添加

<context:annotation-config/>

并删除

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"></bean>

因为不再需要它了。