Spring Configuration using Java (No XML)

In this tutorial, we are going to create a spring project and configure it by using the Java code. We will not use any XML code but only Java code. This project contains the following files.

  • BankApp.java

  • AppConfig.java

  • Accountant.java

  • Manager.java

  • Employee.java

  • pom.xml

And the following is a maven project structure created for the Spring application.

Spring Project-Structure:

Following is the project structure for the spring project:

spring project configuration using Java

The files created into the above project contains the following code. See the files below.


This file contains the code to create an IOC container for our application. The AnnotationConfigApplicationContext class is used to create an object for application context.

package com.studytonight.community;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class BankApp {

	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
		Manager employee = context.getBean(Manager.class);


This is a configuration file in Java which is an alternate of the applicationContext.xml file that we created for the XML-based configuration example. The @Configuration annotation indicates that this is not a simple class but a configuration class and the @ComponentScan annotation is used to indicate the component location in our spring project.

package com.studytonight.community;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

public class AppConfig {


This is a component class that is marked using @Component annotation. It implements the Employee interface and overrides its method doWork().

package com.studytonight.community;

import org.springframework.stereotype.Component;

public class Accountant implements Employee{

	public void doWork() {
		System.out.println("Auditing Accounts...");


This is another component class that is marked using the @Component annotation and implements the Employee interface.

package com.studytonight.community;
import org.springframework.stereotype.Component;
public class Manager implements Employee{
	public void doWork() {
		System.out.println("Manage the branch office");


This is an interface Employee that contains a doWork() abstract method. Each class that implements this interface will have to override the doWork() method.

package com.studytonight.community;

public interface Employee {
	void doWork();



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