Wednesday, 20 April 2016

How to use inner beans in Spring ?



Inner bean : bean inside another bean

If a bean element is embedded to a property tag directly, then that bean is said to the Inner Bean.

They are created and used on the fly, and can not be used outside the enclosing beans.
The id and scope attributes for inner beans are of no use.

Inner bean definition as the property of bean :
<beans ...>
    <bean id="CustomerBean" class="com.genius.common.Customer">
        <property name="person">
            <bean class="com.genius.common.Person">
                <property name="name" value="shaan" />
                <property name="address" value="address1" />
                <property name="age" value="28" />
            </bean>
        </property>
    </bean>
</beans>

This inner bean also supported in constructor injection as following :
<beans ...>
    <bean id="CustomerBean" class="com.genius.common.Customer">
        <constructor-arg>
            <bean class="com.genius.common.Person">
                <property name="name" value="shaan" />
                <property name="address" value="address1" />
                <property name="age" value="28" />
            </bean>
        </constructor-arg>
    </bean>
</beans>

No comments:

Post a Comment

Note: only a member of this blog may post a comment.