What is Spring Inner Beans?

In the previous article, we have seen the Spring Bean Life cycle. In this article, we will look at what is Spring Inner beans.

As we have already seen in this article How to use Has-A relationship using Dependency Injection? when we have to use one bean inside the other we can use the ref attribute in the property tag. But if in the Spring framework, if there is only one property of bean, it is advisable to use the inner bean instead of using the ref attribute.

What is Spring Inner Bean?

Spring Inner Beans are those beans that are defined within the scope of other beans just like a HAS-A relationship. We can inject the inner bean by using both <constructor-arg> as well as Setter Method Injection.

Syntax By using Setter Method Injection

<bean id="Outer_bean" class="Outer_bean">
<property name="innerBean">
<bean class="InnerBean">
</property>
</bean>

Syntax By using Constructor Injection

<bean id="outer_bean" class="Outer_bean">
 <constructor-arg>
            <bean  class="InnerBean"/>
       </ constructor-arg>
</bean>

Example to implement Spring Inner Bean using Setter Method Injection

Create a Spring Project Go to File> New > Other > Search maven > Select Maven Project > Next > Search Filter org.apche.maven.archetypes/webapp > Next > Enter Group Id & Archetype id > Finish.

In this example, we will create a Class Lab where we will have an inner bean Teacher which one property called TeacherName, and Lab Bean has one property Lab Number.

Teacher.java

package com.codedec.di;

public class Teacher {
  private String teacherName;


  public void setTeacherName(String teacherName) {
    this.teacherName = teacherName;
  }

  @Override
  public String toString() {
    return "Teacher [teacherName=" + teacherName + "]";
  }
  
}

Lab.java

package com.codedec.di;

public class Lab {

  private String labNumber;
  private Teacher allotedTo;
  
  public void setLabNumber(String labNumber) {
    this.labNumber = labNumber;
  }
  
  public void setAllotedTo(Teacher allotedTo) {
    this.allotedTo = allotedTo;
  }
  
  public void show() {
    System.out.println("Lab Number :: "+labNumber+" \nLab Alloted to :: "+allotedTo);
  }
  
}
  • Create a beans.xml file where we will create the instance of the inner bean class inside the inner bean property of Outerbean class.

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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="l1" class="com.codedec.di.Lab">
    <property name="labNumber" value="LB_105"></property>
    <property name="allotedTo">
      <bean class="com.codedec.di.Teacher">
        <property name="teacherName" value="Nicolas"></property>
      </bean>

    </property>
  </bean>
</beans>
  • Create an App.java and create the object of the Application context, get the bean from it, and call the show() method.

App.java

package com.codedec.di;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

  public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
     Lab lab= context.getBean("l1",Lab.class);
     lab.show();
  }
}

Output:

Lab Number :: LB_105 
Lab Alloted to :: Teacher [teacherName=Nicolas]

Thus, In this way, we can Implement Spring Inner beans using Setter Method Injection.

Example to implement Spring Inner Bean using Constructor Injection

Create a Spring Project Go to File> New > Other > Search maven > Select Maven Project > Next > Search Filter org.apche.maven.archetypes/webapp > Next > Enter Group Id & Archetype id > Finish.

Teacher.java

package com.codedec.di;

public class Teacher {
  private String teacherName;

  public Teacher(String teacherName) {
    this.teacherName = teacherName;
  }

  @Override
  public String toString() {
    return "Teacher [teacherName=" + teacherName + "]";
  }

}

Lab.java

package com.codedec.di;

public class Lab {

  private String labNumber;
  private Teacher allotedTo;

  public Lab(String labNumber, Teacher allotedTo) {
    this.labNumber = labNumber;
    this.allotedTo = allotedTo;
  }

  public void show() {
    System.out.println("Lab Number :: " + labNumber + " \nLab Alloted to :: " + allotedTo);
  }

}
  • Create a beans.xml file where we will create the instance of the inner bean class inside the constructor argument of OuterBean class.

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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="l1" class="com.codedec.di.Lab">
    <constructor-arg name="labNumber" value="Lab_105"></constructor-arg>
    <constructor-arg name="allotedTo">
      <bean class="com.codedec.di.Teacher">
        <constructor-arg name="teacherName" value="Nicolas"></constructor-arg>
      </bean>
    </constructor-arg>
  </bean>
</beans>
  • Create an App.java and create the object of the Application context, get the bean from it, and call the show() method.

App.java

package com.codedec.di;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

  public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
     Lab lab= context.getBean("l1",Lab.class);
     lab.show();
  }
}

Output:

Lab Number :: Lab_105 
Lab Alloted to :: Teacher [teacherName=Nicolas]

Thus, In this way, we can Implement Spring Inner beans using Constructor Injection.

Hence, In this article, we have covered Spring Inner Bean using both Constructor and Setter method Injection.