Signup/Sign In

Spring Annotation and XML Based Configuration

In this topic, we will learn to create a String application and configure it using the XML and annotations 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 project structure for the spring project:

spring project with XML config and annotations

Let's create the Java classes and XML files for the project.

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 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.

import org.springframework.stereotype.Component;
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.

import org.springframework.stereotype.Component;

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


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

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

	<context:component-scan base-package=""></context:component-scan>



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.