In Spring Boot and the broader Spring Framework, @Configuration
and @Bean
are used to define and manage beans within the Spring container. They are closely related but serve different purposes and are used in different contexts.
@Configuration
Purpose:
@Configuration
is a class-level annotation that indicates that a class contains one or more@Bean
definitions and can be used by the Spring container as a source of bean definitions.
Usage:
@Configuration
classes are used to define and configure beans that should be managed by the Spring container.- Typically,
@Configuration
classes are used to provide a centralized place for bean definitions and application configuration.
Key Features:
- Class-Level Annotation: It is applied at the class level.
- Component Scanning: It is a specialized form of
@Component
, meaning it will be picked up by component scanning if scanning is enabled. - Configuration Classes: You can define multiple
@Bean
methods within a single@Configuration
class to configure different beans. - @Bean Methods: Each
@Bean
method in a@Configuration
class creates and configures an instance of the specified bean and is managed by the Spring container.
Example:
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
@Bean
public MyRepository myRepository() {
return new MyRepositoryImpl();
}
}
In this example, AppConfig
is a configuration class that defines two beans: myService
and myRepository
.
@Bean
Purpose:
@Bean
is a method-level annotation used to indicate that a method produces a bean to be managed by the Spring container.
Usage:
@Bean
is used inside a@Configuration
class to define individual beans and their configurations.- The method annotated with
@Bean
returns an instance of the bean that Spring should manage.
Key Features:
- Method-Level Annotation: It is applied to methods within a
@Configuration
class. - Bean Creation: Each
@Bean
method is responsible for creating and configuring an instance of the bean. - Dependency Injection: Beans defined with
@Bean
methods can depend on other beans, and Spring will manage the dependencies.
Example:
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}
In this example, the myService
method is annotated with @Bean
, meaning it will create and return an instance of MyServiceImpl
, which Spring will manage.
Comparison and Relationship
@Configuration
: Used at the class level to indicate that the class provides Spring configuration. It can contain multiple@Bean
methods and is part of the application context configuration.@Bean
: Used at the method level within a@Configuration
class to define individual beans. Each@Bean
method is a source of bean definitions and creates an instance of the bean.
Usage Scenarios
@Configuration
:- Use when you want to define a configuration class to group multiple bean definitions together.
- Useful for organizing and managing bean definitions in a modular way.
@Bean
:- Use within
@Configuration
classes to define specific beans. - Useful for creating and configuring beans that are managed by the Spring container.
Example Integration
@Configuration
public class AppConfig {
@Bean
public DataSource dataSource() {
// Configure and return the DataSource bean
return new DataSource();
}
@Bean
public MyService myService(DataSource dataSource) {
// Inject DataSource into MyService and return the bean
return new MyServiceImpl(dataSource);
}
}
In this example, dataSource
and myService
are beans defined within the AppConfig
configuration class. The myService
bean method depends on the dataSource
bean, and Spring will handle the dependency injection automatically.
Summary
@Configuration
is used to define configuration classes that contain bean definitions.@Bean
is used within@Configuration
classes to define and configure individual beans.
By using @Configuration
and @Bean
, you can efficiently manage and configure your Spring application’s beans, ensuring a clean and modular approach to configuration.