英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论