編寫SpringCloud的Config Client端時,當嘗試去配置文件裏拿某${xxx}的值會出現以下錯誤:
org.springframework.beans.factory.BeanCreationException: Error creating bean with nameInjection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'xxx' in value "${xxx}。
當在第一個application.yml下找不到的時候,它就會報錯。
參考諸多解決方法,發現在主啓動類中添加如下片段是正解:
@Bean
public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
PropertySourcesPlaceholderConfigurer c = new PropertySourcesPlaceholderConfigurer();
c.setIgnoreUnresolvablePlaceholders(true);
return c;
}
添加之後會忽略暫時在該配置文件裏未取到的value,繼續尋找下去,該問題就解決了。