Spring boot 项目启动过程中:

org.springframework.boot.SpringApplication#prepareEnvironment

当程序步入listeners.environmentPrepared(environment);这里后,就会读取配置文件中信息。

private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments) {
// Create and configure the environment
ConfigurableEnvironment environment = getOrCreateEnvironment();
configureEnvironment(environment, applicationArguments.getSourceArgs());
listeners.environmentPrepared(environment);
bindToSpringApplication(environment);
if (!this.isCustomEnvironment) {
environment = new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment,
deduceEnvironmentClass());
}
ConfigurationPropertySources.attach(environment);
return environment;
}

这句代码不好调试listeners.environmentPrepared(environment);

但可以换一种方式,找到上图中的这个类:org.springframework.boot.env.OriginTrackedMapPropertySource

在断点处打上断点,就可以看到系统是如何运行的:下面是堆栈信息

在堆栈信息中,可以看到这个类:org.springframework.boot.context.config.ConfigFileApplicationListener

这个类里面的包含一些配置信息:

private static final String DEFAULT_PROPERTIES = "defaultProperties";

    // Note the order is from least to most specific (last one wins)
private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,file:./config/"; private static final String DEFAULT_NAMES = "application"; private static final Set<String> NO_SEARCH_NAMES = Collections.singleton(null); private static final Bindable<String[]> STRING_ARRAY = Bindable.of(String[].class); /**
* The "active profiles" property name.
*/
public static final String ACTIVE_PROFILES_PROPERTY = "spring.profiles.active"; /**
* The "includes profiles" property name.
*/
public static final String INCLUDE_PROFILES_PROPERTY = "spring.profiles.include"; /**
* The "config name" property name.
*/
public static final String CONFIG_NAME_PROPERTY = "spring.config.name"; /**
* The "config location" property name.
*/
public static final String CONFIG_LOCATION_PROPERTY = "spring.config.location";

一个properties. 一个yaml配置sourceloader

加载yaml文件的类是YamlPropertySourceLoader

public class YamlPropertySourceLoader implements PropertySourceLoader {

    @Override
public String[] getFileExtensions() {
return new String[] { "yml", "yaml" };
} @Override
public List<PropertySource<?>> load(String name, Resource resource) throws IOException {
if (!ClassUtils.isPresent("org.yaml.snakeyaml.Yaml", null)) {
throw new IllegalStateException(
"Attempted to load " + name + " but snakeyaml was not found on the classpath");
}
List<Map<String, Object>> loaded = new OriginTrackedYamlLoader(resource).load();
if (loaded.isEmpty()) {
return Collections.emptyList();
}
List<PropertySource<?>> propertySources = new ArrayList<>(loaded.size());
for (int i = 0; i < loaded.size(); i++) {
String documentNumber = (loaded.size() != 1) ? " (document #" + i + ")" : "";
propertySources.add(new OriginTrackedMapPropertySource(name + documentNumber, loaded.get(i)));
}
return propertySources;
} }

注如果是云环境,则首先加载的是bootstrap.yml, environment也是StandardEnvironment,非servlet环境。这是和Spring boot有很大的不同,加载完成后,再嵌套一StandardServletEnvironment.

加载完成后,再嵌套一StandardServletEnvironment.

最新文章

  1. Android——Handler
  2. LAMP_源码安装全教程
  3. BZOJ 2243: [SDOI2011]染色 [树链剖分]
  4. ES6与ES5差别
  5. 遍历map的常用方法
  6. 【转】IP分片和TCP分段
  7. ASP.NET MVC 返回JsonResult序列化内容超出最大限制报错的解决办法
  8. sina 行情api
  9. Handler 取不到session 解决办法
  10. How Tomcat Works(十六)
  11. 逻辑很重要:一句sql语句的事,自己却想了半天,绕了个大弯子
  12. EasyUI - Combo组件
  13. c#中 uint--byte[]--char[]--string相互转换汇总
  14. document.onreadystatechange()来判断页面加载完
  15. POJ 2653 Pick-up sticks [线段相交 迷之暴力]
  16. LuoguP4234_最小差值生成树_LCT
  17. ASP.Net Mvc实现自定义User Identity用户身份识别系统(1)
  18. java解压多层目录中多个压缩文件和处理压缩文件中有内层目录的情况
  19. yaf twig配置
  20. Python-简单打印进度条

热门文章

  1. 关于开发APP接口版本不兼容的问题
  2. C++学习 之 初识头文件
  3. 【React自制全家桶】三、React使用ref操作DOM与setState遇到的问题
  4. Python获取两个文件的交集、并集、差集
  5. js 中实现 汉字按拼音排序
  6. flask 学习(四)
  7. Python sys.argv[] 使用
  8. mongod 对指定数据库创建用户
  9. js __proto__ 和prototype的联系
  10. split切割.号的字符串