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

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

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

spring 容器的創(chuàng)建對(duì)應(yīng) SpringApplication 中 run 中調(diào)用的 createApplicationContext 方法。這里創(chuàng)建了一個(gè) web 容器,接下就進(jìn)去 prepareContext 容器準(zhǔn)備階段:

  private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment,
      SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) {
    //為容器設(shè)置環(huán)境
    context.setEnvironment(environment);
    //這里的空實(shí)現(xiàn)留給開(kāi)發(fā)者擴(kuò)展,設(shè)置數(shù)據(jù)轉(zhuǎn)換的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 方法,這里主要把我們的啟動(dòng)類(lèi)作為 Bean 注冊(cè)到了 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;
  }

再來(lái)看下 contextLoaded 方法,這里將上下文設(shè)置到監(jiān)聽(tīng)器中,同時(shí)也把監(jiān)聽(tīng)器添加到上下文中。最后發(fā)布了一個(gè) 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));
  }

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

相關(guān)文章

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

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

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

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

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

    Java異常處理實(shí)例分析

    這篇文章主要介紹了Java異常處理,實(shí)例分析了java異常處理的常見(jiàn)用法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04
  • 最新評(píng)論