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.

Spring Boot 获取yaml配置文件信息的更多相关文章

  1. Spring Boot 的核心配置文件有哪几个?它们的区别是什么?

    Spring Boot 的核心配置文件是 application 和 bootstrap 配置文件.application 配置文件这个容易理解,主要用于 Spring Boot 项目的自动化配置.b ...

  2. Java 获取到配置文件信息

    Java程序将数据库或者服务器IP写入到代码中,难免缺少灵活性. 如果写入到配置文件,部署到不通服务器上,只需要修改配置文 件即可. Java怎么读取配置文件 /** * 获取到配置文件信息 * @p ...

  3. Spring Boot获取前端页面参数的几种方式总结

    Spring Boot的一个好处就是通过注解可以轻松获取前端页面的参数,之后可以将参数经过一系列处理传送到后台数据库. 获得的方式有很多种,这里稍微总结一下,大致分为以下几种: 1.指定前端url请求 ...

  4. Spring Boot 获取 java resources 下文件

    Spring Boot 获取 java resources 下文件 Spring Boot 获取 resources 目录下的目录(例:获取 resources 目录下的 template 目录): ...

  5. spring boot 项目从配置文件中读取maven 的pom.xml 文件标签的内容。

    需求: 将pom.xml 文件中的版本号读取到配置文件并打印到日志中. 第一步: 在pom.xml 中添加以下标签. 第二步: 将version 标签的值读取到配置文件中 这里使用 @@  而不是  ...

  6. Spring Boot - 获取所有的Bean信息

    前言 Spring Boot启动的时候需要加载许多Bean实现最小化配置,本文将尝试找出Spring启动后加载的所有Bean信息: 通过ApplicationContext 去获取所有的Bean 通过 ...

  7. Spring Boot 2.4 配置文件将加载机制大变化

    Spring Boot 2.4.0.M2 刚刚发布,它对 application.properties 和 application.yml 文件的加载方式进行重构.如果应用程序仅使用单个 applic ...

  8. 利用神器BTrace 追踪线上 Spring Boot应用运行时信息

    概述 生产环境中的服务可能会出现各种问题,但总不能让服务下线来专门排查错误,这时候最好有一些手段来获取程序运行时信息,比如 接口方法参数/返回值.外部调用情况 以及 函数执行时间等信息以便定位问题.传 ...

  9. 【spring Boot】spring boot获取资源文件的三种方式【两种情况下】

    首先声明一点,springboot获取资源文件,需要看是 1>从spring boot默认的application.properties资源文件中获取 2>还是从自定义的资源文件中获取 带 ...

随机推荐

  1. SRS之监听端口的管理:RTMP

    1. 监听端口管理的入口函数 监听端口的管理入口在 run_master 函数中,如下: int run_master() { ... if ((ret = _srs_server->liste ...

  2. ShareX的使用

    给截图设置边框 https://github.com/ShareX/ShareX/issues/2509 TaskSettings-->Image-->Effects-->image ...

  3. docker Swarm mode集群

    基本概念 Swarm 是使用 SwarmKit 构建的 Docker 引擎内置(原生)的集群管理和编排工具. 使用 Swarm 集群之前需要了解以下几个概念. 节点 运行 Docker 的主机可以主动 ...

  4. 获取客户端ip(包含中间反向代理)

    /** * 根据request获取用户的IP地址 * @param request * @return */ public static String getRemoteHost(HttpServle ...

  5. vmware安装centos虚拟机,没有ip地址

    如果centos没有ip地址,就不能通过xshell等工具进行连接,且在上面部署的项目,外部也不能正常访问 两种方式可以解决 第一种,在安装的时候,在INSTALLATION SUMMARY界面的SY ...

  6. koa 基础(二十六)数据库 与 art-template 模板 联动 --- 编辑数据、删除数据

    1.通过 ObjectID 获取 _id 根目录/module/db.js /** * DB库 */ var MongoDB = require('mongodb'); var MongoClient ...

  7. 删除github上的一个repository

    在学github之初,我们免不了会建立一些简单的repository,那么,如何删掉它们呢? 第一步,打开所要删除的repository,比如:meilin.github.io 第二步,点击setti ...

  8. 解决JxBrowser中BrowserView控件覆盖其他控件的办法

    https://blog.csdn.net/w815878564/article/details/79699559 JxBrowser是一个基于chromium的Java浏览器组件,同时支持Swing ...

  9. shell做成csv文件

    echo a,b,c,d >aa.csv 其实就是用逗号做分隔符

  10. sql输出表中重复数据

    数据: 1 1 2 32 2 2 33 1 2 34 2 2 35 2 1 36 1 1 37 3 2 1 表格查询: SELECT * FROM `t1`; 可以看到,如果界定为 a.b.c 都相同 ...