我一直在尝试使用 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>
但是,我对第二个属性有一个异常(exception),因为它无法复制肯尼的乐器。歌曲复制正确,没有抛出任何异常
我得到这个异常:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carl' defined in class path resource [Beans.xml]: Cannot resolve reference to bean '#{kenny.instrument}' while setting bean property 'instrument'; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 6): Field or property 'instrument' cannot be found on object of type 'com.springinaction.springidol.Instrumentalist'
知道如何复制仪器并将其设置为 ID 为“carl”的 bean 吗?
最佳答案
A ref
是对另一个 bean 的引用;在您的情况下,您希望使用 kenny
bean 的 instrumemt
属性的值。
你应该使用
<property name="instrument" value="#{kenny.instrument}" />
这假设 Instrumentalist
上有一个 getInstrument()
方法。
关于java - 使用 SpEL 复制 bean 的引用不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17506276/