无法在 application.yml 中使用 @Value() 读取值。

huangapple go评论90阅读模式
英文:

Cannot read value using @Value() in application.yml

问题

  1. 我只是尝试设置环境变量的默认值,如下所示:

app:
security:
testUser: ${TEST_USER:spring}
testPassword: ${TEST_PASSWORD:secret}

  1. 我尝试读取这些值:

@Value("${TEST_USER}")
private String testUser;

@Value("${TEST_PASSWORD}")
private String testPassword;

  1. 然而,在运行应用程序时,它没有读取默认值并抛出错误:
  2. > 无法解析占位符 'TEST_USER' 在值 "${TEST_USER}"
  3. 我也尝试过:

testUser: ${TEST_USER:#{spring}}

  1. 但仍然出现相同的错误。那么,如何解决这个问题?
英文:

I am simply trying to set default values for environment variables as shown below:

  1. app:
  2. security:
  3. testUser: ${TEST_USER:spring}
  4. testPassword: ${TEST_PASSWORD:secret}

I am trying to read this values:

  1. @Value("${TEST_USER}")
  2. private String testUser;
  3. @Value("${TEST_PASSWORD}")
  4. private String testPassword;

However, when running app, it does not read the default values and throws error:

> Could not resolve placeholder 'TEST_USER' in value "${TEST_USER}"

I also tried as:

  1. testUser: ${TEST_USER:#{spring}}

But still the same error. So, how to solve this?

答案1

得分: 0

你需要说 @Value("${app.security.testUser}")。它将从TEST_USER环境变量中获取值,如果不存在这样的环境变量,则默认为 spring

英文:

You need to say @Value("${app.security.testUser}"). It will take the value from TEST_USER environment variable or default to spring if such an environment variable does not exist

huangapple
  • 本文由 发表于 2023年3月21日 02:54:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794229-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定