公司简介
发展历程
高端网站建设
微信开发
APP开发
网络营销服务
电商网站定制
生物医药网站建设
外贸网站建设
教育培训网站建设
小程序开发
功能应用
客户案例
网站建设案例
小程序案例
电商平台案例
APP案例
系统平台案例
网站建设
网站设计
常见问题
小程序
公司地址
人才招聘
地址:成都市太升南路288号锦天国际A幢1002号
电话:028-86922220
为企业提供网站建设、域名注册、服务器等服务
今天就跟大家聊聊有关Feign中FeignClientFactoryBean的作用是什么,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
创新互联公司是一家专业从事成都做网站、成都网站制作、网页设计的品牌网络公司。如今是成都地区具影响力的网站设计公司,作为专业的成都网站建设公司,创新互联公司依托强大的技术实力、以及多年的网站运营经验,为您提供专业的成都网站建设、营销型网站建设及网站设计开发服务!
图1
其属性如List-1所示,这些属性的值都是在FeignClientsRegistrar中设置的。
List-1
class FeignClientFactoryBean implements FactoryBean, 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 getTarget() { FeignContext context = this.applicationContext.getBean(FeignContext.class); Feign.Builder builder = feign(context); ... List-3public class FeignAutoConfiguration { @Autowired(required = false) private List 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-4Map 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-5public void setConfigurations(List configurations) { for (C client : configurations) { this.configurations.put(client.getName(), client); } } List-6中getConext方法获取ApplicationContext,之后从该ApplicationContext中获取beangetConext方法中,如过name对应的ApplicationContext不存在,则调用createContext方法进行创建AnnotationConfigApplicationContext.register方法把配置类注册到ApplicationContext中,还设置了AnnotationConfigApplicationContext的parent为当前Spring上下文,这样当在AnnotationConfigApplicationContext中获取不到bean时,就会从父ApplicationContext中获取 List-6public T getInstance(String name, Class 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 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.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的作用是什么有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。 分享名称:Feign中FeignClientFactoryBean的作用是什么 当前路径:http://cqcxhl.cn/article/gdhjpp.html 其他资讯 兴安盟抖音代运营多少钱 抖音账号短视频,影片的内容制作 三水短视频内容运营 打造高效直播运营团队,直播团队建设的关键要素 短视频代运营费用明细一览,如何选择短视频代运营介绍商 在线咨询 服务热线 服务热线:028-86922220 TOP
由于实现了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 getTarget() { FeignContext context = this.applicationContext.getBean(FeignContext.class); Feign.Builder builder = feign(context); ...
List-3
public class FeignAutoConfiguration { @Autowired(required = false) private List 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 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 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 getInstance(String name, Class 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 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.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的作用是什么有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。