Java

Spring xml-based Configuration Setup

Spring IOC container is an important component of spring framework that manages the beans during runtime.

It reads all the beans metadata from the application configuration file that can be an XML file or a Java file.

In older versions of Spring, we can only configure in the XML file but now in the modern versions, we can do this using Java annotations or mix of both as well.

In this article, we will learn to configure a spring application by using XML code. For this, we will create a applicationContext.xml file that will contain all the beans definitions.

To know more about the IOC container, you can refer to this detailed article <editor make this link to IOC article>.

First, we will start by creating a maven project and then will add spring dependencies to the pom.xml file. We will create a java bean and the main class that will execute this spring application.

Spring dependencies:

After adding spring core and context dependencies our pom.xml file looks like this:

//pom.xml
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.linuxhint</groupId>
  <artifactId>springapp</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>springapp</name>
  <description>a simple spring app</description>
  <dependencies>
    <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>
</dependencies>
<properties>
        <spring.version>5.2.8.RELEASE</spring.version>
</properties>
</project>

Java Bean

Now, create a Java bean class. The bean class is a special java class that contains private properties and getters setters. Spring IOC manages its runtime cycle. We will discuss this in detail in the next articles.

For now, let’s create a class Employee that contains employee id, name, and getters setters. The source code of the file looks like this:

(We created this class inside the com.linuxhint.beans package to make the project more structured.)

// Empoyee.java

package com.linuxhint.beans;
public class Employee {
   
    private int id;
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }  
}

Creating Application Context

After creating the Employee bean, let’s create a configuration file to configure this bean. For example, we created an applicationContext.xml file that contains the following code:

// Applicationcontext.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 = "emp" class = "com.linuxhint.beans.Employee">
      <property name = "name" value = "Rohan"/>
   </bean>
</beans>

The <beans>tag is a parent tag that contains useful URIs. The <bean> tag is the main tag that is used to configure the Employee bean.

It requires bean id and class parameters. So, we passed the bean id and class name with full package name. The bean id is the name that will be used to read/fetch bean data.

The <property> tag is used to set the value for the name property of the Employee class.

Creating Main Class

This is the main class where we are creating the object of the ApplicationContext container and accessing the bean by using the getBean() method.

So, we created a class SpringApp that contains a main() method. Inside this, we read the applicationContext.xml file.

The string “emp” where we passed in the getBean() method is the same as we passed in the <bean> tag as a id. The getName() method is the getter method of the Employee bean class that returns the name of the employee that we set in the <property> tag.

The complete source code is below.

// SpringApp.java

package com.linuxhint;

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

import com.linuxhint.beans.Employee;

public class SpringApp {
    public static void main(String[] args) {

        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Employee hello = (Employee) ac.getBean("emp");
        String name = hello.getName();
        System.out.println("Name: "+name);
    }
}

Project Structure

After creating all the files, our application project should look like this:

Run Application

After writing all the code, let’s run the application. Right-click on the project and select the Run As -> Java Application. It will trigger the execution process and returns the output in the console tab.

Output:

About the author

Irfan Khan

I’m a software programmer having more than 5 years of development experience in Java and related technology. I love to write technical content as well and love to share the technical knowledge to make it available for all.