[英]Spring boot - Default properties file not being read when using profile
I have a spring boot 2 application with 2 property files:我有一个带有 2 个属性文件的 Spring Boot 2 应用程序:
When I run the application in IntellijIdea using the dev profile, Spring reads both the profile specific file and the default file.当我使用 dev 配置文件在 IntellijIdea 中运行应用程序时,Spring 会读取配置文件特定文件和默认文件。
Once I build an executable jar file, I created a new application-prod.properties outside of the jar, with production environment properties and execute the application with --spring.profiles.active=prod
expecting it to read this file AND the default application.properties
, but it is only reading the file outside the jar (application-prod.properties).一旦我构建了一个可执行的 jar 文件,我就在 jar 之外创建了一个新的 application-prod.properties,具有生产环境属性,并使用
--spring.profiles.active=prod
执行应用程序,期望它读取这个文件和默认application.properties
,但它只是读取 jar (application-prod.properties) 外的文件。
Should Spring Boot read both files or am I expecting something that it does not do by default? Spring Boot 应该读取这两个文件,还是我期待它默认不做的事情?
If it should, what can I be missing?如果应该,我会缺少什么?
As mentioned by Michael, Spring boot supports property hierarchies .正如迈克尔所说,Spring boot 支持属性层次结构。
I tried this and it works as expected, you can find a reference project in here我试过了,它按预期工作,你可以在这里找到一个参考项目
A few things that I can think about:我能想到的几点:
Extract the contents of your jar file and confirm that the file named application.properties is bundled in there.提取 jar 文件的内容并确认名为 application.properties 的文件捆绑在其中。
You may be overwriting the spring.config.location property and using filenames instead of directories您可能会覆盖 spring.config.location 属性并使用文件名而不是目录
From Spring Boot documentation来自Spring Boot 文档
If you have specified any files in spring.config.location, profile-specific variants of those files are not considered.
如果您在 spring.config.location 中指定了任何文件,则不会考虑这些文件的特定于配置文件的变体。 Use directories in spring.config.location if you want to also use profile-specific properties.
如果您还想使用特定于配置文件的属性,请使用 spring.config.location 中的目录。
By default, application.properties will be used to run the Spring Boot application.默认情况下,application.properties 将用于运行 Spring Boot 应用程序。 While running the JAR file, you need to specify the spring active profile.
在运行 JAR 文件时,您需要指定 spring 活动配置文件。 only one file will be read
只会读取一个文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.