spring - 从application.properties文件中读取环境变量

Ole*_*leg 7 java spring spring-boot

我在application.properties文件中指定了Spring属性.如何从环境变量中填充这些属性?

这是我尝试过的,但它似乎不起作用:

application.properties

spring.datasource.url=jdbc:postgresql://#{ systemProperties['DATABASE_HOST']}:5432/dbname
spring.datasource.username = postgres
spring.datasource.password = postgres
Run Code Online (Sandbox Code Playgroud)

Mac*_*iak 8

您可以像使用${...}语法引用Spring属性一样引用环境属性.

在你的情况下:

spring.datasource.url=jdbc:postgresql://${DATABASE_HOST}:5432/dbname
Run Code Online (Sandbox Code Playgroud)

  • 我有一个环境变量名称:user 和值:pass。在我的 application.properties 中,我使用了 datasource.username=${user}。它不读取值 (2认同)