首页 > 其他分享 >springboot Config类@Import导入Config类中bean覆盖方法

springboot Config类@Import导入Config类中bean覆盖方法

时间:2023-02-01 10:36:00浏览次数:39  
标签:feign return springboot new springframework bean org import Config

spring-cloud-openfeign-core第三方包里面有FeignClientsConfiguration.class里面是包括了feign需要的bean,但里面已经定义了feignEncoder、feignDecoder,如果项目中想要定制feignEncoder、feignDecoder就会冲突报错No bean found of type interface feign.codec.Encoder for XXXapi,但项目中有自定义这两个bean。显然这种在Config类@Import另一个Config类,又重定义里里面的bean,即使另一个Config类有用到@ConditionalOnMissingBean也不行,只需要在冲突的bean上加 @Primary就能正常加载了,而且另一个Config类冲突的bean也不会加载到。

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import feign.Logger;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.optionals.OptionalDecoder;
import io.vavr.jackson.datatype.VavrModule;
import okhttp3.ConnectionPool;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.FeignClientsConfiguration;
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import java.util.concurrent.TimeUnit; @Import(FeignClientsConfiguration.class) @Configuration public class FeignClientConfig { @Bean public okhttp3.OkHttpClient okHttpClient(){ return new okhttp3.OkHttpClient.Builder() .readTimeout(60, TimeUnit.SECONDS) .connectTimeout(60,TimeUnit.SECONDS) .connectionPool(new ConnectionPool()) .build(); } @Bean public Logger.Level feignLoggerLevel(){ return Logger.Level.BASIC; } @Bean
@Primary public Encoder feignEncoder() { return new SpringEncoder(feignHttpMessageConverter()); } @Bean
@Primary public Decoder feignDecoder() { return new OptionalDecoder( new ResponseEntityDecoder(new SpringDecoder(feignHttpMessageConverter()))); } public ObjectFactory<HttpMessageConverters> feignHttpMessageConverter() { MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter(); ObjectMapper objectMapper = jackson2HttpMessageConverter.getObjectMapper(); objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); // Either 序列化问题 objectMapper.registerModule(new VavrModule()); jackson2HttpMessageConverter.setObjectMapper(objectMapper); return () -> new HttpMessageConverters(jackson2HttpMessageConverter); } }

  

/*
 * Copyright 2013-2022 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.cloud.openfeign;

import java.util.ArrayList;
import java.util.List;

import feign.Contract;
import feign.Feign;
import feign.Logger;
import feign.QueryMapEncoder;
import feign.Retryer;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.form.MultipartFormContentProcessor;
import feign.form.spring.SpringFormEncoder;
import feign.micrometer.MicrometerCapability;
import feign.optionals.OptionalDecoder;
import io.micrometer.core.instrument.MeterRegistry;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.client.circuitbreaker.CircuitBreaker;
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
import org.springframework.cloud.openfeign.clientconfig.FeignClientConfigurer;
import org.springframework.cloud.openfeign.support.AbstractFormWriter;
import org.springframework.cloud.openfeign.support.FeignEncoderProperties;
import org.springframework.cloud.openfeign.support.HttpMessageConverterCustomizer;
import org.springframework.cloud.openfeign.support.PageableSpringEncoder;
import org.springframework.cloud.openfeign.support.PageableSpringQueryMapEncoder;
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.cloud.openfeign.support.SpringMvcContract;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.core.convert.ConversionService;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.format.support.FormattingConversionService;

import static feign.form.ContentType.MULTIPART;

/**
 * @author Dave Syer
 * @author Venil Noronha
 * @author Darren Foong
 * @author Jonatan Ivanov
 * @author Olga Maciaszek-Sharma
 * @author Hyeonmin Park
 * @author Yanming Zhou
 */
@Configuration(proxyBeanMethods = false)
public class FeignClientsConfiguration {

	@Autowired
	private ObjectFactory<HttpMessageConverters> messageConverters;

	@Autowired(required = false)
	private List<AnnotatedParameterProcessor> parameterProcessors = new ArrayList<>();

	@Autowired(required = false)
	private List<FeignFormatterRegistrar> feignFormatterRegistrars = new ArrayList<>();

	@Autowired(required = false)
	private Logger logger;

	@Autowired(required = false)
	private SpringDataWebProperties springDataWebProperties;

	@Autowired(required = false)
	private FeignClientProperties feignClientProperties;

	@Autowired(required = false)
	private FeignEncoderProperties encoderProperties;

	@Bean
	@ConditionalOnMissingBean
	public Decoder feignDecoder(ObjectProvider<HttpMessageConverterCustomizer> customizers) {
		return new OptionalDecoder(new ResponseEntityDecoder(new SpringDecoder(messageConverters, customizers)));
	}

	@Bean
	@ConditionalOnMissingBean
	@ConditionalOnMissingClass("org.springframework.data.domain.Pageable")
	public Encoder feignEncoder(ObjectProvider<AbstractFormWriter> formWriterProvider,
			ObjectProvider<HttpMessageConverterCustomizer> customizers) {
		return springEncoder(formWriterProvider, encoderProperties, customizers);
	}

	@Bean
	@ConditionalOnClass(name = "org.springframework.data.domain.Pageable")
	@ConditionalOnMissingBean
	public Encoder feignEncoderPageable(ObjectProvider<AbstractFormWriter> formWriterProvider,
			ObjectProvider<HttpMessageConverterCustomizer> customizers) {
		PageableSpringEncoder encoder = new PageableSpringEncoder(
				springEncoder(formWriterProvider, encoderProperties, customizers));

		if (springDataWebProperties != null) {
			encoder.setPageParameter(springDataWebProperties.getPageable().getPageParameter());
			encoder.setSizeParameter(springDataWebProperties.getPageable().getSizeParameter());
			encoder.setSortParameter(springDataWebProperties.getSort().getSortParameter());
		}
		return encoder;
	}

	@Bean
	@ConditionalOnClass(name = "org.springframework.data.domain.Pageable")
	@ConditionalOnMissingBean
	public QueryMapEncoder feignQueryMapEncoderPageable() {
		PageableSpringQueryMapEncoder queryMapEncoder = new PageableSpringQueryMapEncoder();
		if (springDataWebProperties != null) {
			queryMapEncoder.setPageParameter(springDataWebProperties.getPageable().getPageParameter());
			queryMapEncoder.setSizeParameter(springDataWebProperties.getPageable().getSizeParameter());
			queryMapEncoder.setSortParameter(springDataWebProperties.getSort().getSortParameter());
		}
		return queryMapEncoder;
	}

	@Bean
	@ConditionalOnMissingBean
	public Contract feignContract(ConversionService feignConversionService) {
		boolean decodeSlash = feignClientProperties == null || feignClientProperties.isDecodeSlash();
		return new SpringMvcContract(parameterProcessors, feignConversionService, decodeSlash);
	}

	@Bean
	public FormattingConversionService feignConversionService() {
		FormattingConversionService conversionService = new DefaultFormattingConversionService();
		for (FeignFormatterRegistrar feignFormatterRegistrar : feignFormatterRegistrars) {
			feignFormatterRegistrar.registerFormatters(conversionService);
		}
		return conversionService;
	}

	@Bean
	@ConditionalOnMissingBean
	public Retryer feignRetryer() {
		return Retryer.NEVER_RETRY;
	}

	@Bean
	@ConditionalOnMissingBean(FeignLoggerFactory.class)
	public FeignLoggerFactory feignLoggerFactory() {
		return new DefaultFeignLoggerFactory(logger);
	}

	@Bean
	@ConditionalOnMissingBean(FeignClientConfigurer.class)
	public FeignClientConfigurer feignClientConfigurer() {
		return new FeignClientConfigurer() {
		};
	}

	private Encoder springEncoder(ObjectProvider<AbstractFormWriter> formWriterProvider,
			FeignEncoderProperties encoderProperties, ObjectProvider<HttpMessageConverterCustomizer> customizers) {
		AbstractFormWriter formWriter = formWriterProvider.getIfAvailable();

		if (formWriter != null) {
			return new SpringEncoder(new SpringPojoFormEncoder(formWriter), messageConverters, encoderProperties,
					customizers);
		}
		else {
			return new SpringEncoder(new SpringFormEncoder(), messageConverters, encoderProperties, customizers);
		}
	}

	private class SpringPojoFormEncoder extends SpringFormEncoder {

		SpringPojoFormEncoder(AbstractFormWriter formWriter) {
			super();

			MultipartFormContentProcessor processor = (MultipartFormContentProcessor) getContentProcessor(MULTIPART);
			processor.addFirstWriter(formWriter);
		}

	}

	@Configuration(proxyBeanMethods = false)
	@Conditional(FeignCircuitBreakerDisabledConditions.class)
	protected static class DefaultFeignBuilderConfiguration {

		@Bean
		@Scope("prototype")
		@ConditionalOnMissingBean
		public Feign.Builder feignBuilder(Retryer retryer) {
			return Feign.builder().retryer(retryer);
		}

	}

	@Configuration(proxyBeanMethods = false)
	@ConditionalOnClass(CircuitBreaker.class)
	@ConditionalOnProperty("feign.circuitbreaker.enabled")
	protected static class CircuitBreakerPresentFeignBuilderConfiguration {

		@Bean
		@Scope("prototype")
		@ConditionalOnMissingBean({ Feign.Builder.class, CircuitBreakerFactory.class })
		public Feign.Builder defaultFeignBuilder(Retryer retryer) {
			return Feign.builder().retryer(retryer);
		}

		@Bean
		@Scope("prototype")
		@ConditionalOnMissingBean
		@ConditionalOnBean(CircuitBreakerFactory.class)
		public Feign.Builder circuitBreakerFeignBuilder() {
			return FeignCircuitBreaker.builder();
		}

	}

	@Configuration(proxyBeanMethods = false)
	@ConditionalOnBean(type = "io.micrometer.core.instrument.MeterRegistry")
	@ConditionalOnClass(name = "feign.micrometer.MicrometerCapability")
	@ConditionalOnProperty(name = "feign.metrics.enabled", matchIfMissing = true)
	@Conditional(FeignClientMetricsEnabledCondition.class)
	protected static class MetricsConfiguration {

		@Bean
		@ConditionalOnMissingBean
		public MicrometerCapability micrometerCapability(MeterRegistry meterRegistry) {
			return new MicrometerCapability(meterRegistry);
		}

	}

}

  

标签:feign,return,springboot,new,springframework,bean,org,import,Config
From: https://www.cnblogs.com/klm-kain/p/17081681.html

相关文章

  • Spring-IOC/DI配置管理第三方bean
    Spring-IOC/DI配置管理第三方bean1,IOC/DI配置管理第三方bean1.1案例:数据源对象管理本次案例将使用咱们前面提到过的数据源Druid(德鲁伊)和C3P0来配置学习下。1.1.1......
  • 初次使用 WebSocket -springboot 集成
    参考自:SpringBoot集成websocket_清泉影月的博客-CSDN博客,WebSocket中利用service层交互数据库_戒烟的李白的博客-CSDN博客_websocketservice核心依赖<dependency>......
  • SpringBoot2.0版本与老版本区别
    SpringBoot2.0版本新特性 以Java8为基准SpringBoot2.0要求Java版本必须8以上,Java6和7不再支持。内嵌容器包结构调整为了支持reactive使用场景,内嵌的容器包结构......
  • 实例化bean的四种方法
    方法一:用构造方法实例化bean提供可靠的构造方法,不写默认调用无参构造方法//配置<beanid="bookDao"class="org.wcx.dao.impl.BookDaoImpl"/> 方法二:用静态工厂实......
  • SpringBoot实现热部署
    在项目开发的过程中,会频繁修改类的代码,导致需要重新编译、启动,影响开发效率。SpringBoot提供了spring-boot-devtools组件,使代码改动后,无须手动重启SpringBoot应用即可重......
  • SpringBoot设置日志级别
    输出到控制台logging:#日志记录到文件中file:#指定文件名name:server.loglogback:rollingpolicy:#指定文件大小max-file-size......
  • vue+springboot集成钉钉扫码登录
    具体接入方式可参考官方介绍:https://open.dingtalk.com/document/orgapp-server/tutorial-obtaining-user-personal-information本文只演示vue+springboot如何实现钉钉扫......
  • SpringBoot 开发抖音开放平台获取用户的粉丝统计和短视频数据
    大家好,我是小悟​抖音开放平台刚面世不久,资料比较少。即使对于一个开发人员来说,接入第三方接口都大同小异,不会太难,但我还是想把这些记录下来,特别是遇到的坑,会列在下面,一起参......
  • SpringBoot项目前端使用vue下载后端resources下的文件(导出模板)
     放到resources/xls/import-template.xls这个路径可以自己修改代码同步修改即可 @GetMapping(value="/downloadXls")@ApiOperation(value="下载导入模板"......
  • SpringBoot 整合 JDBC 实例
     0、数据库表CREATEDATABASEspringboot;USEspringboot;CREATETABLE`user`(`id`int(11)NOTNULLAUTO_INCREMENTCOMMENT'主键',`name`varchar(50)D......