0

私は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 自体を他の 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> 

回避策はありますか?

4

2 に答える 2

1

あなたの理解が正しければ、studentWithSchoolが作成され、 のメソッドによって返されanotherBeanます。その場合は、 factory-method を使用できます

<bean id="studentWithSchool" factory-bean="anotherBean" factory-method="getBeanProperty" scope="prototype" />
于 2014-12-05T16:47:43.147 に答える
1

Spring でファクトリ パターンを使用しようとしていると思います。そのために、春からファクトリ Bean を使用できます。

<bean id="studentWithSchool" factory-bean="anotherBeanStaticFactory" factory-           method="createBeanProperty" scope="prototype"       
<property name="school">
    <bean class="com.model.School" scope="prototype"/>
</property>

詳細については、以下のリンクを使用できます:-

http://docs.spring.io/spring-framework/docs/2.5.6/api/org/springframework/beans/factory/BeanFactory.html

于 2014-12-05T17:32:41.560 に答える