美文网首页
Spring之Bean的生命周期

Spring之Bean的生命周期

作者: 躁动的中年大叔 | 来源:发表于2018-10-20 20:55 被阅读16次

生命周期

  1. Spring对bean进行实例化;
  2. Spring将值和bean的引用注入到bean对应的属性中;
  3. 如果bean实现了BeanNameAware接口,Spring调用setBeanName()方法,并传递bean的ID;
  4. 如果bean实现了BeanFactoryAware接口,Spring调用setBeanFactory()方法,将BeanFactory容器实例传入;
  5. 如果bean实现了ApplicationContextAware接口,Spring调用setApplicationContext()方法,将bean所在应用上下文的引用传入;
  6. 如果bean实现了InitializingBean接口,Spring调用afterPropertiesSet()方法;
  7. 如果bean实现了BeanPostProcessor接口,Spring调用postProcessBeforeInitialization()方法;
  8. 如果bean实现了BeanPostProcessor接口,Spring调用postProcessAfterInitialization()方法;
  9. 此时bean已经准备就绪,可以被应用程序使用了,并且将一直存在应用上下文中,直至应用上下文被销毁;
  10. 若果bean实现了DisposableBean接口,销毁时Spring将调用它的destroy()接口方法。

注意点

《Spring实战》一书中第7步先于第6步执行。但笔者写代码测试是反过来的。

Code

测试bean实体类

public class BeanLifeCycle implements BeanNameAware, BeanFactoryAware, ApplicationContextAware,
        BeanPostProcessor, InitializingBean, DisposableBean {
    private static Logger logger = LoggerFactory.getLogger(BeanLifeCycle.class);

    private String name;

    private BeanLifeCycle(){
        logger.info("Call method 构造函数.");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "BeanLifeCycle{" +
                "name='" + name + '\'' +
                '}';
    }

    @Override
    public void setBeanName(String name) {
        logger.info("Call method setBeanName.");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        logger.info("Call method setBeanFactory.");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        logger.info("Call method setApplicationContext.");
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        logger.info("Call method postProcessBeforeInitialization.");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        logger.info("Call method postProcessAfterInitialization.");
        return bean;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        logger.info("Call method afterPropertiesSet.");
    }


    @Override
    public void destroy() throws Exception {
        logger.info("Call method destroy.");
    }
}

main函数

public class IocMain {
    private static Logger logger = LoggerFactory.getLogger(IocMain.class);

    public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring/ioc/ioc-main.xml");

        BeanLifeCycle beanLifeCycle = (BeanLifeCycle) context.getBean("beanLifeCycle");
        logger.info("{}", beanLifeCycle);
    }
}

输出

11:32:16.258 [main] INFO  ai.code.mikasa.spring.ioc.BeanLifeCycle - Call method 构造函数.
11:32:16.282 [main] INFO  ai.code.mikasa.spring.ioc.BeanLifeCycle - Call method setBeanName.
11:32:16.282 [main] INFO  ai.code.mikasa.spring.ioc.BeanLifeCycle - Call method setBeanFactory.
11:32:16.283 [main] INFO  ai.code.mikasa.spring.ioc.BeanLifeCycle - Call method setApplicationContext.
11:32:16.283 [main] INFO  ai.code.mikasa.spring.ioc.BeanLifeCycle - Call method afterPropertiesSet.
11:32:16.302 [main] INFO  ai.code.mikasa.spring.ioc.BeanLifeCycle - Call method postProcessBeforeInitialization.
11:32:16.303 [main] INFO  ai.code.mikasa.spring.ioc.BeanLifeCycle - Call method postProcessAfterInitialization.
11:32:16.314 [main] INFO  ai.code.mikasa.spring.ioc.IocMain - BeanLifeCycle{name='null'}

相关文章

网友评论

      本文标题:Spring之Bean的生命周期

      本文链接:https://www.haomeiwen.com/subject/ronmzftx.html