Loading
Spring Configuration-tutorial
Spring Framework is one of the most popular Java frameworks for building modern applications.
At the heart of Spring lies one important concept Configuration.

Spring needs to know:

  • Which objects (beans) to create
  • How to inject dependencies
  • How to manage their lifecycle
  • How components talk to each other
To achieve this, Spring provides three different configuration approaches:

1. XML-Based Configuration
2. Java-Based Configuration
3. Annotation-Based Configuration

Here you will learn all three approaches with simple examples, live-coding explanations, and a final recommendation, Which one should you use.


What is Spring Configuration?

Spring needs to know which objects (beans) to create, how to manage them, and how they depend on each other.
This process is called Spring Configuration.


Why Does Spring Configuration Matter?

Before Spring can run your application, it must know:

  • Which classes should become Spring beans
  • How dependencies should be injected
  • How objects should be created and destroyed

Configuration controls the entire Inversion of Control (IoC) and Dependency Injection (DI) mechanism.


1. XML-Based Configuration (Traditional Method)

XML was the first and default way of configuring Spring applications.
It is still found in legacy enterprise systems, but rarely used for new projects.


Step 1: Add Required Dependencies (pom.xml)

// <!-- Spring Core + Context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>6.1.2</version>
</dependency>

// <!-- XML Schema parsing (optional but needed) -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>5.3.39</version>
</dependency>

// <!-- Logging (optional) -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>2.0.17</version>
</dependency>


Step 2: Create applicationContext.xml

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

    <bean id="myService" class="com.demo.services.MyService"/>
</beans>

What does <beans> tag do?

  • It is the root element of Spring's XML configuration.
  • It tells Spring: "This file contains bean definitions."
  • All Spring-managed beans must be declared inside this tag.
What does <bean> tag do?

  • It defines a single Spring bean.
  • It tells Spring:
  • id → name of the bean
  • class → class to instantiate
Example Explanation

<bean id="myService" class="com.demo.services.MyService"/>

  • Spring will automatically create an object of MyService
  • Store it in the container
  • Manage its lifecycle


Step 3: Create the Service Class

package com.demo.services;

public class MyService {
    public void doWork() {
        System.out.println("MyService is working — loaded via XML!");
    }
}


Step 4: Load XML Configuration

ClassPathXmlApplicationContext context =
        new ClassPathXmlApplicationContext("applicationContext.xml");

MyService service = context.getBean("myService", MyService.class);
service.doWork();

context.close();


2. Java-Based Configuration (Modern & Clean)

Java-based configuration is the recommended modern replacement for XML.

You simply define beans inside Java classes using annotations.


Step 1: Create AppConfig.java

@Configuration
public class AppConfig {

    @Bean
    public MyService myService() {
        return new MyService();
    }
}

Explanation:

  • @Configuration → marks this class as a configuration class
  • @Bean → registers a bean method into the Spring container


Step 2: Load Java Configuration

AnnotationConfigApplicationContext context =
        new AnnotationConfigApplicationContext(AppConfig.class);

MyService service = context.getBean(MyService.class);
service.doWork();

context.close();


3. Annotation-Based Configuration (Most Popular Today)

This is the most convenient method especially in Spring Boot.

You simply annotate the class, and Spring automatically detects and registers it.


Step 1: Add @Component

@Component
public class MyService {
    public void doWork() {
        System.out.println("MyService is working — loaded via Annotations!");
    }
}


Step 2: Enable Component Scanning

@Configuration
@ComponentScan("com.demo")
public class AppConfigAnnotation {
}


Step 3: Run the Application

AnnotationConfigApplicationContext context =
        new AnnotationConfigApplicationContext(AppConfigAnnotation.class);

MyService service = context.getBean(MyService.class);
service.doWork();

context.close();


Comparison Table

FeatureXMLJava ConfigAnnotations
Clean & readableNoYesYes
Type-safeNoYesYes
Easy to maintainNoYesSuper easy
Modern usageLowHighVery High
Used in Spring BootNoYesYes
Recommended for new projectsNoYesYes


Which Method Should You Use in 2025?

Here is the final verdict:

For new Spring or Spring Boot projects

Java Config + Annotation-Based Configuration
(Modern, clean, fast, scalable)


For enterprise legacy projects

XML (only if required by existing codebase)


For maximum clarity & control

Java Config


For fastest development

Annotation-based setup



Two Minute Drill

  • Sprng XML, Java Config and Annotaions are three ways to configure a Spring applicaton.
  • Today, XML is mostly outdated it is verbose and not type-safe, used only in legacy projects.
  • Java Config is cleaner, readable and type-safe, making it ideal for modern Spring setups.
  • Annotatoins provide the fastest, most convenient configuration style using @component, @Configuration and auto-scanning, which reduces boilerplate and increases development speed.
  • The best appraoch today is a hybrid of Java Config + Annotations, giving you both flexibility and simplicity while keeping the code base modern and maintainable.