A Spring Bean is an object that is created, configured, initialized, and managed by the Spring IoC (Inversion of Control) container. Instead of manually creating objects using the new keyword throughout the application, Spring manages the lifecycle of these objects and automatically handles their dependencies.
Why Do We Need Spring Beans?
Service service =
new Service();
Repository repository =
new Repository();Developers manually create and connect objects.
This reduces boilerplate code and improves maintainability.
How Spring Bean Works
Application Start
|
|
Spring Container
|
|
Creates Beans
|
|
Injects Dependencies
|
|
Application Ready
The Spring IoC Container is responsible for managing all beans.
How to Create a Spring Bean
Using @Component
import org.springframework.stereotype.Component;
@Component
public class EmployeeService {
}Specialized Bean Annotations
@Component
Generic Spring Bean
@Service
Business logic layer
@Repository
Data access layer
@Controller
Web controller layer
@Service
public class UserService {
}Spring automatically registers it as a bean.
Dependency Injection Example
@Repository
public class EmployeeRepository {
}
@Service
public class EmployeeService {
private final EmployeeRepository repository;
public EmployeeService(
EmployeeRepository repository) {
this.repository = repository;
}
}1. Creates EmployeeRepository bean.
2. Creates EmployeeService bean.
3. Injects EmployeeRepository into EmployeeService.
No manual object creation is required.
Bean Lifecycle
1. Bean Creation
2. Dependency Injection
3. Initialization
4. Ready for Use
5. Destruction
Instantiate Bean
|
Inject Dependencies
|
Initialize Bean
|
Use Bean
|
Destroy Bean
Bean Scope
Spring supports multiple bean scopes.
Singleton (Default)
Prototype
Request
Session
@Scope("prototype")
@Component
public class Employee {
}Real-World Example
Consider an e-commerce application.
new ProductService();
new OrderService();Spring creates and connects all of them automatically.
Benefits of Spring Beans
Suppose OrderService depends on PaymentService.
PaymentService paymentService =
new PaymentService();
OrderService orderService =
new OrderService(
paymentService);@Service
public class PaymentService {
}
@Service
public class OrderService {
private final PaymentService paymentService;
public OrderService(
PaymentService paymentService) {
this.paymentService = paymentService;
}
}Spring automatically injects the dependency.
A concise interview answer is
A Spring Bean is an object whose lifecycle is managed by the Spring IoC container. Spring creates, configures, and injects dependencies into beans automatically, helping developers build loosely coupled, maintainable, and scalable applications. Beans are typically defined using annotations such as @Component, @Service, @Repository, or @Controller.
Last updated: 11 November 2026
Previous question: What is Spring Initializr?
Next question: What is autowiring in Spring?