Spring的生命周期

在讲springBean的生命周期之前首先来看一下Web容器启动的时候,是如何去开启Sping的整个流程。

  1. 随着容器启动执行Listener的 contextInitialized() 方法。
  2. 通过 createWebApplicationContext 创建一个 XmlWebApplicationContext 对象。
  3. 组装一些上下文信息后执行 refresh() 方法,进行整个spring流程的执行。

refresh() 方法中,主要进行的操作有如下几步:

  1. 完成维护整个SpringBean关系的BeanFactory的创建。
  2. 实例化 BeanFactoryPostProcessors 并执行invokeBeanFactoryPostProcessors()方法
  3. 实例化 BeanPostProcessors 对象
  4. 执行 finishBeanFactoryInitialization() 完成整个Spring容器中非懒加载并且单例的对象创建。
synchronized (this.startupShutdownMonitor) {
            // Prepare this context for refreshing.
            prepareRefresh();

            // Tell the subclass to refresh the internal bean factory.
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

            // Prepare the bean factory for use in this context.
            prepareBeanFactory(beanFactory);

            try {
                // Allows post-processing of the bean factory in context subclasses.
                postProcessBeanFactory(beanFactory);

                // Invoke factory processors registered as beans in the context.
                invokeBeanFactoryPostProcessors(beanFactory);

                // Register bean processors that intercept bean creation.
                registerBeanPostProcessors(beanFactory);

                // Initialize message source for this context.
                initMessageSource();

                // Initialize event multicaster for this context.
                initApplicationEventMulticaster();

                // Initialize other special beans in specific context subclasses.
                onRefresh();

                // Check for listener beans and register them.
                registerListeners();

                // Instantiate all remaining (non-lazy-init) singletons.
                finishBeanFactoryInitialization(beanFactory);

                // Last step: publish corresponding event.
                finishRefresh();
            }

            catch (BeansException ex) {
                // Destroy already created singletons to avoid dangling resources.
                destroyBeans();

                // Reset 'active' flag.
                cancelRefresh(ex);

                // Propagate exception to caller.
                throw ex;
            }
        }

前三个方法都属于bean的组装和环境准备工作。通过对xml和注解的处理把bean的关系进行组装起来。

  1. obtainFreshBeanFactory() 方法通过xml配置,和解析命名标签 完成BeanFactory对Bean关系的维护。
  2. prepareBeanFactory() 进行一些配置(此次忽略)
  3. postProcessBeanFactory() 进行添加一些没有在过程1中加载的BeanPostProcessor对象。

SpringBean生命周期里的方法

在上面的这些顺序步骤之后,我们已经了解了SpringBean是如何维护在Spring容器中的,接下来就是整个Bean的生命周期过程中经历的方法。在这个过程中会有很多扩展接口帮助我们对Bean进行操作。这里一一陈列图中生命周期涉及的18个过程

  1. 实例化BeanFactoryPostProcessor实现类
  2. 执行BeanFactoryPostProcessor的

invokeBeanFactoryPostProcessors()

在这个方法中完成BeanDefinitionRegistryPostProcessorBeanFactoryPostProcessor的创建并且执行BeanDefinitionRegistryPostProcessorpostProcessBeanDefinitionRegistry() 方法和BeanFactoryPostProcessorpostProcessBeanFactory() 方法。

  • . postProcessBeanDefinitionRegistry() :提供向容器中自定义注册bean的功能。
  • . postProcessBeanFactory() :可以修改beanFactory中bean的属性。

  1. 实例化BeanPostProcessor实现类
  2. 实例化InstantiationAwareBeanPostProcessor对象

BeanPostProcessors就像是Bean创建流水线上的各种打标器,在实例化Bean的过程中都会经过若干个BeanPostProcessors。

registerBeanPostProcessors(beanFactory);

这俩步可以归结为一类就是实例化BeanPostProcessor,调用 beanFactory.getBean(ppName, BeanPostProcessor.class) 方法得到化BeanPostProcessor对象并且放进BeanFactory维护的BeanPostProcessor的集合中,为了后期实例化其他对象使用。


  1. 执行InstantiationAwareBeanPostProcessor的postProcessBeforeInstantiation方法
// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
Object bean = resolveBeforeInstantiation(beanName, mbd);

这里提供了一个方法,进行容器中是否有BeanPostProcessor的判断,如果有的话那么就执行 InstantiationAwareBeanPostProcessorpostProcessBeforeInstantiation()方法。

这个注释上说是給BeanPostProcessors一个机会返回代理proxy对象。但是是根据targetSource来创建代理对象


  1. 执行Bean的构造方法

如果在上一步没有生成代理对象,那么就很进行正常的bean的实例化过程,开始执行Bean的构造方法。

通过获取构造器然后通过反射的方式来进行Bean的实例化。

  1. 执行InstantiationAwareBeanPostProcessor的postProcessPropertyValues方法

这个方法是在进行属性注入前的一个补漏工作,因为我们在最开始维护Bean关系的时候,没有处理过通过注解完成的属性注入关系,所以要通过这个方法找到例如我们通过@Resource来进行属性注入的Bean,然后把需要注入的属性和Bean之间关联起来。


  1. 为Bean注入属性

通过xml配置执行InstantiationAwareBeanPostProcessor的postProcessPropertyValues方法,已经完全组装好了一个Bean以及Bean的所有属性关系。接下里就是进行属性注入,根据注入属性的类型分别通过反射的方式来创建对象。


  1. 调用BeanNameAware的SetBeanName方法
  2. 调用BeanFactoryAware的setBeanFactory方法

这俩点都属于一个回调方法,让bean来感知自己在容器中的存在的编号,以及感知自己所属的是哪个beanFactory。通过这俩个方法我们可以操作已经在beanFactory中实例化的对象。

这里有一个彩蛋:如果实现了BeanFactoryAware接口的Bean调用setBeanFactory()方法时,进行了beanFactory.getBean(),通过一个提前处理的方式来解决循环调用。


  1. 执行BeanPostProcessor的postProcessBeforeInitialization方法

用来返回属性已经填充完全的bean或者是bean的包装类。


  1. 执行InitializingBean的afterPropertiesSet()方法
  2. 执行init-method方法

执行初始化方法。


  1. 执行BeanPostProcessor的postProcessAfterInitialization方法

这里可以生成代理对象,通过 AbstractAutoProxyCreator 类的
postProcessAfterInitialization方法

Create proxy if we have advice.


  1. 适配destroyMethod和disposableBean

创建了 DisposableBeanAdapter 对象放入disposableBeans 适配了2种不同的销毁方式提供了一个统一的destory的入口。


  1. 执行业务方法

启动web容器,可以提供业务处理。


  1. 销毁bean,执行销毁方法

当容器停止的时候,调用 ContextLoaderListenercontextDestroyed方法。执行destory方法


总结

初始化applicationContext,完成维护了整个容器的bean关系的beanFactory创建,实例化BeanFactoryPostProcessor,完成自定义bean的注册和bean属性的修改。实例化BeanPostProcessor对象,为后续bean创建过程中提供多个扩展点,如针对目标bena创建代理对象。执行InstantiationAwareBeanPostProcessor的postProcessBeforeInstantiation方法,开始bean的构造,执行InstantiationAwareBeanPostProcessor的postProcessPropertyValues方法解决注解方式的属性依赖。完成所有对象的属性注入,依次执行提供针对bean和beanFactory的感知的多个Aware方法。执行BeanPostProcessor的postProcessBeforeInitialization方法,执行afterPropertiesSet()方法和执行init-method方法来做一些初始化操作,执行BeanPostProcessor的postProcessAfterInitialization方法对有advice增强的对象创建代理对象。创建DisposableBeanAdapter适配destroyMethod和disposableBean,等待容器结束时调用destory方法。进行业务处理,关闭容器,执行destory()方法。

你可能感兴趣的:(Spring的生命周期)