首页 > 编程语言 > 详细

Spring——IOC容器初始化

时间:2020-02-25 23:39:17      阅读:134      评论:0      收藏:0      [点我收藏+]

一、版本

springboot-2.1.3 (spring-5.1.5)

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
  </parent>
    ...
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

技术分享图片

二、IOC容器初始化的主要代码

IOC容器初始化的核心逻辑是各种ApplicationContext的refresh()方法实现的。

    /* 使用模板方法模式定义的一个顶级抽象父类,方法具体实现延迟到子类 */
    /* org.springframework.context.support.AbstactApplicationContext */
    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();
            }
        }
    }

选取springboot默认启动的AnnotationConfigServletWebServerApplicationContext研究refresh()的实现逻辑。

三、AnnotationConfigServletWebServerApplicationContext

① 加锁

新技能get:由synchronized实现的一个细粒度锁。拥有ReentranLock.lock()的细粒度,还不用释放unlock()。

    public void refresh() throws BeansException, IllegalStateException {
        //private final Object startupShutdownMonitor;
        //构造方法中 this.startupShutdownMonitor = new Object();
        //定义的一个当前applicationContext实例的独占锁
        Object var1 = this.startupShutdownMonitor;
        synchronized(this.startupShutdownMonitor) {
         ...
        }
    }    

② this.prepareRefresh():

③ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();

④this.prepareBeanFactory(beanFactory);

⑤this.postProcessBeanFactory(beanFactory);

⑥this.invokeBeanFactoryPostProcessors(beanFactory);

⑦this.registerBeanPostProcessors(beanFactory);

⑧this.initMessageSource();

⑨this.initApplicationEventMulticaster();

⑩this.onRefresh();

?this.registerListeners();

?this.finishBeanFactoryInitialization(beanFactory);

?this.finishRefresh();

?this.destroyBeans();

?this.cancelRefresh(var9);

?this.resetCommonCaches();

Spring——IOC容器初始化

原文:https://www.cnblogs.com/wqff-biubiu/p/12364643.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!