What is @Bean Annotation in Spring?
The @Bean annotation in Spring is used to manually create and configure Spring Beans inside a Java configuration class.
It tells the Spring IoC (Inversion of Control) container that a method produces a bean object that should be managed by Spring.
In simple terms:
- @Bean creates Spring-managed objects
- Spring stores these objects in the IoC container
- Dependencies can be injected automatically
- Bean lifecycle is managed by Spring
The @Bean annotation is widely used in:
- Spring Framework
- Spring Boot
- Enterprise Applications
- Microservices Architecture
- Cloud-Native Systems
Simple Definition
The @Bean annotation is used to define a Spring Bean manually inside a configuration class.
Why @Bean Annotation is Important
In enterprise applications:
- Some classes cannot use annotations like @Component
- Third-party library objects need Spring management
- Complex object creation logic may exist
- Custom configuration may be required
The @Bean annotation solves these problems.
Real-Time Banking Example
Consider a banking application requiring:
- Payment Service
- Notification Service
- Database Configuration
- Email Configuration
Some external library classes may not contain Spring annotations.
Using @Bean, developers can manually register them as Spring Beans.
How @Bean Works Internally
Application Starts
|
@Configuration Class Loaded
|
@Bean Methods Detected
|
Bean Objects Created
|
Beans Stored in IoC Container
|
Dependencies Injected
|
Application Ready
Basic @Bean Example
@Configuration
public class AppConfig {
@Bean
public NotificationService notificationService() {
return new NotificationService();
}
}
Here:
- Spring calls the method
- Creates NotificationService object
- Registers it as a Spring Bean
Understanding @Configuration
@Configuration marks the class as a Spring configuration class.
Spring scans this class for:
- @Bean methods
- Configuration metadata
Complete Example with Dependency Injection
public class NotificationService {
public void sendNotification() {
System.out.println("Notification Sent");
}
}
public class PaymentService {
private NotificationService notificationService;
public PaymentService(
NotificationService notificationService) {
this.notificationService =
notificationService;
}
}
@Configuration
public class AppConfig {
@Bean
public NotificationService notificationService() {
return new NotificationService();
}
@Bean
public PaymentService paymentService() {
return new PaymentService(
notificationService()
);
}
}
Spring automatically manages dependencies.
Bean Naming in @Bean Annotation
By default:
- Method name becomes bean name
Example
@Bean
public NotificationService notificationService() {
return new NotificationService();
}
Bean name:
notificationService
Custom Bean Name Example
@Bean(name = "customNotificationService")
public NotificationService notificationService() {
return new NotificationService();
}
Bean Scope with @Bean
Spring allows bean scope definition using:
@Scope
Singleton Scope Example
@Bean
@Scope("singleton")
public PaymentService paymentService() {
return new PaymentService();
}
Prototype Scope Example
@Bean
@Scope("prototype")
public ReportService reportService() {
return new ReportService();
}
Bean Lifecycle Methods with @Bean
Spring supports initialization and destruction methods.
Example
@Bean(
initMethod = "init",
destroyMethod = "cleanup"
)
public PaymentService paymentService() {
return new PaymentService();
}
How Spring Calls @Bean Methods
Spring internally:
- Executes @Bean methods
- Stores returned objects as beans
- Manages bean lifecycle
- Injects dependencies automatically
@Bean vs @Component
| Feature | @Bean | @Component |
|---|---|---|
| Usage | Manual Bean Creation | Automatic Component Scanning |
| Location | Inside @Configuration Class | Directly on Class |
| Control | More Control | Less Control |
| Third-Party Classes | Supported | Not Supported |
When to Use @Bean
- Third-party library classes
- Complex object creation logic
- Manual bean customization
- Conditional bean creation
- Custom configuration handling
When to Use @Component
- Application business classes
- Services
- Repositories
- Controllers
Advantages of @Bean Annotation
- Manual bean control
- Supports third-party classes
- Flexible configuration
- Custom initialization support
- Lifecycle management support
1. Better Control
Developers can fully control object creation logic.
2. Third-Party Library Integration
External library classes can become Spring Beans easily.
3. Flexible Configuration
Beans can be customized dynamically.
4. Lifecycle Support
Initialization and destruction methods can be configured.
Disadvantages of @Bean Annotation
- Requires manual configuration
- May increase configuration code
- Less automatic compared to component scanning
@Bean Annotation in Spring Boot
Spring Boot heavily uses @Bean for:
- Security configuration
- Database configuration
- Custom REST clients
- External integrations
- Cloud-native configurations
Spring Boot Example
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
Spring Boot manages RestTemplate as a Bean.
@Bean Annotation in Microservices
In Microservices Architecture:
- Custom clients are configured using @Bean
- Security filters use @Bean
- Gateway configurations use @Bean
- Load balancers use @Bean
Microservices Banking Example
@Bean
public WebClient webClient() {
return WebClient.builder().build();
}
WebClient becomes a reusable Spring Bean.
Common Spring Annotations Related to @Bean
- @Configuration
- @Bean
- @Scope
- @Lazy
- @Primary
- @Qualifier
Real-World Enterprise Use Cases
- Database configuration
- Security configuration
- REST client configuration
- Messaging systems
- Cloud integrations
Common Interview Questions
What is @Bean annotation in Spring?
The @Bean annotation is used to manually create and configure Spring Beans inside a configuration class.
Where is @Bean annotation used?
@Bean is used inside classes annotated with @Configuration.
What is the difference between @Bean and @Component?
@Bean is used for manual bean creation, while @Component uses automatic component scanning.
Professional Interview Answer
The @Bean annotation in Spring is used to manually define and configure Spring Beans inside a configuration class annotated with @Configuration. Spring IoC container executes @Bean methods, creates the returned objects, and manages them as Spring Beans. The @Bean annotation is especially useful for configuring third-party library classes, custom object creation logic, and advanced application configurations in enterprise and microservices architectures.
Conclusion
The @Bean annotation is one of the most powerful configuration mechanisms in Spring Framework and Spring Boot.
It provides full control over bean creation, lifecycle management, dependency injection, and application configuration.
Modern enterprise applications, banking systems, cloud-native platforms, and microservices architectures heavily rely on @Bean annotation for advanced configuration management and external integrations.
Understanding the @Bean annotation is essential for Java developers, Spring Boot developers, backend engineers, and software architects preparing for interviews and building enterprise-grade applications.