springboot : Could not resolve placeholder 'name' in value '${name}'
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;
}
本文发布于程序达人 ,转载请注明出处,谢谢合作
共同学习,写下你的评论
相关热点文章推荐
Spring Boot文档翻译【转】
Spring Boot报java.lang.IllegalArgumentException:Property 'sqlSessionFactory' or 'sqlSessionTemplate'
SpringBoot 2.0 报错: Failed to configure a DataSource: 'url' attribute is not specified and no embe...
UploadiFive Documentation (api 说明文档)
svn: 目录中的条目从本地编码转换到 UTF8 失败 解决办法
解决Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile办法
程序达人 - chengxudaren.com
一个帮助开发者成长的社区