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.
1. Add the required dependencies
2. Create the POJO Class
3. Create a XML Configuration for your POJO
4. Instantiate the Spring IoC Container
5. Get Bean Instance from the IoC Container
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>