springboot Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder ‘name’ in va

springboot啓動時會檢索 @Value 對應配置文件中的key,當該key不存在時就會報:Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder異常,解決方案有兩種:java

1,設置 @Value 的默認值spring

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

上面代碼中,當配置文件中 name key 不存在時,就會使用「default_name」做爲默認值,key 與默認值用「:」符號分割。springboot

2,在 Application 類中設置PropertySourcesPlaceholderConfigurer類的默認屬性spa

1 // 設置@Value註解取值不到忽略(不報錯)
2 @Bean
3 public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
4     PropertySourcesPlaceholderConfigurer c = new PropertySourcesPlaceholderConfigurer();
5     c.setIgnoreUnresolvablePlaceholders(true);
6     return c;
7 }
相關文章
相關標籤/搜索