Searching...
Thursday, 27 June 2013

Spring Injecting Inner Beans Example

08:55

Inner beans are beans that are defined within the scope of another bean. Inner bean we can add to beans property or constructor-arg tag

See this below example (Insurance class bean is injecting to Car class)

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">


    <!-- Setter based dependency injection -->
    <!-- Here car order is the parent class and injecting the car insurance
        bean as property and doing the car insurance bean creation there itself -->
    <bean id="carOrder" class="com.vinod.test.CarOrder">

        <property name="carInsurance">
            <bean id="carInsurance" class="com.vinod.test.CarInsurance">
                <property name="name" value="Progressive" />
                <property name="policyDetails" value="Full cover" />
            </bean>
        </property>
    </bean>

</beans>    

0 comments: