首页 » java

springboot : Could not resolve placeholder 'name' in value '${name}'

   发表于:java评论 (0)   热度:3560

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 的默认值

@Value("${name:default_name}")
private String name;

上面代码中,当配置文件中 name key 不存在时,就会使用“default_name”作为默认值,key 与默认值用“:”符号分割。

2,在 Application 类中设置PropertySourcesPlaceholderConfigurer类的默认属性

// 设置@Value注解取值不到忽略(不报错)
@Bean
public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
    PropertySourcesPlaceholderConfigurer c = new PropertySourcesPlaceholderConfigurer();
    c.setIgnoreUnresolvablePlaceholders(true);
    return c;
}

 

 

(。・v・。)
喜欢这篇文章吗?欢迎分享到你的微博、QQ群,并关注我们的微博,谢谢支持。