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!
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
- How to Download Apple Vision Pro visionOS Simulator on Xcode 15 - Apple
- Internet Explorer browser auto redirect to Microsoft Edge for compatibility with modern web sites - Microsoft
- Robinhood unexpected server error - Android
- Python: Convert int to binary String - Python
- Guide: Install Vim on Mac - vi
- Fix: nano is not recognized as an internal or external command - Windows PowerShell - Powershell
- sudo is not recognized as an internal or external command - Windows
- Android : Connection with adb was interrupted 0 attempts have been made to reconnect - Android