咱們首先來看一下報錯信息java
Description: Field businessFeignClient in com.ysc.service.BusinessConfigService required a bean of type 'com.ysc.feignclient.BusinessFeignClient' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'com.ysc.feignclient.BusinessFeignClient' in your configuration.
再來看一下 Feign 的配置信息spring
@SpringBootApplication( scanBasePackages = "com.ysc", exclude = { DataSourceAutoConfiguration.class, ThymeleafAutoConfiguration.class }) @EnableFeignClients public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
從表面上看配置並無什麼問題,那麼咱們來分析一下問題的具體緣由。ide
註解 @EnableFeignClients 與 @ComponentScan 有衝突,兩種註解都會搜索注入指定目錄中的 bean 。@EnableFeignClients 引入了 FeignClientsRegistrar 類,實現了 Spring 的bean 資源的加載。ui
FeignClientsRegistrar中registerFeignClients方法獲取了@EnableFeignClients註解中的basepackage 屬性值,並進行注入。若是兩種註解都使用時,其中@EnableFeignClients會覆蓋 @ComponentScan 中指定的目錄,從而恢復到默認目錄。code
如何解決這個問題:ip
一、能夠將 FeignClient 這個 bean 放在和 Application 啓動類同級目錄資源
二、能夠在 @EnableFeignClients中經過 clients 屬性指定 bean 目錄io
@EnableFeignClients(clients = { BusinessFeignClient.class })