Spring Beans and Beans Life Cycles-tutorial
The Spring Framework is built on top of the powerful idea that the framework not you should manage the objects in your application.
These managed objects are known as Spring Beans, and understanding how they are created, configured, and destroyed is essential for mastering Spring development.
Declaring a Spring Bean (Most Common Way)
2. Prototype
3. Request
4. Session
5. Application
2. Dependency Injection
3. Initialization Phase
Using InitializingBean interface
Using custom init method
4. Bean is Ready for Use
Using DisposableBean
Custom destroy method
What is a Spring Bean?
A Spring Bean is any Java object that the Spring IoC Container creates and manages.
Spring is responsible for:
- Creating the object
- Injecting its dependencies
- Managing its lifecycle
- Destroying it when the application ends
Declaring a Spring Bean (Most Common Way)
@Componentpublic class MyService {}Classes marked with:
- @Component
- @Service
- @Repository
- @Controller
are automatically detected and created as beans.
How Spring Creates Beans
Spring uses component scanning to locate classes and create beans.
Example configuration
@Configuration@ComponentScan("com.example")public class AppConfig {}When your application starts:
- Spring scans the package
- Finds component classes
- Creates bean instances
- Injects dependencies
- Stores the beans inside the IoC container
This automation makes Spring applications clean and scalable.
Bean Scopes - How Many Instances Does Spring Create?
A Bean’s scope defines how often a new instance is created.
1. Singleton (Default)
One instance for the entire application.
@Scope("singleton")public class MyBean {}2. Prototype
Creates a new instance every time it is requested.
@Scope("prototype")public class MyBean {}3. Request
One instance per HTTP request (Web apps).
@Scope("request")4. Session
One instance per user session.
@Scope("session")5. Application
One instance per servlet context.
Understanding the Spring Bean Lifecycle
Spring manages a bean from the moment it is created until the moment it is destroyed.
Here’s the entire process step-by-step:
1. Bean Instantiation
1. Bean Instantiation
Spring first creates an instance of your class (calls the constructor).
public MyBean() { System.out.println("Constructor called!");}2. Dependency Injection
Next, Spring injects dependencies using:
- Constructor Injection
- Setter Injection
- Field Injection
Example
@Autowiredprivate MyRepository repo;3. Initialization Phase
After DI, Spring initializes the bean using one of several mechanisms.
Using @PostConstruct
@PostConstructpublic void init() { System.out.println("Bean Initialized");}Using InitializingBean interface
public class MyBean implements InitializingBean { @Override public void afterPropertiesSet() { System.out.println("Init using afterPropertiesSet()"); }}Using custom init method
@Bean(initMethod = "start")public MyBean myBean() { return new MyBean();}4. Bean is Ready for Use
At this point:
- The object is constructed
- Dependencies are injected
- Initialization is complete
Your application can now safely use the bean.
5. Destruction Phase
5. Destruction Phase
When the application is shutting down, Spring allows the bean to clean up resources.
Using @PreDestroy
@PreDestroypublic void cleanup() { System.out.println("Bean Destroyed!");}Using DisposableBean
public class MyBean implements DisposableBean { @Override public void destroy() { System.out.println("DisposableBean.destroy()"); }}Custom destroy method
@Bean(destroyMethod = "stop")Complete Bean Lifecycle Summary
The full lifecycle looks like this:
- Bean Instantiated
- Dependencies Injected
- Initialization (@PostConstruct, init methods, etc.)
- Bean Ready
- Destruction (@PreDestroy, destroy methods, etc.)