簡體   English   中英

運行時的Spring @Qualifier值

[英]Spring @Qualifier value at runtime

是否可以在運行時分配@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嗎?

是的,我看到您為一個類定義了兩個bean。 @Qualifier注釋將告訴類必須從spring配置文件中使用哪個bean

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM