【问题标题】:How to find location of application.yml loaded by spring?如何找到spring加载的application.yml的位置?
【发布时间】:2020-07-20 20:43:04
【问题描述】:

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

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

我正在使用 spring 5.2.2 和 springboot 2.2.2。

【问题讨论】:

    标签: java spring spring-boot configuration


    【解决方案1】:

    我不知道一种编程方式,但 Spring 的文档中有一个很棒的链接,它显示了所有 17(!) default property locations,包括它们被读取的顺序。

    您可能想在这里查看:https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config

    【讨论】:

    • 谢谢,有趣的阅读。考虑到配置的可能性,我想无论如何根据配置位置做出运行时决定都会很臭。我的目标是检查用户是否按预期运行,或者他/她是否修改了与部署的应用程序包随附的application.yml 文件相比的某些属性。
    【解决方案2】:

    我认为在应用启动后是不可能找到的。

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

    要快速启用调试日志到控制台,您可以在命令行中使用--debug 或在application.yml 中使用debug=true 运行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)
    

    【讨论】:

      猜你喜欢
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      • 2019-07-17
      • 2016-12-07
      • 2018-12-30
      • 1970-01-01
      • 2012-03-12
      • 2020-02-19
      相关资源
      最近更新 更多