zoukankan      html  css  js  c++  java
  • Spring Cloud 使用 FeignClient 启动报错

    我们首先来看一下报错信息

    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 的配置信息

    @SpringBootApplication(
            scanBasePackages = "com.ysc",
            exclude = {
                    DataSourceAutoConfiguration.class,
                    ThymeleafAutoConfiguration.class
            })
    @EnableFeignClients
    public class Application {
    	
    	public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
    }
    

    从表面上看配置并没有什么问题,那么我们来分析一下问题的具体原因。

    注解 @EnableFeignClients 与 @ComponentScan 有冲突,两种注解都会搜索注入指定目录中的 bean 。@EnableFeignClients 引入了 FeignClientsRegistrar 类,实现了 Spring 的bean 资源的加载。

    FeignClientsRegistrar中registerFeignClients方法获取了@EnableFeignClients注解中的basepackage 属性值,并进行注入。如果两种注解都使用时,其中@EnableFeignClients会覆盖 @ComponentScan 中指定的目录,从而恢复到默认目录。

    如何解决这个问题:

    1、可以将 FeignClient 这个 bean 放在和 Application 启动类同级目录

    2、可以在 @EnableFeignClients中通过 clients 属性指定 bean 目录

    @EnableFeignClients(clients = {
            BusinessFeignClient.class
    })
    
  • 相关阅读:
    IntelliJ IDEA设置JVM运行参数
    IntelliJ IDEA 乱码解决方案 (项目代码、控制台等)
    188.索引与视图
    187.数据库操作
    186.元素
    185.流程设计
    184.数据操纵语言DML
    改变linux shell前景色和背景色
    Centos文本方式安装情况下lvm分区的创建
    深入理解计算机系统第二版习题解答CSAPP 2.20
  • 原文地址:https://www.cnblogs.com/yuansc/p/10536659.html
Copyright © 2011-2022 走看看