Injection of autowired dependencies failed; nested exception
无法解析value的$符号
当我们想要在程序中使用时候,想当然的使用 @Value 注解去读取这个值,就像下面这种写法一样:
@Value("${test.list}")
private List<String> testList;
你会发现程序直接报错了,报错信息如下:
java.lang.IllegalArgumentException: Could not resolve placeholder 'test.list' in value "${test.list}"
这个问题也是可以解决的,以我们要配置的 key 为 test.list 为例,新建一个 test 的配置类,将 list 作为该配置类的一个属性:
@Configuration
@ConfigurationProperties("test")
public class TestListConfig {
private List<String> list;
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
在程序其他地方使用时候。采用自动注入的方式,去获取值:
@Autowired
private TestListConfig testListConfig;
// testListConfig.getList();