在運行configclient項目時發生以下錯誤:
2017-08-10 22:41:52.006 WARN 59068 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configClientApplication': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'foo' in string value "${foo}"
2017-08-10 22:41:52.012 INFO 59068 --- [ main] o.apache.catalina.core.StandardService : Stopping service Tomcat
經過仔細排查,配置文件和代碼都沒有問題,就是一直報錯,後來發現configclient項目不能在maven module中正確運行,特別是跟config server在一個maven project中,原因是pom。xml的依賴造成的,作爲config client不能有以下依賴:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
否則會報錯,因此如果config client和config server在同一個項目中則要注意依賴問題,而新建一個maven project可以比較容易的避免上述問題。