Spring boot 启动原理加载配置文件prepareEnv

2020-01-09  本文已影响0人  多喝热水丶
public ConfigurableApplicationContext run(String... args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ConfigurableApplicationContext context = null;
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
        configureHeadlessProperty();
        SpringApplicationRunListeners listeners = getRunListeners(args);
        listeners.starting();
        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(
                    args);
            // 本文核心讲解
            ConfigurableEnvironment environment = prepareEnvironment(listeners,
                    applicationArguments);
            configureIgnoreBeanInfo(environment);
            .....................
}

private ConfigurableEnvironment prepareEnvironment(
            SpringApplicationRunListeners listeners,
            ApplicationArguments applicationArguments) {
        // Create and configure the environment
        // 创建并且准备环境(根据应用环境 加载系统配置(JVM JDK 系统参数))
        ConfigurableEnvironment environment = getOrCreateEnvironment();
        // 加载当前默认配置文件,profiles.active,args  等信息加载
        configureEnvironment(environment, applicationArguments.getSourceArgs());
        // 触发 ApplicationEnvironmentPreparedEvent 事件,同时会触发多中监听器
        // 同时触发ConfigFileApplicationListener 加载spring配置文件
        listeners.environmentPrepared(environment);
        //将环境environment绑定到SpringApplication
        bindToSpringApplication(environment);
        if (!this.isCustomEnvironment) {
            // 类型转换
            environment = new EnvironmentConverter(getClassLoader())
                    .convertEnvironmentIfNecessary(environment, deduceEnvironmentClass());
        }
        ConfigurationPropertySources.attach(environment);
        return environment;
}


// 
/**
* 创建并且准备环境(根据应用环境 加载系统配置(JVM JDK 系统参数))
* 根据当前应用类型创建相应的Environment
* 初始化一些系统参数
**/
private ConfigurableEnvironment getOrCreateEnvironment() {
        if (this.environment != null) {
            return this.environment;
        }
        switch (this.webApplicationType) {
        case SERVLET:
            return new StandardServletEnvironment();
        case REACTIVE:
            return new StandardReactiveWebEnvironment();
        default:
            return new StandardEnvironment();
    }
}
//配置环境
protected void configureEnvironment(ConfigurableEnvironment environment,
            String[] args) {
        // 将args 的参数绑定到PropertySources(MutablePropertySources)
        configurePropertySources(environment, args);
        // 配置环境(会加载System.Property属性,以及系统参数)
        configureProfiles(environment, args);
    }
// 启动监听器
public void environmentPrepared(ConfigurableEnvironment environment) {
    for (SpringApplicationRunListener listener : this.listeners) {
        istener.environmentPrepared(environment);
    }
}

// 触发ApplicationEnvironmentPreparedEvent 事件
@Override
public void environmentPrepared(ConfigurableEnvironment environment) {
    this.initialMulticaster.multicastEvent(new ApplicationEnvironmentPreparedEvent(
            this.application, this.args, environment));
}
// 下班开溜....
上一篇 下一篇

猜你喜欢

热点阅读