Spring Feign 注入失敗問題排查思路

Spring Feign 注入失敗問題排查思路

問題的表現很明顯,就是在spring容器中找不到被@FeignClient標註類的實例:

***************************
APPLICATION FAILED TO START
***************************
 
Description:
 
Field xxxClient in xxx包 required a bean of type 'xxxClient' that could not be found.
 
 
Action:
Consider defining a bean of type 'xxxClient' in your configuration.

造成上述異常的原因很明顯,在spring容器中找不到對應的實例。

被@FeignClient標註的類,在容器初始化的時候會生成對應的代理的,如果沒有生成,說明spring沒有掃描到該類。所以在啓動類上加上 @EnableFeignClients 即可。

但有的情況下,只加@EnableFeignClients也是掃描不到的,比如說,你的項目是多模塊,包名不同,所以掃描不到,這樣可以在註解中指定 basePackages:

@EnableFeignClients(basePackages = "xxx.proxy")
public class xxxApplication {

    public static void main(String[] args) {
        SpringApplication.run(xxxApplicationn.class, args);
    }
}

意思是掃描指定包路徑下的 帶有 @FeignClient 的類。

如果你的 @EnableFeignClients 飄紅引用不到,那你一定是引用錯包了,對於SpringBoot這邊建議引用starter:

<!--    feign    -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章