简体   繁体   中英

Java Spring IOC bean creation value

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 i what i try 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> 

Any workaround.

Sorry by my poor english, any help is hugely appreciate...

Best regards from Venezuela.

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" />

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM