SpringIOC初始化流程

SpringIOC初始化流程

在spring初始化时就会注册7个后置处理器分别如下:

AbstractApplicationContext构造器中会初始化AnnotatedBeanDefinitionReader AnnotatedBeanDefinitionReader构造器中初始化以下后置处理器

以下6个后置处理器除了ConfigurationClassPostProcessor 是实现了BeanFactoryPostProccessorBeanDefinationRegistryPostProcessor 其余全是普通的 BeanPostProccessor

ConfigurationClassPostProcessor

  1. 这个类是解析添加了@Configuration 注解的类,并且实现了 BeanFactoryPostProccessorBeanDefinationRegistryPostProcessor 在springioc初始化过程有有至关重要的作用

  2. AutowiredAnnotationBeanPostProcessor

  3. RequiredAnnotationBeanPostProcessor

  4. CommonAnnotationBeanPostProcessor

  5. PersistenceAnnotationBeanPostProcessor

  6. EventListenerMethodProcessor

  7. DefaultEventListenerFactory

一切的一切都在下边这个方法中定义了整个初始化过程

AbstractApplicationContext.refresh()

public void refresh() throws BeansException, IllegalStateException {
    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) {
            if (logger.isWarnEnabled()) {
                logger.warn("Exception encountered during context initialization - " +
                            "cancelling refresh attempt: " + ex);
            }

            // Destroy already created singletons to avoid dangling resources.
            destroyBeans();

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

            // Propagate exception to caller.
            throw ex;
        }

        finally {
            // Reset common introspection caches in Spring's core, since we
            // might not ever need metadata for singleton beans anymore...
            resetCommonCaches();
        }
    }
}
  1. prepareRefresh()

    预刷新动作,主要是对一列成员属性的初始化动作

  2. prepareBeanFactory(beanFactory)
    ClassPathXmlApplictionContext在此方法完成xml解析及相关组件注册到Spring容器中

  3. postProcessBeanFactory(beanFactory);

    空方法以便后续扩展

  4. invokeBeanFactoryPostProcessors(beanFactory);
    完成BeanDefinitionRegistryPostProcessor 以及 BeanFactoryPostProcessor 接口调用
    接口调用顺序
    首先调用BeanDefinitionRegistryPostProcessor并进行排序排序规则如下

    1. 实现PrirotyOrder接口
    2. 实现了Order接口
    3. 普通的接口nonOrdered调用
      再次调用BeanFactoryPostProcessor接口调用,规则同上
  5. registerBeanPostProcessors(beanFactory);
    完成注册BeanPostProcessor注册及实例化

  6. initMessageSource()
    初始化国际化相关东西需重点关注

  7. initApplicationEventMulticaster()
    初始化事件广播器,首先获取beanNameapplicationEventMulticaster 如果容器中没有
    默认使用SimpleApplicationEventMulticaster

  8. onRefresh()
    空方法给以便后续扩展
    SpringBoot 内嵌Tomcat在此方法完成方法调用

  9. registerListeners()
    注册Spring时间监听器 实现了次ApplicationListener的所有类

  10. finishBeanFactoryInitialization()
    完成普通Bean的实例化

  11. finishRefresh(beanFactory)
    IOC容器刷新完成并发布 ContextRefreshedEvent 容器刷新完成事件

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值