如何找到被Spring加载的application.yml文件的位置?

huangapple go评论85阅读模式
英文:

How to find location of application.yml loaded by spring?

问题

关于配置Spring Boot应用程序,我可以通过将配置/属性文件(比如说 application.yml)放置在Spring扫描此类文件的位置之一(./config/、当前工作目录、classpath:config/、classpath根目录),来控制加载哪个文件。我还可以使用命令行界面(spring.config.location)或环境变量指向特定位置。

在运行时,我该如何查找属性文件最终加载自哪个位置?我希望检查用户是否指定/使用了他自己的配置文件,还是使用了提供的配置文件。

我正在使用Spring 5.2.2和Spring Boot 2.2.2。

英文:

For configuring a spring boot application I can control which configuration/properties file (let's say application.yml) is loaded by placing the file in one of the locations where spring scans for such files (./config/, cwd, classpath:config/, classpath root). I can also point to a specific location using CLI (spring.config.location) or environment variables.

How can I find out where the properties file(s) was finally loaded from during runtime? I wish to check if the user specified/used his own config file or if the provided config file was used.

I am using spring 5.2.2 and springboot 2.2.2.

答案1

得分: 5

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

但是,您可以在启动过程中检查 org.springframework.boot 的调试日志输出。

要快速将调试日志输出到控制台,您可以在命令行中使用 --debug 来运行 Spring Boot 应用程序,或在 application.yml 中使用 debug=true。Spring Boot 找到的配置文件应该位于前几行。

.   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.6.RELEASE)
 
2020-04-08 15:05:22.736  INFO 19079 --- [           main] com.example.demo.DemoApplication         : 在 Dwalin 上以 PID 19079 启动 DemoApplication (/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         : 未设置活动配置文件,回退到默认配置文件: default
2020-04-08 15:05:22.737 DEBUG 19079 --- [           main] o.s.boot.SpringApplication               : 加载源类 com.example.demo.DemoApplication
2020-04-08 15:05:22.765 DEBUG 19079 --- [           main] o.s.b.c.c.ConfigFileApplicationListener  : 已加载配置文件 'file:/home/geertp/repos/github.com/greyfairer/spring-boot-test/demo/target/classes/application.yml' (classpath:/application.yml)
英文:

I don't think it's possible to find that after the application is started.

However, you can check the debug log output for 'org.springframework.boot' during startup.

To quickly enable debug logging to console you can run the spring boot application with --debug on the command line or debug=true in application.yml. The config files found by Spring Boot should be in the first few lines.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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)

答案2

得分: 2

我不知道有没有编程的方法来实现这一点,但在Spring的文档中有一个很棒的链接,展示了所有17(!)个默认属性位置,包括它们被读取的顺序。

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

英文:

I don't know of a programmatic way of doing it, but there's a fantastic link in Spring's documentation which shows all 17(!) default property locations, including the order they are being read in.

You might want to check it out here: https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config

答案3

得分: 0

与Spring Boot 2.4一样,ConfigFileApplicationListener已被弃用,应使用新的ConfigDataEnvironment

logging.level:
    # 对于Spring Boot 1.x.x。
    "org.springframework.core.env": trace
    # 对于Spring Boot 1.x.x-2.3.x。
    "org.springframework.boot.context.config.ConfigFileApplicationListener": trace
    # 对于Spring Boot 2.4.x-3.x.x。
    "org.springframework.boot.context.config.ConfigDataEnvironment": trace
    "org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor": trace
英文:

As with Spring Boot 2.4 the ConfigFileApplicationListener was deprecated the new one should be used: ConfigDataEnvironment:

logging.level:
    # For Spring Boot 1.x.x.
    "org.springframework.core.env": trace
    # For Spring Boot 1.x.x-2.3.x.
    "org.springframework.boot.context.config.ConfigFileApplicationListener": trace
    # For Spring Boot 2.4.x-3.x.x.
    "org.springframework.boot.context.config.ConfigDataEnvironment": trace
    "org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor": trace

huangapple
  • 本文由 发表于 2020年4月8日 20:27:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/61100662.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定