创建名称为 'scopedTarget.contextHeadersBuilder 的 bean 时出错

Error creating bean with name 'scopedTarget.contextHeadersBuilder

我正在使用 Spring Boot 1.5.9 版本以及 Feign 和 Fallback。 通过 Feign 客户端调用单个服务时出现奇怪的错误。 请帮忙。

我的Feign配置

@Configuration
@Import(HeaderConverter.class)
public class SecuritiesFeignConfig {
    private final HeaderConverter headerConverter;

    public SecuritiesFeignConfig(HeaderConverter headerConverter) {
        this.headerConverter = headerConverter;
    }

    @Bean
    public RequestInterceptor requestInterceptor() {
        return requestTemplate -> headerConverter.getCallContextHeaders().forEach(requestTemplate::header);
    }

    @Bean
    public Decoder feignDecoder() {
        HttpMessageConverter<?> jacksonConverter = new MappingJackson2HttpMessageConverter(customObjectMapper());
        ObjectFactory<HttpMessageConverters> objectFactory = () -> new HttpMessageConverters(jacksonConverter);
        return new ResponseEntityDecoder(new SpringDecoder(objectFactory));
    }

    private ObjectMapper customObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true);
        objectMapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.registerModule(new JavaTimeModule());
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

        return objectMapper;
    }
}

和我在应用程序中的主要配置 class

@Configuration
@Import(FiltersConfig.class)
public class frameworkConfig {
    @Bean
    @Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
    public MSACallContextImpl callContext() {
        return new MSACallContextImpl();
    }

    @Bean
    public CallContextCreator callContextCreator() {
        return new CallContextCreator();
    }

    @Bean
    @Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
    public ContextHeadersBuilder contextHeadersBuilder(MSACallContextImpl callContextImpl) {
        return new ContextHeadersBuilder(callContextImpl);
    }

    @Bean
    public RequestContextListener requestContextListener() {
        return new RequestContextListener();
    }
}

但是当我尝试通过 Feign 客户端进行服务调用时,出现以下错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.contextHeadersBuilder': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

我找到了当前问题的解决方案。 需要添加到应用程序属性文件

hystrix:
  command:
    default:
      execution:
        timeout:
          enabled: false
        isolation:
          thread:
            timeoutInMilliseconds: 6000
          strategy: SEMAPHORE

似乎没有创建 Bean,并且 @Autowired 无法创建 OBJECT,所以请尝试 当我将@Service Annotation 放在Class 级别

时,@Service Annotation 将丢失,因为它对我有用