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.

  • Employee.java

  • Accountant.java

  • Manager.java

  • BankApp.java

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

package com.studytonight.community;

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

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.

package com.studytonight.community;

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.

package com.studytonight.community;
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.

package com.studytonight.community;
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="http://www.springframework.org/schema/beans"

	<context:component-scan base-package="com.studytonight.community"></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="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">
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->


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