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.
@Configuration - A look at Spring Annotations in Depth
| 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
| Target Element | Applied with @Configuration |
|---|---|
| Class | Yes |
| Method | No |
| Field | No |
| Constructor | No |
| Package | No |
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;
}
}
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!