简体   繁体   English

如何在 Spring Boot application.properties 中使用 Eclipse 变量

[英]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 服务器实例,并且尝试了以下操作:

  1. In "run configuration", in the Enviroment Tab, I added 2 enviroment variables: DB_USERNAME and DB_PASSWORD在“运行配置”的环境选项卡中,我添加了 2 个环境变量:DB_USERNAME 和 DB_PASSWORD

  2. 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.

相关问题 如何在 spring 引导中使用带有 application.properties 的环境变量? - How to use environment variables with application.properties in spring boot? 如何阅读Spring Boot application.properties? - How to read Spring Boot application.properties? Eclipse 正在将一个空的 application.properties 复制到 target/classes (spring boot) - Eclipse is copying an empty application.properties to target/classes (spring boot) 在 Spring-boot 的 application.properties 添加自定义变量 - Add custom variables in application.properties of Spring-boot spring-boot application.properties中的环境变量错误 - Environment Variables in spring-boot application.properties error application.properties 中电子邮件数据中 Spring-Boot 中的环境变量 - environment variables in Spring-Boot in email data in application.properties 如何在Spring Boot中的application.properties中指定外部属性文件? - How to specify external properties files in application.properties in Spring Boot? Spring Boot:使用 database 和 application.properties 进行配置 - Spring Boot: Use database and application.properties for configuration 使用 Spring Boot Cloud Config 更改类路径中的 application.properties - Use Spring Boot Cloud Config to change application.properties in classpath 运行测试时如何指示 Spring Boot 使用测试资源中的 application.properties 文件 - How to instruct Spring Boot to use application.properties file in test's resources when running tests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM