您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關Feign中FeignClientFactoryBean的作用是什么,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
圖1
其屬性如List-1所示,這些屬性的值都是在FeignClientsRegistrar中設置的。
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; ...
由于實現了FactoryBean接口,我們來看最重要的getObject()方法,如List-2,getTarget方法中首先從Spring上下文中獲取FeignContext。FeignContext是在FeignAutoConfiguration中注冊到Spring容器中的,如List-3所示,會將spring容器所有的FeignClientSpecification放入到FeignContext中,FeignClientSpecification在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后,調用feign方法,從FeignContext中獲取Encoder、Decoder、Contract,其實內部是從spring容器中獲取的,得到Feign.Builder。
我們可以實現RequestInterceptor接口,之后交給Spring容器,feign會自動加上這個攔截器,這個的實現也在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()方法內部是如何實現的呢。來看下FeignContext的父類NamedContextFactory,List-5中的setConfigurations方法在List-3中調用,在構造FeignContext的時候調用的。
List-5
public void setConfigurations(List<C> configurations) { for (C client : configurations) { this.configurations.put(client.getName(), client); } }
List-6中
getConext方法獲取ApplicationContext,之后從該ApplicationContext中獲取bean
getConext方法中,如過name對應的ApplicationContext不存在,則調用createContext方法進行創建
AnnotationConfigApplicationContext.register方法把配置類注冊到ApplicationContext中,還設置了AnnotationConfigApplicationContext的parent為當前Spring上下文,這樣當在AnnotationConfigApplicationContext中獲取不到bean時,就會從父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; }
看完上述內容,你們對Feign中FeignClientFactoryBean的作用是什么有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。