Reading:  

Quick to the point introduction to Spring Framework


Injecting Inner Beans

It is same as the concept of Java inner classes where user can define a bean inside another bean. A <bean/> element within the <constructor-arg/> or <property/> elements is called inner bean. The characteristics of Spring inner beans:

  • Inner bean is defined inside the context of its enclosing bean.
  • Inner bean does not have an id associated with it user can provide an id.
  • To declare an inner bean whenever a bean does not need to be shared with other beans.

Syntax:

<?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="outerBean" class="...">
      <property name="target">
         <bean id="innerBean" class="..."/>
      </property>
   </bean>

</beans>
 

Example:

Java file: Student

package pack1;  
public class Student {  
private Student_Details  stud;  

    public void setName(Student stud) {
      System.out.println("Inside setName" );
      this.stud = stud;
   }

    public Student getName() {
      return stud;
   }
    public void display(){
      stud.Stud_Address();
   }
}  
 

Java file: Student_Details

package pack1;  

public class Student_Details {  
  
    public Student_Details (){
      System.out.println("Inside Student_Details  constructor" );
   }
   public void Stud_Address(){
      System.out.println("Inside Stud_Address");
    }
}  
 

Java file: ExmTest

package pack1;  
  
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
  
public class ExmTest {  
  public static void main(String[] args) {  
      ApplicationContext cont = new ClassPathXmlApplicationContext("Beans.xml");
      Student st = (Student) cont.getBean("studentBean");
      st. Stud_Address();
  }  
}  
 

The configuration file is 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="studentBean" class="pack1.Student">
      <property name="stud">
         <bean id=" Student_Details" class="pack1. Student_Details"/>
       </property>
   </bean>
</beans>
 

Compile and run the program will get the below result:

Result:

Inside Student_Details  constructor
Inside setName
Inside Stud_Address

 

Description

This tutorial covers various topics releated to Spring Framework as listed below

  • Framework Overview
  • Modules
  • Environment Setup
  • Saying Hello World with Spring
  • IoC Containers
  • Bean Definition
  • Bean Scopes 
  • Bean Life Cycle
  • Bean Post Processors
  • Bean Definition Inheritance
  • Dependency Injection
  • Injecting Inner Beans
  • Injecting Collection
  • Beans Auto-Wiring
  • Annotation Based Configuration
  • Java Based Configuration
  • Event Handling in Spring
  • Custom Events in Spring
  • JDBC Framework Overview
  • Transaction Management

 

 



Prerequisites

Prior knowledge of Java is essential

Audience

Beginners or students seeking quick introduction to Spring

Learning Objectives

This tutorial is for beginners seeking quick introduction to Spring Framework.

Author: Subject Coach
Added on: 22nd Jun 2015

You must be logged in as Student to ask a Question.

None just yet!