Spring Inner Bean

In this article, we will quickly walk through a simple example demonstrating a usage of Spring’s inner bean. Generally if you have seen the earlier examples, dependency are resolved using dependency injection using ‘ref’ attribute

But in this example, we will see how to wire the dependent bean directly into the property tag under bean element

There are cases, where beans are required for one particular property. At times, instead of declaring it has a separate bean in the bean definition we can directly wire it as inner beans

Note: Although, you can leverage this feature one should avoid having a separate bean and refer using ‘ref’ attribute.

Quick to the illustration for Inner Bean concept

 

Technology Used

  • Java 1.7
  • Eclipse Kepler IDE
  • Maven 3.0.4
  • Spring-4.0.0-RELEASE

Mavenize or download required jars

Add Spring-4.0.0 dependencies to the pom.xml

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-core</artifactId>
	<version>${spring.version}</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>${spring.version}</version>
</dependency>

Folks who aren’t familiar with Maven concepts or don’t require maven for their project, can download the below jars individually from the spring site and include them in the classpath

  • spring-core-4.0.0-RELEASE
  • spring-context-4.0.0-RELEASE
  • spring-beans-4.0.0-RELEASE
  • spring-aop-4.0.0-RELEASE
  • spring-expression-4.0.0-RELEASE
  • commons-logging-1.1.1
  • aopalliance-1.0

Let’s see coding in action

 

Create Employee & Address Class

Address class

Address bean with two simple string properties with their getter/setter

Address.java

package com.spring.series.inner.bean;

public class Address {

	// instance variable
	private String city;
	private String state;    

	/**
	 * getter and setter
	 */
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}

	@Override
	public String toString() {
		return "Address {"
				+ "city : " + "\"" + city + "\""
				+ "\tstate : " + "\"" + state + "\""
				+ "}";
	}
}

Employee class

Employee bean with one reference to address bean and its getter/setter

Employee.java

package com.spring.series.inner.bean;

public class Employee {

	// reference variable
	private Address address;

	/**
	 * getter and setter
	 */
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}

	@Override
	public String toString() {
		return "Employee {"
				+ "employee : " + address
				+ "}";
	}
}

Create Spring Bean Configuration file (Spring XML)

Employee bean defined with it dependent property ‘address’ declared (wired) directly inside the bean as a property

Note: Having attributes ‘id’ or ‘name’ makes no sense when declared inside the bean as property

SpringContext.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" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	<bean id="employee" class="com.spring.series.inner.bean.Employee">
		<property name="address">
			<bean id="address" class="com.spring.series.inner.bean.Address">
				<property name="city" value="Columbus" />
				<property name="state" value="OHIO" />
			</bean>
		</property>
	</bean>

</beans>

Note: Name of the Spring Bean Configuration file can be anything (not necessary to have SpringContext.xml) and it’s your choice. But, in the enterprise application keep these file names appropriate to the business context. So that it will increase the readability of the application

Project Structure in Eclipse (Package Explorer view)

SpringInnerBean

Test the Application that’s exactly …. Run it!

Let’s test using ApplicationContext

Simple test class. Self-explanatory !!

TestEmployee.java

package com.spring.series.inner.bean;

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

public class TestEmployee {

	public static void main(String[] args) {
		testInnerBean();
	}

	private static void testInnerBean(){

		// loading spring context xml from classpath
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/spring/series/inner/bean/SpringContext.xml");

		// getting bean from XML & cast it
		Employee employee = (Employee) applicationContext.getBean("employee");

		// printing the values
		System.out.println(employee);
	}
}

Output in console

Aug 03, 2014 6:13:17 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@31681629: startup date [Sun Aug 03 18:13:17 IST 2014]; root of context hierarchy
Aug 03, 2014 6:13:17 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/spring/series/inner/bean/SpringContext.xml]

Employee {employee : Address {city : "Columbus"	state : "OHIO"}}

Download project

Spring Inner Bean (3kB)

Happy Coding !!
Happy Learning !!

Spring - Loading multiple configuration XML files
Spring Bean Lifecycle