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!
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
- Notepad++ Happy vs Unhappy Versions - NotepadPlusPlus
- How to Sign Up for ChatGPT AI Chat Bot with Steps - HowTos
- Git Rename master branch make to main using Command - Git
- Turn off Auto-Capitalization on macOS Ventura 13 - MacOS
- Python: Convert int to binary String - Python
- [Fix] Cannot resolve No versions available for org.osgi.service:org.osgi.service.prefs:jar:[1.1.0,1.2.0) within specified range - Java
- 0xCAA20003: You ran into an authorization problem. [Microsoft] - Microsoft
- AADSTS90033: A transient error has occurred. Please try again. [Microsoft 365] - Microsoft