“使用IDEA,配置文件是yml,无法获取[环境变量],值是null, UnsatisfiedDependencyException:......Injection of autowired dependencies failed; IllegalArgumentException”的问题处理

为了便于隐藏用户名和密码,我们有时会用到“环境变量”。

Spring Boot提供了很好的机制,可以在配置文件中,如application.yml书写以下格式,然后在代码中@Value就可以获取“环境变量”的值。但这有一个不大不小的坑...

mail:
  smtp:
    auth: true
    host: smtp.xxmail.com
  user-name: XX监控中心
  user: ${EMAIL_USERNAME}
  password: ${EMAIL_PASSWD}
  recipients: xx@xxx.com

如果IDEA先打开,环境变量后加入,在IDEA调试代码时,就会出现无法获取环境变量的问题,报null的错,如:

org.springframework.beans.factory.UnsatisfiedDependencyException:..................Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'XXXX' in value "${XXXX}"

 

解决办法,很简单:先加入环境变量,重新打开IDEA即可。

要注意,关闭所有的IDEA再重启,否则,还是无法加载环境变量。

posted @ 2019-01-09 13:25  姚一^o^  阅读(2191)  评论(4编辑  收藏  举报