Spring Injecting Inner Beans Example

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>    

No comments:

Post a Comment

12 classic String-based Java interview questions with simple explanations and code.

  1️⃣ Check if a String is a Palindrome Problem Given a string, check if it reads the same forward and backward. Example: "madam...

Featured Posts