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;
}
}
Facing issues? Have Questions? Post them here! I am happy to answer!
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
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
- Release Date and iPhone Devices that will Support iOS 16 - iOS
- How to remove disable google search blue triangle - Google
- Bash Script to prefix files with sequential numbers in a directory - Bash
- Run Java Code Every Second - Java
- How to reduce background noise in Audio using iMovies App on Mac? - MacOS
- How to open SharePoint Online Modern SPFX links in new tab - SharePoint
- Convert Java Byte Array to String with code examples - Java
- Create SharePoint Site Collection with new Content database in existing web application - SharePoint