What is a Spring Bean?

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.

Key Points: • A Spring Bean is any object managed by the Spring container. • Spring automatically creates and injects dependencies between beans. • Beans help achieve loose coupling and better maintainability. • Beans can be configured using annotations, Java configuration, or XML. • The Spring container controls the entire bean lifecycle.

Why Do We Need Spring Beans?

In a traditional Java application:

Service service =
        new Service();

Repository repository =
        new Repository();

Developers manually create and connect objects.

In Spring:

• Spring creates the objects. • Spring injects dependencies automatically. • Developers focus on business logic instead of object management.

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

Code Example:

import org.springframework.stereotype.Component;

@Component
public class EmployeeService {

}

When Spring starts:

• It scans the classpath. • Detects @Component. • Creates a bean automatically.

Specialized Bean Annotations

Spring provides stereotype annotations:

@Component

Generic Spring Bean

@Service

Business logic layer

@Repository

Data access layer

@Controller

Web controller layer

Example:

@Service
public class UserService {

}

Spring automatically registers it as a bean.

Dependency Injection Example

Code Example:

@Repository
public class EmployeeRepository {

}

@Service
public class EmployeeService {

    private final EmployeeRepository repository;

    public EmployeeService(
            EmployeeRepository repository) {

        this.repository = repository;
    }
}

Spring automatically:

1. Creates EmployeeRepository bean. 2. Creates EmployeeService bean. 3. Injects EmployeeRepository into EmployeeService.

No manual object creation is required.

Bean Lifecycle

A Spring Bean typically follows these stages:

1. Bean Creation 2. Dependency Injection 3. Initialization 4. Ready for Use 5. Destruction

Flow:

Instantiate Bean | Inject Dependencies | Initialize Bean | Use Bean | Destroy Bean

Bean Scope

Spring supports multiple bean scopes.

Singleton (Default)

• One bean instance per Spring container.

Prototype

• New instance every request.

Request

• One instance per HTTP request.

Session

• One instance per user session.

Example:

@Scope("prototype")

@Component
public class Employee {

}

Real-World Example

Consider an e-commerce application.

Beans:

• ProductService • OrderService • PaymentService • ProductRepository

Instead of manually creating each object:

new ProductService();

new OrderService();

Spring creates and connects all of them automatically.

Benefits of Spring Beans

• Loose coupling • Better testability • Automatic dependency management • Centralized object lifecycle management • Easier maintenance and scalability

Example: Suppose OrderService depends on PaymentService.

Without Spring:

PaymentService paymentService =
        new PaymentService();

OrderService orderService =
        new OrderService(
                paymentService);

With Spring:

@Service
public class PaymentService {

}

@Service
public class OrderService {

    private final PaymentService paymentService;

    public OrderService(
            PaymentService paymentService) {

        this.paymentService = paymentService;
    }
}

Spring automatically injects the dependency.

Interview Tip: 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."