While learning Spring and Spring Boot, one of the most fundamental concepts I encountered is the Spring Bean. Understanding beans is essential because almost everything in a Spring application revolves around them.
What is a Spring Bean?
A Spring Bean is an object that is created, configured, and managed by the Spring IoC (Inversion of Control) container.
In simple terms:
- Spring creates objects for you
- Manages their dependencies
- Controls their lifecycle
Beans form the core building blocks of a Spring application, and the entire application is essentially a collection of beans wired together.
How Are Beans Defined in Spring?
1️⃣ Using Stereotype Annotations
The most common way to define beans is by using stereotype annotations. When a class is annotated with one of these, Spring automatically treats it as a bean.
Common stereotype annotations:
- @Component – Generic component
- @Service – Business logic layer
- @Repository – Data access layer
- @Controller – Web controller layer
These annotations tell Spring:
"This class should be managed by the IoC container."
2️⃣ Explicit Bean Declaration Using Configuration Class
Another way to define beans is by using a configuration class.
- The class is annotated with @Configuration
- Methods inside the class define beans programmatically
This approach gives more control over how beans are created and configured, especially when customization is required.
Spring Bean Lifecycle
Every Spring bean follows a defined lifecycle managed by the IoC container.
🔁 Bean Lifecycle Stages
Bean Created
The IoC container creates the bean instance.Dependency Injection
Spring injects required dependencies using constructor, setter, or field injection.Bean Initialized
If initialization logic exists, Spring executes it after dependencies are injected.Bean is Used
The bean is now fully ready and used by the application.Bean Destroyed
When the application context shuts down, Spring destroys the bean and performs cleanup.
Bean Lifecycle Hooks
Spring provides annotations to hook into the lifecycle of a bean.
@PostConstruct
Executed after the bean is created and dependencies are injected.
Used for initialization logic.@PreDestroy
Executed just before the bean is destroyed.
Used for cleanup tasks like closing resources.
Scope of Spring Beans
Bean scope defines how many instances of a bean Spring creates.
Common Bean Scopes
singleton (default)
One instance per Spring IoC container.prototype
A new instance is created every time the bean is requested.request
One bean instance per HTTP request (web applications only).websocket
One bean instance per WebSocket lifecycle.
Understanding scope is important because it directly impacts memory usage, performance, and application behavior.
Why Beans Matter
Beans are the foundation of:
- Dependency Injection
- Loose coupling
- Clean architecture
- Testable and maintainable applications
Once the concept of beans is clear, understanding other Spring features becomes much easier.
Top comments (0)