In this tutorial, we will examine how to design and configure POJO instance values for the Spring IoC container in an XML file. Next, instantiate the Spring IoC container to access to the POJO instance values defined in an XML file.

For the sake of simplicity, in this example we will declare a class NameGenerator which will generate full name from initial, firstname and surname.

Let us first began with creating a new eclipse Maven project.

1. Add the required dependencies

Add the following Maven depandencies to your project pom.xml file.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.javatechig</groupId>
	<artifactId>SpringBeanExample</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<org.springframework.version>4.0.2.RELEASE</org.springframework.version>
		<apache.tiles>3.0.3</apache.tiles>
		<spring.security.version>3.2.3.RELEASE</spring.security.version>
	</properties>

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

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

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

2. Create the POJO Class

Let us create a NameGenerator class that has three properties— prefix, firstName, and surName.

public class NameGenerator {
	private String firstName;
	private String surName;
	private String initial;

	public NameGenerator() {}

	public NameGenerator(String initial, String firstName, String surName) {
		this.firstName = firstName;
		this.surName = surName;
		this.initial = initial;
	}

	public String getFullName() {
		return initial + " " + firstName + " " + surName;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getSurName() {
		return surName;
	}

	public void setSurName(String surName) {
		this.surName = surName;
	}

	public String getInitial() {
		return initial;
	}

	public void setInitial(String initial) {
		this.initial = initial;
	}
}

Notice that, in the above code snippet the NameGenerator class has a method getFullName(), that is used to generate the fullname.

The NameGenerator class can be instantiated by using standard Java constructorn call. When you use the Spring IoC container to initialize POJOs, if you use the standard Java constructor the mechanism is called constructor injection, where as if you use setter methods the mechanism is called setter injection.

3. Create a XML Configuration for your POJO

To define instances of a POJO class in the Spring IoC container, you have to create an XML configuration and initialize the properties.

Create a new xml file named bean.xml under the root of the project classpath for easier testing with in an IDE. The following code snippet explains how to configure bean in Spring.

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

	<bean name="nameGenerator" class="com.javatechig.spring.bean.NameGenerator">
		<property name="initial">
			<value>Mr.</value>
		</property>
		<property name="firstName">
			<value>Steve</value>
		</property>
		<property name="surName">
			<value>Peterson</value>
		</property>
	</bean>
</beans>

Notes:

  • Spring XML configuration file can have any name, in this example we have created beans.xml.
  • Each POJO instances must have a unique name or id per context. The id will be used to to uniquly identify the beans.
  • The value for class attribute should contain a fully qualified POJO class name so the Spring IoC container can instantiate it.
  • If you want to configure the bean property via setter injection, you use the <property> element and specify the property name in its name attribute. A requires that the underlying POJO class contain a corresponding setter method.
  • The Spring bean properties can be initialized via constructor injection by declaring them in the <constructor-arg> elements.

The following example shows how to use the constructor-arg element to initialize bean via constructor injection.

<bean name="nameGenerator" class="com.javatechig.spring.bean.NameGenerator">
		<constructor-arg>
			<value>Mr.</value> 
		</constructor-arg>
		<constructor-arg>
			<value>Steve</value> 
		</constructor-arg>
		<constructor-arg>
			<value>Peterson</value> 
		</constructor-arg>
</bean>

Note that the constructor arguments are order based, so you have to be careful about the order while declaring in bean.xml file.

Although a bean’s name can be defined by the name attribute of the element, the preferred way of identifying a bean is through the standard XML id attribute. In this way, if your text editor is XML-aware, it can help to validate each bean’s uniqueness at design time.

4. Instantiate the Spring IoC Container

To create bean instances, we first need to instantiate the Spring IoC container by reading the XML configuration files (in our case beans.xml). Once IoC container is initialized, you can get the bean instances from the IoC container using bean name or id.

Spring provides two types of IoC container implementations.

  • Using bean factory (BeanFactory class)
  • Using application context (ApplicationContext interface)

The application context provides some of the advance features than the bean factory. We recommend using the application context for every application unless it is intended. The ApplicationContext is an interface, so will use GenericXmlApplicationContext class which is an implementation of ApplicationContext interface.

The following code snippet shows how to load the XML configuration file from the classpath.

ApplicationContext context = new GenericXmlApplicationContext("beans.xml");

Once the application context is instantiated, we can get the POJO instance and access the properties.

5. Get Bean Instance from the IoC Container

To get the bean instance, you need to call the getBean() method by pasing unique bean name.
Let us create a new class with a main method so that we can instantiate the bean and call the getFullName() method.

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

public class Main {
	public static void main(String[] args) {
		ApplicationContext context = new GenericXmlApplicationContext("beans.xml");
		NameGenerator generator = (NameGenerator) context.getBean("nameGenerator");
		System.out.println(generator.getFullName());
	}
}

Output:

The application will initialize the bean and print the full name on console.

This article is about: Spring Core,

Nilanchala

A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (goo.gl/qUZ0XV3), DZone MVB and founder of stacktips.com.

Discussions

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.