关于springboot:81SpringBoot启动流程把spring应用跑起来

筹备环境 :

private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
            DefaultBootstrapContext bootstrapContext, ApplicationArguments applicationArguments) {
        // Create and configure the environment
        //如果以后有上下文环境信息,就获取并返回;没有就创立
        //比方保留底层运行的环境变量
        ConfigurableEnvironment environment = getOrCreateEnvironment();
        //配置环境(拿到环境信息,命令行参数)
        configureEnvironment(environment, applicationArguments.getSourceArgs());
        //绑定环境信息
        ConfigurationPropertySources.attach(environment);
//监听器调用 listener.environmentPrepared();告诉所有的监听器以后环境筹备实现
        listeners.environmentPrepared(bootstrapContext, environment);
//将环境信息设置到类的最初(这个不必管)
        DefaultPropertiesPropertySource.moveToEnd(environment);
        //配置额定的Profiles,激活哪些环境(不必管)
        configureAdditionalProfiles(environment);
        //绑定工作(都是找到一些货色,把一些属性绑定到某个值外面)
        bindToSpringApplication(environment);
        //又是筹备一些环境信息
        if (!this.isCustomEnvironment) {
            environment = new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment,
                    deduceEnvironmentClass());
        }
        //筹备完了后,把环境信息再次绑定
        ConfigurationPropertySources.attach(environment);
        return environment;
    }

=================进入getOrCreateEnvironment()
private ConfigurableEnvironment getOrCreateEnvironment() {
        if (this.environment != null) {
            return this.environment;
        }
        switch (this.webApplicationType) {
        case SERVLET: //如果是原生servlet编程
            return new StandardServletEnvironment();
        case REACTIVE: //如果是响应式编程
            return new StandardReactiveWebEnvironment();
        default:
            return new StandardEnvironment();
        }
    }


====================进入configureEnvironment()
protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) {
        //给环境增加一些类型转换器
        if (this.addConversionService) {
            ConversionService conversionService = ApplicationConversionService.getSharedInstance();
            environment.setConversionService((ConfigurableConversionService) conversionService);
        }
        //加载(读取)内部配置源
        //即读取所有的配置源的配置属性值
        configurePropertySources(environment, args);
        //激活Profiles环境
        configureProfiles(environment, args);
    }
==========进入configurePropertySources()
protected void configurePropertySources(ConfigurableEnvironment environment, String[] args) {
        //获取到所有配置源
        MutablePropertySources sources = environment.getPropertySources();
        DefaultPropertiesPropertySource.ifNotEmpty(this.defaultProperties, sources::addLast);
        //命令行信息
        if (this.addCommandLineProperties && args.length > 0) {
            String name = CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME;
            if (sources.contains(name)) {
                PropertySource<?> source = sources.get(name);
                CompositePropertySource composite = new CompositePropertySource(name);
                composite.addPropertySource(
                        new SimpleCommandLinePropertySource("springApplicationCommandLineArgs", args));
                composite.addPropertySource(source);
                sources.replace(name, composite);
            }
            else {
                sources.addFirst(new SimpleCommandLinePropertySource(args));
            }
        }
    }

==============进入environmentPrepared()
void environmentPrepared(ConfigurableBootstrapContext bootstrapContext, ConfigurableEnvironment environment) {
        doWithListeners("spring.boot.application.environment-prepared",
                (listener) -> listener.environmentPrepared(bootstrapContext, environment));
    }

所有配置源

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理