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. 如果是这种情况,您可以使用工厂方法

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

我相信您正在尝试对 Spring 使用工厂模式。为此,您可以使用 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 回答