0

I need a bean like this

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

This is OK.

My problem is I have the student returning from a method from a different bean.

I usually load the bean like this when is a property.

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

But in this case I need the new bean itself being set from the other bean method (School object is returned from another bean method).

This is what I try, and of course this is wrong:

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

Is there any workaround?

halfer
  • 19,824
  • 17
  • 99
  • 186
chiperortiz
  • 4,751
  • 9
  • 45
  • 79

2 Answers2

1

If I understand you correctly, the studentWithSchool is created and returned by a method in anotherBean. If that's the case, you can use a factory-method:

<bean id="studentWithSchool" factory-bean="anotherBean" factory-method="getBeanProperty" scope="prototype" />
Khalid
  • 2,212
  • 11
  • 12
1

I believe you are trying to use factory patter with Spring . For that you can use factory bean from spring.

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

For more detail you can use below link :-

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

Panther
  • 3,312
  • 9
  • 27
  • 50