在 Spring Boot 中读取 yml 值时无法解析占位符

mas*_*urm 6 yaml spring-boot

我正在使用 Spring Boot 并在我的 application.yml 中使用这个值

config:
  username: abc
  password: xyz
Run Code Online (Sandbox Code Playgroud)

在我的课堂上,我这样使用它 -

@Value("${config.username}")
    private String username;
Run Code Online (Sandbox Code Playgroud)

我收到以下错误 -

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'config.username' in value "${config.username}"
Run Code Online (Sandbox Code Playgroud)

但是,如果我在 application.yml 文件中使用它,它就可以工作。你能告诉我我在这里做错了什么吗?

config.username: abc
config.password: xyz
Run Code Online (Sandbox Code Playgroud)

小智 4

检查你的占位符是 Tab 还是 Space ?确保 yml 文件中没有 Tab。