Backend Development 9 min read

Understanding the Bean Lifecycle in Spring ApplicationContext

This article explains the Spring bean lifecycle within an ApplicationContext, detailing container‑level, factory‑level, and bean‑level post‑processors, showing how to implement and register custom processors, and providing code examples and XML configuration to illustrate the execution order and practical usage.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Understanding the Bean Lifecycle in Spring ApplicationContext

This article introduces the lifecycle of beans managed by Spring's ApplicationContext , emphasizing its practical relevance for developers who need to customize bean initialization and destruction.

The lifecycle is illustrated with a diagram that categorizes processing steps into three groups: container‑level post‑processors (green arrows, such as InstantiationAwareBeanPostProcessor ), factory‑level post‑processors (red circles, e.g., BeanFactoryPostProcessor ), and bean‑level callbacks that apply to individual beans.

It notes that the invocation order of these interfaces may vary across Spring versions, and developers can control ordering by implementing org.springframework.core.Ordered , though typically a single implementation per type suffices.

public class Student implements BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean, ApplicationContextAware {
    private String name;
    public Student(String name) { this.name = name; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("BeanFactoryAware......");
    }
    @Override public void setBeanName(String s) {
        System.out.println("BeanNameAware......");
    }
    @Override public void destroy() throws Exception {
        System.out.println("DisposableBean......");
    }
    @Override public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean......");
    }
    @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("ApplicationContextAware......");
    }
}

Custom processors are then defined:

public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    public MyBeanFactoryPostProcessor() { System.out.println("这是BeanFactoryPostProcessor实现类构造器!!"); }
    @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory bf) throws BeansException {
        System.out.println("BeanFactoryPostProcessor调用postProcessBeanFactory方法");
        // Example of modifying BeanDefinition
    }
}

public class MyBeanPostProcessor implements BeanPostProcessor {
    public MyBeanPostProcessor() { System.out.println("这是BeanPostProcessor实现类构造器!!"); }
    @Override public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
        System.out.println("BeanPostProcessor接口方法Before对属性进行更改!");
        return bean;
    }
    @Override public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
        System.out.println("BeanPostProcessor接口方法After对属性进行更改!");
        return bean;
    }
}

public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {
    public MyInstantiationAwareBeanPostProcessor() { System.out.println("这是InstantiationAwareBeanPostProcessorAdapter实现类构造器!!"); }
    @Override public Object postProcessBeforeInstantiation(Class
beanClass, String beanName) throws BeansException {
        System.out.println("InstantiationAwareBeanPostProcessor调用Before方法");
        return null;
    }
    @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("InstantiationAwareBeanPostProcessor调用Ater方法");
        return bean;
    }
    @Override public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
        System.out.println("InstantiationAwareBeanPostProcessor调用postProcessPropertyValues方法");
        return pvs;
    }
}

The App class loads the context, retrieves the Student bean, and triggers the lifecycle:

public class App {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student stu = (Student) ac.getBean("student");
        stu.setName("wangwu");
    }
}

The accompanying XML configuration registers the bean and all custom processors, ensuring they are part of the container.

Running the program prints the sequence of constructor calls and lifecycle callbacks, demonstrating how Spring invokes each processor and aware interface in order.

Key takeaways include: the importance of processor ordering, the ability to modify bean definitions before instantiation via BeanFactoryPostProcessor , using ApplicationContextAware to obtain the context, and leveraging InitializingBean for post‑property‑set logic; while many of these hooks are rarely needed in everyday code, they are essential for framework extensions such as AOP.

backendJavaspringBeanPostProcessorApplicationContextbean lifecycle
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.