[英]Override Spring Boot yaml property via environment variable
Using Spring Boot 2.6.1, If I have an application.properties
file that looks like:使用 Spring Boot 2.6.1,如果我有一个如下所示的application.properties
文件:
spring.datasource.url="jdbc://blahblah"
I can override that value at runtime with an environment variable named spring.datasource.url
and my application will connect to the database specified in the env var.我可以在运行时使用名为spring.datasource.url
的环境变量覆盖该值,我的应用程序将连接到 env var 中指定的数据库。
However, if I have an equivalent application.yaml
file, specifying the environment variable that way appears to have no effect.但是,如果我有一个等效的application.yaml
文件,那么以这种方式指定环境变量似乎没有效果。
spring:
datasource:
url: "jdbc://localhost..."
However, if I rename my environment variable to SPRING_DATASOURCE_URL
, the override works again.但是,如果我将环境变量重命名为SPRING_DATASOURCE_URL
,则覆盖将再次起作用。 This appears to be consistent across other properties as well (not just the datasource url).这似乎在其他属性中也是一致的(不仅仅是数据源 url)。
Looking through the docs it wasn't obvious why this should be the case, except that yaml configuration seems like it's generally treated a little different than "normal" properties files.浏览文档并不清楚为什么会出现这种情况,除了 yaml 配置似乎通常与“普通”属性文件略有不同。
Is this behaviour expected?这种行为是预期的吗?
If i did not misunderstand, you should create application-{profile}.yml files and specify datasource url for environments如果我没有误解,您应该创建 application-{profile}.yml 文件并为环境指定数据源 url
As described in the documentation , you should use the environment variable SPRING_DATASOURCE_URL
to set the spring.datasource.url
property.如文档中所述,您应该使用环境变量SPRING_DATASOURCE_URL
来设置spring.datasource.url
属性。 I am surprised that spring.datasource.url
worked at all when configured as an environment variable and I would not rely on it continuing to do so.我很惊讶spring.datasource.url
在配置为环境变量时完全有效,我不会依赖它继续这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.