繁体   English   中英

获取 NoSuchBeanDefinitionException:在 Spring WebFlux 中没有 ServerRequest 类型的合格 bean

[英]Getting NoSuchBeanDefinitionException: No qualifying bean of type ServerRequest in Spring WebFlux

由于我仍然没有为我的主题找到一个完全令人满意的反应式解决方案: click ,主要假设是:

How to run a Web Flux method cyclically in a reactive way?

我找到了一个使用@Scheduled注释使其成为现实的解决方法。 下面的实现:

@Component
@AllArgsConstructor
public class Covid19APIHandler {

  private Covid19APIService apiService;

  private CountryCasesHistoryRepository repository;

  private CountryCasesWrapperRepository countryCasesWrapperRepository;

  private ServerRequest serverRequest;

  public Mono<Void> getCountryCasesAndSave(ServerRequest serverRequest) {
    return apiService
        .findCasesByCountry()
        .flatMap(
            wrapper ->
                countryCasesWrapperRepository
                    .save(
                        CountryCasesWrapper.builder()
                            .countries_stat(wrapper.getCountries_stat())
                            .statistic_taken_at(wrapper.getStatistic_taken_at())
                            .build())
                    .then(Mono.empty()));
  }

  @Scheduled(fixedDelay = 10000)
  public void casesByCountryScheduled() {
    getCountryCasesAndSave(serverRequest);
  }
}

问题是在执行代码时我收到一个错误:

Description:

Parameter 3 of constructor in com.covid.application.Covid19APIHandler required a bean of type 'org.springframework.web.reactive.function.server.ServerRequest' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.web.reactive.function.server.ServerRequest' in your configuration.

我用@RequiredArgsConstructor尝试了final关键字,生成了所有参数。 通过IntelliJ构造函数,但我的ServerRequest字段仍未初始化。 问题来了,如何创建我的ServerRequest自定义 bean 并使其正确初始化。 我将不胜感激有关如何使其成为现实的建议。

正如评论中所指出的,为什么你甚至需要一个ServerRequest ,你甚至没有使用它。 删除它并清理代码。

@Component
public class Covid19APIHandler {

    private Covid19APIService apiService;
    private CountryCasesWrapperRepository countryCasesWrapperRepository;

    @Autowire
    public Covid19APIHandler(Covid19APIService apiService, CountryCasesHistoryRepository repository) {
        this.apiService = apiService;
        this.repository = repository;
    }

    @Scheduled(fixedDelay = 10000)
    public void casesByCountryScheduled() {
        apiService.findCasesByCountry()
            .flatMap(response ->
                return countryCasesWrapperRepository.save(
                    CountryCasesWrapper.builder()
                        .countries_stat(response.getCountries_stat())
                        .statistic_taken_at(response.getStatistic_taken_at())
                        .build()))
        .subscribe();
    }
}

在此代码中,如果您希望它每小时运行一次,您的计划任务将按照fixedDelay运行,我建议在调度程序中设置一个cron作业。

由于subscribe ,代码将运行。 您会看到,当您调用subscribe时,您基本上是在运行代码。 consumer应该始终是subscriber 您的应用程序是另一个应用程序的consumer 因此,您的服务通过调用 subscribe 发起请求,这将启动发出请求并将其存储在数据库中的流程。

我建议阅读以下内容

使用@Scheduled的 cron 作业

https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/scheduling.html#scheduling-annotation-support-scheduled

cron 语法

https://en.wikipedia.org/wiki/Cron

在您订阅之前什么都不会发生(反应堆文档)

https://projectreactor.io/docs/core/release/reference/#reactive.subscribe

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM