首页 > 解决方案 > 如何找到spring加载的application.yml的位置?

问题描述

为了配置 Spring Boot 应用程序,我可以application.yml通过将文件放置在 Spring 扫描此类文件的位置之一(./config/、cwd、classpath:config/、classpath root)来控制加载哪个配置/属性文件(比方说) )。spring.config.location我还可以使用 CLI ( ) 或环境变量指向特定位置。

如何找出运行时最终从何处加载属性文件?我想检查用户是否指定/使用了他自己的配置文件,或者是否使用了提供的配置文件。

我正在使用 spring 5.2.2 和 springboot 2.2.2。

标签: javaspringspring-bootconfiguration

解决方案


我认为在应用程序启动后不可能找到它。

但是,您可以在启动期间检查“org.springframework.boot”的调试日志输出。

要快速启用调试日志到控制台,您可以--debug在命令行或debug=trueapplication.yml 中运行 spring boot 应用程序。Spring Boot 找到的配置文件应该在前几行。

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.6.RELEASE)

2020-04-08 15:05:22.736  INFO 19079 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication on Dwalin with PID 19079 (/home/geertp/repos/github.com/greyfairer/spring-boot-test/demo/target/classes started by geertp in /home/geertp/repos/github.com/greyfairer/spring-boot-test/demo)
2020-04-08 15:05:22.736  INFO 19079 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2020-04-08 15:05:22.737 DEBUG 19079 --- [           main] o.s.boot.SpringApplication               : Loading source class com.example.demo.DemoApplication
2020-04-08 15:05:22.765 DEBUG 19079 --- [           main] o.s.b.c.c.ConfigFileApplicationListener  : Loaded config file 'file:/home/geertp/repos/github.com/greyfairer/spring-boot-test/demo/target/classes/application.yml' (classpath:/application.yml)

推荐阅读