Lesson

Spring Bean Life Cycle

spring/Spring Boot

Spring Bean Life Cycle

1. Introduction

The Spring Bean Life Cycle defines how a bean is managed inside the Spring IoC container.

It starts from:

  • Object creation

  • Dependency injection

  • Initialization

  • Ends with destruction


2. Phases of Bean Life Cycle

A Spring bean goes through the following phases:

1. Container Initialization

  • Spring container starts

  • Loads configuration (XML, annotations, Java config)

  • Registers bean definitions


2. Bean Instantiation

  • Bean object is created using:

    • Constructor

    • Factory method

  • Dependencies are not injected yet


3. Dependency Injection

  • Spring injects required dependencies

  • Injection types:

    • Constructor injection

    • Setter injection

    • Field injection


4. Initialization

  • Runs after dependency injection

  • Used for setup tasks:

    • Resource initialization

    • Validation

    • Starting connections


5. Custom Methods

  • Normal methods inside the bean

  • Called manually by developer


6. Destruction

  • Runs before bean is removed

  • Used for cleanup:

    • Closing database connections

    • Stopping threads


3. Ways to Implement Bean Life Cycle

Spring provides three ways:

  1. XML Configuration

  2. Programmatic (Interfaces)

  3. Annotations


4. Using XML Configuration

Step 1: Add Dependency

html
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.30</version>
</dependency>

Step 2: Create Bean Class

java
package beans;

public class HelloWorld {

    public void init() {
        System.out.println("Bean initialized using XML");
    }

    public void destroy() {
        System.out.println("Bean destroyed using XML");
    }
}

Step 3: Configure XML

html
<bean id="hw" class="beans.HelloWorld"
      init-method="init"
      destroy-method="destroy"/>

Step 4: Run Application

java
package test;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {

    public static void main(String[] args) {

        ConfigurableApplicationContext context =
            new ClassPathXmlApplicationContext("spring.xml");

        context.close();
    }
}

5. Using Programmatic Approach (Interfaces)

Bean Class

java
package beans;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.DisposableBean;

public class HelloWorld implements InitializingBean, DisposableBean {

    @Override
    public void afterPropertiesSet() {
        System.out.println("Bean initialized");
    }

    @Override
    public void destroy() {
        System.out.println("Bean destroyed");
    }
}

Key Points

  • afterPropertiesSet() → called after initialization

  • destroy() → called before container shutdown

  • Tight coupling with Spring (not recommended)


6. Using Annotations (Recommended)

Step 1: Add Dependency

html
<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
    <version>1.3.2</version>
</dependency>

Step 2: Bean Class

java
package beans;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class HelloWorld {

    @PostConstruct
    public void init() {
        System.out.println("Bean initialized using annotations");
    }

    @PreDestroy
    public void destroy() {
        System.out.println("Bean destroyed using annotations");
    }
}

Step 3: Enable Annotations in XML

html
<context:annotation-config/>

Step 4: Run Application

java
ConfigurableApplicationContext context =
    new ClassPathXmlApplicationContext("spring.xml");

context.close();

7. Important Notes

  • Always close the container to trigger destroy methods

  • Annotation-based approach is best for modern applications

  • Avoid interface-based approach in real projects


8. Conclusion

  • Bean Life Cycle is important for managing resources

  • XML and Interfaces are older approaches

  • Annotations are simple and widely used in Spring Boot