12 - Injecting Inner Beans in Spring

12.1 Overview

In the previous chapters we discussed the dependency injection approaches for beans ,simple types and how to wire collections. In this chapter we will take that discussion forward to understand how to inject inner beans

Inner classes are the classes which are defined inside the scope of another class. Similarly inner beans are the beans which are defined in the scope of another bean. Spring provides a way to inject inner beans also.

12.2 Injecting Inner Beans

 As mentioned earlier that inner beans are defined in a scope of another beans which means inner beans are not shared by another beans.

Inner beans are defined like below

<bean id=”outer_bean” class=”OuterBean”>
      <property name=”innerbean”>
           <bean  class=”InnerBean”/>
      </property>
</bean>

As you can see, instead of using ref  attribute of property tag, beans are defined inside property tag. In this case an instance of InnerBean class will be created and wired in to innerbean property of OuterBean class.

We can use inner beans in constructor-arg tag as well like below

<bean id=”outer_bean” class=”OuterBean”>
       <constructor-arg>
            <bean  class=”InnerBean”/>
       </ constructor-arg>
</bean>

In this case an instance of InnerBean class will be created and  will be passed as an constructor  argument of OuterBean class.

Note: You must have noticed that we have not defined id attribute of inner class and it is not necessary but it is legal to define an id for inner beans also. But we can not call getBean() on it.

12.2.1 Write an example to demonstrate inner bean dependency injection

Solution:

  1. Create  a Student class as below
public class Student {
       private String name ;
       public String getName() {
              return name;
       }
       public void setName(String name) {
              this.name = name;
       } 
}
  1. Create Room class as below
public class Room
{
       private int roomNumber;
       private Student allotedTo;      
              public int getRoomNumber() {
              return roomNumber;
       }
       public void setRoomNumber(int roomNumber) {
              this.roomNumber = roomNumber;
       }
       public Student getAllotedTo() {
              return allotedTo;
       }
       public void setAllotedTo(Student allotedTo) {
              this.allotedTo = allotedTo;
       }
       @Override
       public String toString() {
       return "Room [roomNumber=" + roomNumber + ", allotedTo=" + allotedTo.getName()
                           + "]";
       }      
}
  1. Define below beans entry in beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd">  
    <bean id="room" class="Room">
              <property name="roomNumber" value="101" />
              <property name="allotedTo">
                     <bean class="Student">
                     <property name="name" value="joe bloggs" />
                     </bean>
              </property>
    </bean>
</beans>
  1. Create TestInnerBeanDependency class to test the example.
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestInnerBeanDependency {
       public static void main(String[] args) {
              ApplicationContext context =
                           new ClassPathXmlApplicationContext("beans.xml");
              Room room = (Room)context.getBean("room");             
              System.out.println(room);             
       }
}
  1. Run the TestInnerBeanDependency. You will see the below results

Like us on Facebook