【问题标题】:Error creating bean with name 'scopedTarget.contextHeadersBuilder创建名称为“scopedTarget.contextHeadersBuilder”的 bean 时出错
【发布时间】:2020-04-16 07:45:08
【问题描述】:

我正在使用 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;
    }
}

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

@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.

【问题讨论】:

  • 你正在使用一个你不应该使用的范围。
  • 对不起,你的意思是:@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)?
  • 您正在使用ContextHeadersBuilder 的 bean 由不在请求范围内的组件使用。因此,mkaing 这个 bean 请求作用域是行不通的。为什么首先需要请求范围?
  • 如果我将“request”替换为“singleton” - 它对我有用。但在这种情况下,我们失去了上下文 =(

标签: java spring spring-boot spring-cloud-feign feign


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    似乎 Bean 创建没有发生,@Autowired 无法创建 OBJECT,所以请尝试 当我将@Service Annotation 放在类级别时,@Service Annotation 将丢失,因为它对我有用

    【讨论】:

      猜你喜欢
      • 2017-06-05
      • 2017-02-26
      • 2015-02-23
      • 2022-07-21
      • 2020-02-07
      • 2018-12-17
      • 2020-07-24
      • 2011-10-09
      • 2018-01-31
      相关资源
      最近更新 更多