简体   繁体   English

通过环境变量覆盖 Spring 引导 yaml 属性

[英]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.

相关问题 Spring Boot YAML默认值和环境变量像HOCON文件一样被覆盖 - spring boot YAML default and environment variable override like HOCON files 通过环境变量设置Spring引导yaml配置列表属性 - Set Spring boot yaml configuration list property via environment variables Spring Boot - 覆盖地图中包含的属性(YAML) - Spring Boot - override property contained in a map (YAML) Spring 引导 + Docker 撰写:如何覆盖“地图” <string, list<string> &gt;' 来自 yaml 属性的环境变量</string,> - Spring Boot + Docker Compose: How to override 'Map<String, List<String>>' environment variable from yaml properties Springboot with Docker: environment variable to override RabbitMQ host IP property in spring boot's application.properties is not working - Springboot with Docker: environment variable to override RabbitMQ host IP property in spring boot's application.properties is not working 在 Spring 引导中使用环境变量覆盖 application.yml - Override application.yml with environment variable in Spring boot 通过 spring config + spring config 中的环境变量设置 logback 属性 - Setting logback property via spring config + environment variable in spring config 通过环境变量在 Spring 引导中设置日志记录级别 - Set Logging Level in Spring Boot via Environment Variable Spring 引导:覆盖属性值 - Spring boot: Override property value Tomcat 10 将环境变量或属性传递给 spring 启动上下文 - Tomcat 10 pass Environment Variable or Property to spring boot context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM