美文网首页
SpringBoot源码分析-005 准备容器 prepareC

SpringBoot源码分析-005 准备容器 prepareC

作者: Mattle | 来源:发表于2019-04-24 10:54 被阅读0次
<!-- 准备环境 -->
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
private void prepareContext(ConfigurableApplicationContext context,ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,ApplicationArguments applicationArguments, Banner printedBanner) {

    <!-- 这里将上下文环境set为 StandardServletEnvironment 一般 -->
    context.setEnvironment(environment);

    postProcessApplicationContext(context);
    
    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);
    }
    // Load the sources
    Set<Object> sources = getAllSources();

    Assert.notEmpty(sources, "Sources must not be empty");
    
    load(context, sources.toArray(new Object[0]));
    <!-- 广播 ApplicationPreparedEvent -->
    listeners.contextLoaded(context);
}


protected void postProcessApplicationContext(ConfigurableApplicationContext context) {
    <!-- 添加一个 BeanNameGenerator-->
    if (this.beanNameGenerator != null) {
        context.getBeanFactory().registerSingleton(
                AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR,
                this.beanNameGenerator);
    }
    <!-- 设置 ResourceLoader -->
    if (this.resourceLoader != null) {
        if (context instanceof GenericApplicationContext) {
            ((GenericApplicationContext) context)
                    .setResourceLoader(this.resourceLoader);
        }
        if (context instanceof DefaultResourceLoader) {
            ((DefaultResourceLoader) context)
                    .setClassLoader(this.resourceLoader.getClassLoader());
        }
    }

    if (this.addConversionService) {
        context.getBeanFactory().setConversionService(
                ApplicationConversionService.getSharedInstance());
    }
}

<!-- 这个方法暂时没看懂 -->
protected void applyInitializers(ConfigurableApplicationContext context) {
    for (ApplicationContextInitializer initializer : getInitializers()) {
        Class<?> requiredType = GenericTypeResolver.resolveTypeArgument(
                initializer.getClass(), ApplicationContextInitializer.class);
        Assert.isInstanceOf(requiredType, context, "Unable to call initializer.");
        initializer.initialize(context);
    }
}

@Override
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源码分析-005 准备容器 prepareC

      本文链接:https://www.haomeiwen.com/subject/shswgqtx.html