簡體   English   中英

當該bean已經通過xml設置了另一個屬性時,是否可以通過自動裝配注入屬性?

[英]Is it possible to inject a property through autowired when that bean already has another property set through xml

我正在嘗試定義一個bean。

<bean id="prop1" class="Prop1"/>

<bean id="myBean" class="myClass">
    <property name="prop2">
        <bean class="Prop2">
    </property>
</bean>

現在我上這堂課

public class myClass(){
    @Autowired
    private Prop1 prop1;
    private Prop2 prop2;

    public setProp2(Prop2 prop2){
        this.prop2 = prop2;
    }
    ...
}

我知道我可以將屬性添加到myBean bean中,但是我試圖避免這種情況。 現在,我在prop1上獲得了空值。 可以初始化prop1嗎? 如果沒有,請解釋或給我鏈接以閱讀有關此內容。

TIA

這應該工作。 它可能不起作用,因為您可能缺少AutowiredAnnotationPostProcessor ,它是負責在@Autowired依賴項中進行接線的bean后處理器。 您可以通過將它們添加到xml配置文件中來獲得它:

<context:annoation-config/>

要么

<context:component-scan/>

暫無
暫無

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

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