Spring @Qualifier值在运行时

时间:2015-03-27 10:23:31

标签: java spring dependency-injection

是否可以在运行时分配@Qualifier值。假设我有两个相同类型的bean和一个带依赖注入的类

  <bean id="typeA" class="com.debopam.test.Type">
         <property name="typevalue" value="Export" />
    </bean>
    <bean id="typeB" class="com.debopam.test.Type">
         <property name="typevalue" value="Import" />
    </bean>

    public class Product {
    private Integer price;
    private String name;

    @Autowired
    @Qualifier("typeB")
    private Type type;

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    public Type getType() {
        return type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

有没有办法在运行时定义Product类中的类型而不是硬编码/指定值?如果是,请发一些代码,否则建议使用ApplicationContext.getBean(“bean name”)在运行时加载bean?

1 个答案:

答案 0 :(得分:-1)

是的,我看到你为一个类定义了两个bean。 @Qualifier注释将告诉类必须从spring配置文件中使用哪个bean

相关问题