簡體   English   中英

使用SpEL復制bean的引用不起作用

[英]Using SpEL to copy references of a beans is not working

我一直在嘗試使用Spring。 我希望通過使用SpEL為另一個bean復制一個bean的值和引用屬性。

考慮一下這個bean:

<bean id="kenny" class="com.springinaction.springidol.Instrumentalist">
    <property name="song" value="#{'Jingle Bells'}" /> 
    <property name="instrument" ref="piano" /> 
</bean>

我希望將其值復制到另一個bean中,如下所示:

<bean id="carl" class="com.springinaction.springidol.Instrumentalist"> 
    <property name="song" value="#{kenny.song}" /> 
    <property name="instrument" ref="#{kenny.instrument}" /> <-- I GET EXCEPTION OVER HERE

</bean>

但是,由於第二個prpoperty無法復制肯尼的文書,因此我得到了例外。 正確復制了歌曲,沒有任何異常

我得到這個例外:

線程“主”中的異常org.springframework.beans.factory.BeanCreationException:創建在類路徑資源[Beans.xml]中定義的名稱為“ carl”的bean時出錯:設置時無法解析對bean'#{kenny.instrument}'的引用Bean屬性“工具”; 嵌套異常為org.springframework.beans.factory.BeanExpressionException:表達式解析失敗; 嵌套異常是org.springframework.expression.spel.SpelEvaluationException:EL1008E:(pos 6):在類型為“ com.springinaction.springidol.Instrumentalist”的對象上找不到字段或屬性“ instrument”

知道如何復制儀器並將其設置為ID為“ carl”的bean嗎?

ref是對另一個bean的引用; 在您的情況下,您想使用kenny bean的instrumemt屬性的值。

你應該用

 <property name="instrument" value="#{kenny.instrument}" />

假設在Instrumentalist上有一個getInstrument()方法。

暫無
暫無

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

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