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

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

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 {

}

When Spring starts

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

@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)

Prototype

Request

Session

Example

@Scope("prototype")

@Component
public class Employee {

}

Real-World Example

Consider an e-commerce application.

Beans

Instead of manually creating each object

new ProductService();

new OrderService();

Spring creates and connects all of them automatically.

Benefits of Spring Beans

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.

Last updated: 11 November 2026

Previous question: What is Spring Initializr?

Next question: What is autowiring in Spring?

More Spring Boot - Level I interview questions

All Spring Boot - Level I questions