[英]How to use Eclipse Variables in Spring Boot application.properties
I'm very new to Spring boot and I'm setting up my DB connection in then application.properties.我对 Spring 启动非常陌生,然后我正在 application.properties 中设置我的数据库连接。 Since I'd like to upload my project on GitHub I'm trying to access the datasource id and password from enviroment variables, instead of hardcoding them inside the properties file.
因为我想在 GitHub 上上传我的项目,所以我试图从环境变量中访问数据源 ID 和密码,而不是在属性文件中硬编码它们。 But I'm not really sure about how to do it.
但我不太确定该怎么做。
I'm using a Sql server instance running in a container, and I've tried the following:我正在使用在容器中运行的 Sql 服务器实例,并且尝试了以下操作:
In "run configuration", in the Enviroment Tab, I added 2 enviroment variables: DB_USERNAME and DB_PASSWORD在“运行配置”的环境选项卡中,我添加了 2 个环境变量:DB_USERNAME 和 DB_PASSWORD
The application.properties looks like this: application.properties 看起来像这样:
spring.datasource.username=${DB_USERNAME)
spring.datasource.password=${DB_PASSWORD)
But when i do run the spring boot application i get the following error:但是当我运行 spring 启动应用程序时,我收到以下错误:
com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user '${DB_USERNAME)'
Can you help me figure out how to use the eclipse enviroment variables for this purpose in the right way?您能帮我弄清楚如何以正确的方式为此目的使用 eclipse 环境变量吗?
Looks like there is a typo there:那里似乎有一个错字:
spring.datasource.username=${DB_USERNAME)
spring.datasource.password=${DB_PASSWORD)
Should actually be:实际上应该是:
spring.datasource.username=${DB_USERNAME}
spring.datasource.password=${DB_PASSWORD}
You can also specify default that would be applied if variables are not provided:如果未提供变量,您还可以指定将应用的默认值:
spring.datasource.username=${DB_USERNAME:username}
spring.datasource.password=${DB_PASSWORD:password}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.