Spring中Bean的生命周期

706 阅读2分钟

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

在Spring中,一个Bean的生命周期要经过很长的一段步骤,这也是面试中的高频考点,下面就一起来看看吧。

首先整体地梳理一下Bean的生命周期:

  1. 创建Bean实例
  2. 调用Bean中的setter()方法设置属性值
  3. 检查Bean是否实现了Aware接口,若实现了,则调用对应的接口方法
  4. 若容器中有BeanPostProcessor,则调用其postProcessAfterInitialization
  5. 检查Bean是否实现了InitializingBean,若实现了,则调用其afterPropertiesSet方法
  6. 检查是否指定了Bean的init-method属性,若指定了,则调用其指定的方法
  7. 若容器中有BeanPostProcessor,则调用其postProcessorAfterInitialization
  8. 检查Bean是否实现了DisposableBean,若实现了,则调用其方法
  9. 检查是否指定了Bean的destroy-method属性,若指定了,则调用其指定的方法

一个Bean的生命周期共需要经历上述的9个过程,如图所示:

image.png

下面通过具体的程序来测试一下,首先编写一个Bean:

public class User implements ApplicationContextAware, InitializingBean, DisposableBean {

    private String name;
    private Integer age;

    public User() {
        System.out.println("1--》创建User实例");
    }

    public void setName(String name) {
        this.name = name;
        System.out.println("2--》设置User的name属性");
    }

    public void setAge(Integer age) {
        this.age = age;
        System.out.println("2--》设置User的age属性");
    }

    public void init() {
        System.out.println("6--》调用init-method属性指定的方法");
    }

    public void myDestroy() {
        System.out.println("9--》调用destroy-method属性指定的方法");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("3--》调用对应Aware接口的方法");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("5--》调用InitializingBean接口的afterPropertiesSet方法");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("8--》调用DisposableBean接口的destroy方法");
    }
}

首先这个Bean实现了ApplicationContextAware、InitialzingBean、DisposableBean,并在每个方法中输出对应的内容,然后编写一个BeanPostProcessor:

public class MyBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("7--》调用MyBeanPostProcessor的postProcessBeforeInitialization方法");
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("4--》调用MyBeanPostProcessor的postProcessAfterInitialization方法");
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }
}

最后将它们注册到容器中:

@Configuration
public class MyBeanConfig {

    @Bean(initMethod = "init", destroyMethod = "myDestroy")
    public User user() {
        User user = new User();
        user.setName("zs");
        user.setAge(30);
        return user;
    }

    @Bean
    public BeanPostProcessor beanPostProcessor() {
        return new MyBeanPostProcessor();
    }
}

运行结果:

1--》创建User实例
2--》设置User的name属性
2--》设置User的age属性
3--》调用对应Aware接口的方法
4--》调用MyBeanPostProcessor的postProcessAfterInitialization方法
5--》调用InitializingBean接口的afterPropertiesSet方法
6--》调用init-method属性指定的方法
7--》调用MyBeanPostProcessor的postProcessBeforeInitialization方法
8--》调用DisposableBean接口的destroy方法
9--》调用destroy-method属性指定的方法