Signup/Sign In

Spring XML Based Configuration

In this topic, we will learn to create a String application and configure it using the XML code. Let's create a maven project and configure it using the XML file. If you are not familiar with the maven project, then you can read our detailed article here.

The following are the files created in our project. The source code of these files is given below.





  • applicationContext.xml

And the following is our project structure after creating all the above files (java and xml).

Spring Project Structure:

Following is the structure of the project:

Spring XML Based Configuration

Now let's create the Java classes and the XML configuration file.

It is an interface that contains an abstract method doWork() which will be overridden by the implemented class.


public interface Employee {
	void doWork();


It is our bean class that will be used to perform implementations. It implements the Employee interface and implements doWork() method.


public class Accountant implements Employee{
	public void doWork() {
		System.out.println("Audit the accounts...");

This is another class that implements the Employee interface and override the doWork() method.


public class Manager implements Employee{
	public void doWork() {
		System.out.println("Manage the branch office");

It is a configuration file that reads the applicationContext file and get Bean using the getBean() method and then call method based on the retrieved object.


import org.springframework.context.ApplicationContext;

public class BankApp {

	private static ApplicationContext context;
	public static void main(String[] args) {

		context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Employee e = (Employee) context.getBean("manager");



It is an application context file that configures spring and register the bean.

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=""
	xmlns:xsi="" xmlns:p=""
	xmlns:aop="" xmlns:context=""
	xmlns:jee="" xmlns:tx=""

	<bean id="manager"


This file contains all the dependencies of this project such as spring jars, servlet jars, etc. Put these dependencies into your project to run the application.

<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">
		<!-- -->


Run the Application:

After successfully completing the project and adding the dependencies run the application and you will get the output as below.

Manage the branch office

About the author:
I am a Java developer by profession and Java content creator by passion. I have over 5 years of experience in Java development and content writing. I like writing about Java, related frameworks, Spring, Springboot, etc.