springboot Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder ‘name’ in value “${name}” 错误解决:
springboot启动时会检索 @Value 对应配置文件中的key,当该key不存在时就会报:Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder异常,解决方案有两种:
1,设置 @Value 的默认值
1 @Value("${name:default_name}") 2 private String name;
上面代码中,当配置文件中 name key 不存在时,就会使用“default_name”作为默认值,key 与默认值用“:”符号分割。
2,在 Application 类中设置PropertySourcesPlaceholderConfigurer类的默认属性
1 // 设置@Value注解取值不到忽略(不报错) 2 @Bean 3 public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() { 4 PropertySourcesPlaceholderConfigurer c = new PropertySourcesPlaceholderConfigurer(); 5 c.setIgnoreUnresolvablePlaceholders(true); 6 return c; 7 }