Spring Inner Bean

A <bean/> element inside the <property/> or <constructor-arg/> elements is used to define a so-called inner bean. An inner bean definition does not need to have any id or name defined, and it is best not to even specify any id or name value because the id or name value simply will be ignored by the container.

Inject Inner Bean inside <property/> element

<bean id="employee" class="com.kruders.bean.Employee">
    <property name="employee">
        <bean class="com.kruders.bean.Person">
	    <property name="name" value="Puneet" />
	    <property name="city" value="Delhi" />
	</bean>
    </property>
</bean>

Inject Inner Bean inside <constructor-arg/> element

<bean id="employee" class="com.kruders.bean.Employee">
    <constructor-arg>
        <bean class="com.kruders.bean.Person">
	    <property name="name" value="Puneet" />
	    <property name="city" value="Delhi" />
	</bean>
    </constructor-arg>
</bean>


Following example demonstrates Spring Inner Bean:

First create a new Java Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring</artifactId>
        <version>2.5.6</version>
    </dependency>

</dependencies>

1. POJO

Now create Person Class as following.

package com.kruders.bean;

public class Person {
	private String name;
	private String city;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
}

Now create Employee Class as following

package com.kruders.bean;

public class Employee {
	private Person person;
	
	public Employee() {
	}
	
	public Employee(Person person) {
		this.person = person;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}
}

2. Configuration

Create Spring-Bean.xml and write the following code

<?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.xsd">

	<bean id="employee" class="com.kruders.bean.Employee">
	   <property name="employee">
		<bean class="com.kruders.bean.Person">
			<property name="name" value="Puneet" />
			<property name="city" value="Delhi" />
		</bean>
	   </property>
	</bean>
</beans>

3. Run Program

Create Main.java class that displays the employee details and run it as Java Application

package com.kruders.core;

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

import com.kruders.bean.Employee;

public class Main {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Beans.xml");
		
		Employee employee = (Employee) context.getBean("employee");

		System.out.println("Name " + employee.getPerson().getName());
		System.out.println("City " + employee.getPerson().getCity());
	}

}

When you run the above example you’ll get an output like:

Name Puneet
City Delhi

The folder structure of the example is shown below in Figure 33.1

Figure 33.1 Figure 33.1

You can download the source code of this example here.


No comments yet.

Leave a Reply