欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot 創(chuàng)建容器的實現(xiàn)

 更新時間:2020年10月13日 08:30:04   作者:jiao個朋友  
這篇文章主要介紹了SpringBoot 創(chuàng)建容器的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

spring 容器的創(chuàng)建對應 SpringApplication 中 run 中調用的 createApplicationContext 方法。這里創(chuàng)建了一個 web 容器,接下就進去 prepareContext 容器準備階段:

  private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment,
      SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) {
    //為容器設置環(huán)境
    context.setEnvironment(environment);
    //這里的空實現(xiàn)留給開發(fā)者擴展,設置數(shù)據(jù)轉換的ConversionService
    postProcessApplicationContext(context);
    //執(zhí)行容器中的 Initializers 的 initialize 方法
    applyInitializers(context);
    listeners.contextPrepared(context);
    if (this.logStartupInfo) {
      logStartupInfo(context.getParent() == null);
      logStartupProfileInfo(context);
    }
    // Add boot specific singleton beans
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
    if (printedBanner != null) {
      beanFactory.registerSingleton("springBootBanner", printedBanner);
    }
    if (beanFactory instanceof DefaultListableBeanFactory) {
      ((DefaultListableBeanFactory) beanFactory)
          .setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
    }
    if (this.lazyInitialization) {
      context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor());
    }
    // Load the sources
    Set<Object> sources = getAllSources();
    Assert.notEmpty(sources, "Sources must not be empty");
    load(context, sources.toArray(new Object[0]));
    listeners.contextLoaded(context);
  }

看一下這里的 load 方法,這里主要把我們的啟動類作為 Bean 注冊到了 Spring 的容器中。

  protected void load(ApplicationContext context, Object[] sources) {
    if (logger.isDebugEnabled()) {
      logger.debug("Loading source " + StringUtils.arrayToCommaDelimitedString(sources));
    }
    BeanDefinitionLoader loader = createBeanDefinitionLoader(getBeanDefinitionRegistry(context), sources);
    if (this.beanNameGenerator != null) {
      loader.setBeanNameGenerator(this.beanNameGenerator);
    }
    if (this.resourceLoader != null) {
      loader.setResourceLoader(this.resourceLoader);
    }
    if (this.environment != null) {
      loader.setEnvironment(this.environment);
    }
    loader.load();
  }

  /**
   * Load the sources into the reader.
   * @return the number of loaded beans
   */
  int load() {
    int count = 0;
    for (Object source : this.sources) {
      count += load(source);
    }
    return count;
  }

  private int load(Object source) {
    Assert.notNull(source, "Source must not be null");
    if (source instanceof Class<?>) {
      return load((Class<?>) source);
    }
    if (source instanceof Resource) {
      return load((Resource) source);
    }
    if (source instanceof Package) {
      return load((Package) source);
    }
    if (source instanceof CharSequence) {
      return load((CharSequence) source);
    }
    throw new IllegalArgumentException("Invalid source type " + source.getClass());
  }

  private int load(Class<?> source) {
    if (isGroovyPresent() && GroovyBeanDefinitionSource.class.isAssignableFrom(source)) {
      // Any GroovyLoaders added in beans{} DSL can contribute beans here
      GroovyBeanDefinitionSource loader = BeanUtils.instantiateClass(source, GroovyBeanDefinitionSource.class);
      load(loader);
    }
    if (isEligible(source)) {
      this.annotatedReader.register(source);
      return 1;
    }
    return 0;
  }

再來看下 contextLoaded 方法,這里將上下文設置到監(jiān)聽器中,同時也把監(jiān)聽器添加到上下文中。最后發(fā)布了一個 ApplicationPreparedEvent 事件。

  public void contextLoaded(ConfigurableApplicationContext context) {
    for (ApplicationListener<?> listener : this.application.getListeners()) {
      if (listener instanceof ApplicationContextAware) {
        ((ApplicationContextAware) listener).setApplicationContext(context);
      }
      context.addApplicationListener(listener);
    }
    this.initialMulticaster.multicastEvent(new ApplicationPreparedEvent(this.application, this.args, context));
  }

到此這篇關于SpringBoot 創(chuàng)建容器的實現(xiàn)的文章就介紹到這了,更多相關SpringBoot 創(chuàng)建容器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • maven 環(huán)境變量的配置詳解

    maven 環(huán)境變量的配置詳解

    這篇文章主要介紹了maven 環(huán)境變量的配置詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • Idea如何導入一個SpringBoot項目的方法(圖文教程)

    Idea如何導入一個SpringBoot項目的方法(圖文教程)

    這篇文章主要介紹了Idea如何導入一個SpringBoot項目的方法(圖文教程),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • Java內部類和匿名內部類的用法說明

    Java內部類和匿名內部類的用法說明

    這篇文章主要介紹了Java內部類和匿名內部類的用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • java實現(xiàn)Rabbitmq延遲隊列和惰性隊列

    java實現(xiàn)Rabbitmq延遲隊列和惰性隊列

    本文主要介紹了java實現(xiàn)Rabbitmq延遲隊列和惰性隊列,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-12-12
  • Java經(jīng)典用法總結(二)

    Java經(jīng)典用法總結(二)

    這篇文章主要介紹了Java經(jīng)典用法總結,在本文中,盡量收集一些java最常用的習慣用法,特別是很難猜到的用法,本文重點講解了java應用和輸入輸出常用方法,感興趣的小伙伴們可以參考一下
    2016-02-02
  • Java獲取時間如何將當前時間減一天、一月、一年、并格式化

    Java獲取時間如何將當前時間減一天、一月、一年、并格式化

    這篇文章主要介紹了Java獲取時間,將當前時間減一天、一月、一年,并加以格式化,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-09-09
  • java Collections 排序--多條件排序實例

    java Collections 排序--多條件排序實例

    這篇文章主要介紹了java Collections 排序--多條件排序實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • Spring Boot 配置和使用多線程池的實現(xiàn)

    Spring Boot 配置和使用多線程池的實現(xiàn)

    這篇文章主要介紹了Spring Boot 配置和使用多線程池的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • java使用xstream實現(xiàn)xml文件和對象之間的相互轉換

    java使用xstream實現(xiàn)xml文件和對象之間的相互轉換

    xml是一個用途比較廣泛的文件類型,在java里也自帶解析xml的包,但是本文使用的是xstream來實現(xiàn)xml和對象之間的相互轉換,xstream是一個第三方開源框架,使用起來比較方便,對java?xml和對象轉換相關知識感興趣的朋友一起看看吧
    2023-09-09
  • Java異常處理實例分析

    Java異常處理實例分析

    這篇文章主要介紹了Java異常處理,實例分析了java異常處理的常見用法,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04

最新評論