@Configuration - A look at Spring Annotations in Depth


Hello and welcome to the series of "Spring Annotations in Depth". In each article, we take an in-depth look at the most widely used annotation in Spring Framework and provide examples to make it easy to understand and use in your application.


Annotation First Release Version First Release Date
@Configuration Spring 3.0 November 2009


A Brief History before Spring 3.0

    Before the release of the Spring 3.0 version the configurations for Dependency Injection (DI) and Inversion of Control (IoC) were done with the help of an XML-based configuration file. It was the primary method for configuring the Spring application context.

    This configuration file was usually named as applicationContext.xml


    Example: 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="employee" class="org.code2care.Employee">
            <property name="department" ref="department"/>
        </bean>
    
        <bean id="department" class="org.code2care.Department"/>
    
    </beans>

    In the above Spring configuration XML file we have two beans, employee and department. The department bean is injected into the employee bean using the property (setter) based injection.

    You can achieve the above with pure Java code by making use of the @Configuration and @Bean annotation.

    @Configuration
    public class ApplicationConfig {
    
        @Bean
        public Employee employee(Department department) {
            return new Employee(department);
        }
    
        @Bean
        public Department department() {
            return new Department();
        }
    }

    By using the annotation @Configuration instead of XML-based configuration you get a more flexible and type-safe approach to configuring the application context.


Definition: @Configuration

    "The @Configuration annotation is a part of Core Spring Framework that is used to indicate that a Java class contains one or more bean definition methods".

    The bean definition methods are responsible for creating and configuring beans that will be managed by the IoC (Inversion of Control) container.

    Target ElementApplied with @Configuration
    ClassYes
    MethodNo
    FieldNo
    ConstructorNo
    PackageNo

    @Configuration annotation can only be applied at the class level.



Example: @Configuration with Spring + Maven



Step 1:

Add the spring-context dependency in the pom.xml file.

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>6.0.10</version>
    </dependency>
</dependencies>


Step 2:

Create Java bean class(es) to be managed managed using @Configuration class.

public class Employee {
    private String empName;
    private int empId;

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public int getEmpId() {
        return empId;
    }

    public void setEmpId(int empId) {
        this.empId = empId;
    }
}


Step 3:

Create the @Configuration class to manage the Employee bean.

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

@Configuration
public class AppConfig {

    @Bean
    public Employee employee() {
        Employee employee = new Employee();
        employee.setEmpId(100);
        employee.setEmpName("Sam Smith");
        return employee;
    }
}


Example: @Configuration with Spring Boot + Gradle


Step 1:

Add the spring-boot-starter dependency in build.gradle file.

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
}

Step 2:

Create the Bean Java Class that is to be managed using @Configuration class.

public class Employee {
    private String empName;
    private int empId;

    // Getters and setters
}

Step 3:

Create the @Configuration class with @Bean methods.

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

@Configuration
public class AppConfig {

    @Bean
    public Employee employee() {
        Employee employee = new Employee();
        employee.setEmpId(100);
        employee.setEmpName("Alan Wise");
        return employee;
    }
}

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright ยฉ Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap