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

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

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

问题

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

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


我尝试读取这些值:

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

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


然而,在运行应用程序时,它没有读取默认值并抛出错误:

> 无法解析占位符 'TEST_USER' 在值 "${TEST_USER}"

我也尝试过:

testUser: ${TEST_USER:#{spring}}


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

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

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

I am trying to read this values:

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

@Value("${TEST_PASSWORD}")
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:

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:

确定