2014-12-05 34 views
0

我需要一個bean這樣的Java的Spring IoC bean創建值

<bean id="studentWithSchool" class="com.model.Student" scope="prototype">  
    <property name="school"> 
     <bean class="com.model.School" scope="prototype"/> 
    </property> 
</bean> 

這是確定。

我的問題是我有學生從不同的bean的方法返回。

我通常像這樣加載bean是屬性。

<property name='beanProperty' value='#{anotherBean.getBeanProperty()}'/> 

但在這種情況下,我需要新豆自身與其它bean方法(School object is returned from another bean method)被設置。

這是我所嘗試的,當然這是錯誤的。

<bean id="studentWithSchool" class="com.model.Student" scope="prototype" value='#{anotherBean.getBeanProperty()}'>  
    <property name="school"> 
     <bean class="com.model.School" scope="prototype"/> 
    </property> 
</bean> 

任何解決方法。

我的英語不好對不起,任何幫助是巨大的讚賞...

來自委內瑞拉最好的問候。

回答

1

如果我的理解正確,studentWithSchool被創建並通過anotherBean中的方法返回。如果是這樣的話,你can use a factory-method

<bean id="studentWithSchool" factory-bean="anotherBean" factory-method="getBeanProperty" scope="prototype" /> 
+0

它的工作原理就像一個魅力最好的問候:) – chiperortiz 2014-12-06 12:05:18