欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

@FeignClient注解中屬性contextId的使用說明

 更新時間:2022年06月18日 11:13:03   作者:goodjob110  
這篇文章主要介紹了@FeignClient注解中屬性contextId的使用說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

一、概述

如果我們使用Feign定義了兩個接口,但是目標服務(wù)是同一個,那么在SpringBoot啟動時就會遇到一個問題:

Description:
The bean 'xxxxxxxx.FeignClientSpecification', defined in null, could not be registered. A bean with that name has already been defined in null and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

二、解決方案

2.1 方案1

修改yml配置:spring.main.allow-bean-definition-overriding=true

spring:
  main:
    allow-bean-definition-overriding: true

2.2 方案2

在每個Feign的接口中,在注解上加 contextId屬性

contextId在Feign Client的作用是在注冊Feign Client Configuration的時候需要一個名稱,名稱是通過getClientName方法獲取的

@FeignClient(name = "sale-service",contextId= "saleservice1")
 
public interface saleClient{
 
    @RequestMapping(value = "/sale/add", method = RequestMethod.GET)
 
    String add(@RequestParam("saleNum") String queryStr);
 
}

備注:contextId= "名稱" 中的名稱,不能用“_”會報錯,可以用“-”

三、源代碼分析

  • 包名:spring-cloud-openfeign-core-2.2.5.RELEASE.jar
  • 類路徑:org.springframework.cloud.openfeign.FeignClientsRegistrar

相關(guān)代碼1

 private void registerFeignClient(BeanDefinitionRegistry registry, AnnotationMetadata annotationMetadata, Map<String, Object> attributes) {
        String className = annotationMetadata.getClassName();
        BeanDefinitionBuilder definition = BeanDefinitionBuilder.genericBeanDefinition(FeignClientFactoryBean.class);
        this.validate(attributes);
        definition.addPropertyValue("url", this.getUrl(attributes));
        definition.addPropertyValue("path", this.getPath(attributes));
        String name = this.getName(attributes);
        definition.addPropertyValue("name", name);
        String contextId = this.getContextId(attributes);
        definition.addPropertyValue("contextId", contextId);
        definition.addPropertyValue("type", className);
        definition.addPropertyValue("decode404", attributes.get("decode404"));
        definition.addPropertyValue("fallback", attributes.get("fallback"));
        definition.addPropertyValue("fallbackFactory", attributes.get("fallbackFactory"));
        definition.setAutowireMode(2);
        String alias = contextId + "FeignClient";
        AbstractBeanDefinition beanDefinition = definition.getBeanDefinition();
        beanDefinition.setAttribute("factoryBeanObjectType", className);
        boolean primary = (Boolean)attributes.get("primary");
        beanDefinition.setPrimary(primary);
        String qualifier = this.getQualifier(attributes);
        if (StringUtils.hasText(qualifier)) {
            alias = qualifier;
        }
 
        BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, className, new String[]{alias});
        BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
    }

代碼截圖:

相關(guān)代碼2

可以看到, name應(yīng)該是從注解中的屬性取值來的, 再看看getClientName()方法.

 private String getClientName(Map<String, Object> client) {
        if (client == null) {
            return null;
        } else {
            String value = (String)client.get("contextId");
            if (!StringUtils.hasText(value)) {
                value = (String)client.get("value");
            }
 
            if (!StringUtils.hasText(value)) {
                value = (String)client.get("name");
            }
 
            if (!StringUtils.hasText(value)) {
                value = (String)client.get("serviceId");
            }
 
            if (StringUtils.hasText(value)) {
                return value;
            } else {
                throw new IllegalStateException("Either 'name' or 'value' must be provided in @" + FeignClient.class.getSimpleName());
            }
        }
    }

代碼截圖:

一目了然了, 我們聲明@FeignClient注解時, 只使用了value屬性, 所以產(chǎn)生了沖突, 只要加上contextId就好了.

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring 跨域配置請求詳解

    Spring 跨域配置請求詳解

    這篇文章主要介紹了Spring 跨域配置請求詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-01-01
  • Java編程實現(xiàn)調(diào)用com操作Word方法實例代碼

    Java編程實現(xiàn)調(diào)用com操作Word方法實例代碼

    這篇文章主要介紹了Java編程實現(xiàn)調(diào)用com操作Word方法實例代碼,代碼注釋很詳細,在這里分給大家,需要的朋友可以參考下。
    2017-09-09
  • Java字符串常見的操作(比較,查找,替換等)

    Java字符串常見的操作(比較,查找,替換等)

    在Java當中,為字符串類提供了豐富的操作方法,對于字符串,我們常見的操作就是:字符串的比較、查找、替換、拆分、截取以及其他的一些操作,本文就詳細的介紹一下,感興趣的可以了解一下
    2022-01-01
  • JUnit5常用注解的使用

    JUnit5常用注解的使用

    注解是JUnit的標志性技術(shù),本文就來對它的20個注解,以及元注解和組合注解進行學習,感興趣的可以了解一下
    2021-07-07
  • SpringBoot停止啟動時測試檢查rabbitmq操作

    SpringBoot停止啟動時測試檢查rabbitmq操作

    這篇文章主要介紹了SpringBoot停止啟動時測試檢查rabbitmq操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java實現(xiàn)定制數(shù)據(jù)透視表的示例詳解

    java實現(xiàn)定制數(shù)據(jù)透視表的示例詳解

    數(shù)據(jù)透視表(Pivot?Table)是一種數(shù)據(jù)分析工具,通常用于對大量數(shù)據(jù)進行匯總、分析和展示,本文主要介紹了如何使用Java將計算項添加到數(shù)據(jù)透視表中,感興趣的可以了解下
    2023-12-12
  • java中Hashtable和HashMap的區(qū)別分析

    java中Hashtable和HashMap的區(qū)別分析

    java中Hashtable和HashMap的區(qū)別分析,需要的朋友可以參考一下
    2013-04-04
  • java反射總結(jié)實例詳解

    java反射總結(jié)實例詳解

    這篇文章主要結(jié)合實例形式分析了介紹了java基于反射得到對象屬性值的方法,Class類,基本數(shù)據(jù)類型,類的反射等,需要的朋友可以參考下
    2017-04-04
  • java8中parallelStream性能測試及結(jié)果分析

    java8中parallelStream性能測試及結(jié)果分析

    本篇文章給大家用代碼實例做了segmentfaultjava8中parallelStream性能測試,并對測試結(jié)果做了說明,需要的朋友學習下吧。
    2018-01-01
  • Spring Boot中使用Activiti的方法教程(二)

    Spring Boot中使用Activiti的方法教程(二)

    工作流(Workflow),就是“業(yè)務(wù)過程的部分或整體在計算機應(yīng)用環(huán)境下的自動化”,下面這篇文章主要給大家介紹了關(guān)于Spring Boot中使用Activiti的相關(guān)資料,需要的朋友可以參考下
    2018-08-08

最新評論