溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Feign中FeignClientFactoryBean的作用是什么

發(fā)布時(shí)間:2021-08-03 11:34:10 來源:億速云 閱讀:184 作者:Leah 欄目:大數(shù)據(jù)

今天就跟大家聊聊有關(guān)Feign中FeignClientFactoryBean的作用是什么,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

                 Feign中FeignClientFactoryBean的作用是什么

                                                                                         圖1

    其屬性如List-1所示,這些屬性的值都是在FeignClientsRegistrar中設(shè)置的。

    List-1

class FeignClientFactoryBean
		implements FactoryBean<Object>, InitializingBean, ApplicationContextAware {

	/***********************************
	 * WARNING! Nothing in this class should be @Autowired. It causes NPEs because of some
	 * lifecycle race condition.
	 ***********************************/

	private Class<?> type;

	private String name;

	private String url;

	private String contextId;

	private String path;

	private boolean decode404;

	private ApplicationContext applicationContext;

	private Class<?> fallback = void.class;

	private Class<?> fallbackFactory = void.class;
...

    由于實(shí)現(xiàn)了FactoryBean接口,我們來看最重要的getObject()方法,如List-2,getTarget方法中首先從Spring上下文中獲取FeignContext。FeignContext是在FeignAutoConfiguration中注冊(cè)到Spring容器中的,如List-3所示,會(huì)將spring容器所有的FeignClientSpecification放入到FeignContext中,F(xiàn)eignClientSpecification在Feign源碼分析之EnableFeignClients中講過,即EnableFeignClients的defaultConfiguration。

    List-2

@Override
public Object getObject() throws Exception {
    return getTarget();
}

<T> T getTarget() {
    FeignContext context = this.applicationContext.getBean(FeignContext.class);
    Feign.Builder builder = feign(context);
...

    List-3

public class FeignAutoConfiguration {
	@Autowired(required = false)
	private List<FeignClientSpecification> configurations = new ArrayList<>();

	@Bean
	public HasFeatures feignFeature() {
		return HasFeatures.namedFeature("Feign", Feign.class);
	}

	@Bean
	public FeignContext feignContext() {
		FeignContext context = new FeignContext();
		context.setConfigurations(this.configurations);
		return context;
	}
...

    List-2中,得到FeignContext后,調(diào)用feign方法,從FeignContext中獲取Encoder、Decoder、Contract,其實(shí)內(nèi)部是從spring容器中獲取的,得到Feign.Builder。

    我們可以實(shí)現(xiàn)RequestInterceptor接口,之后交給Spring容器,feign會(huì)自動(dòng)加上這個(gè)攔截器,這個(gè)的實(shí)現(xiàn)也在FeignClientFactoryBean中,在configureUsingConfiguration方法中,如下List-4

    List-4

Map<String, RequestInterceptor> requestInterceptors = context
.getInstances(this.contextId, RequestInterceptor.class);
if (requestInterceptors != null) {
    builder.requestInterceptors(requestInterceptors.values());
}

    List-4中的context.getInstances()方法內(nèi)部是如何實(shí)現(xiàn)的呢。來看下FeignContext的父類NamedContextFactory,List-5中的setConfigurations方法在List-3中調(diào)用,在構(gòu)造FeignContext的時(shí)候調(diào)用的。

    List-5

public void setConfigurations(List<C> configurations) {
    for (C client : configurations) {
        this.configurations.put(client.getName(), client);
    }
}

    List-6中

  1. getConext方法獲取ApplicationContext,之后從該ApplicationContext中獲取bean

  2. getConext方法中,如過name對(duì)應(yīng)的ApplicationContext不存在,則調(diào)用createContext方法進(jìn)行創(chuàng)建

  3. AnnotationConfigApplicationContext.register方法把配置類注冊(cè)到ApplicationContext中,還設(shè)置了AnnotationConfigApplicationContext的parent為當(dāng)前Spring上下文,這樣當(dāng)在AnnotationConfigApplicationContext中獲取不到bean時(shí),就會(huì)從父ApplicationContext中獲取

    List-6

public <T> T getInstance(String name, Class<T> type) {
    AnnotationConfigApplicationContext context = getContext(name);
    if (BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context,
            type).length > 0) {
        return context.getBean(type);
    }
    return null;
}

protected AnnotationConfigApplicationContext getContext(String name) {
    if (!this.contexts.containsKey(name)) {
        synchronized (this.contexts) {
            if (!this.contexts.containsKey(name)) {
                this.contexts.put(name, createContext(name));
            }
        }
    }
    return this.contexts.get(name);
}

protected AnnotationConfigApplicationContext createContext(String name) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    if (this.configurations.containsKey(name)) {
        for (Class<?> configuration : this.configurations.get(name)
                .getConfiguration()) {
            context.register(configuration);
        }
    }
    for (Map.Entry<String, C> entry : this.configurations.entrySet()) {
        if (entry.getKey().startsWith("default.")) {
            for (Class<?> configuration : entry.getValue().getConfiguration()) {
                context.register(configuration);
            }
        }
    }
    context.register(PropertyPlaceholderAutoConfiguration.class,
            this.defaultConfigType);
    context.getEnvironment().getPropertySources().addFirst(new MapPropertySource(
            this.propertySourceName,
            Collections.<String, Object>singletonMap(this.propertyName, name)));
    if (this.parent != null) {
        // Uses Environment from parent as well as beans
        context.setParent(this.parent);
        // jdk11 issue
        // https://github.com/spring-cloud/spring-cloud-netflix/issues/3101
        context.setClassLoader(this.parent.getClassLoader());
    }
    context.setDisplayName(generateDisplayName(name));
    context.refresh();
    return context;
}

看完上述內(nèi)容,你們對(duì)Feign中FeignClientFactoryBean的作用是什么有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI