如何判断正在运行的是 Spring Web 堆栈,Spring WebFlux 还是 Spring MVC?

发布于 2025-01-15 06:57:53 字数 559 浏览 0 评论 0原文

我们有许多通过 Gradle 构建的 Spring-Boot 应用程序,并且所有应用程序都具有略有不同的 spring 依赖项。

当尝试在两者之间复制代码时,我非常怀疑我遇到的问题是由于使用 Spring MVC 的捐赠者服务和使用 Spring WebFlux 的新服务造成的。

有没有一种“简单”的方法来检查哪个堆栈已加载?我看到文档暗示如果两者都存在,那么它将默认为 MVC,但由于缺乏深入研究所有依赖项,我不确定如何检查它。

我检查了日志,它没有说明正在使用哪个堆栈。

有没有办法查询运行时正在加载哪个堆栈(MVC 或 WebFlux)?可以在日志记录中启用某种东西,还是只能由其中之一加载的 Bean?

编辑:澄清一下 - 最初的回复谈论的是 spring-boot-starter-web 或 spring-boot-starter-webflux。就我而言,事实证明它是 spring-boot-starter-gateway (恰好适用于 WebFlux)。鉴于这种情况并不是立即明显的依赖关系,而且我不止一次看到人们导入两者,因为他们不知道更好,我实际上正在寻找一种方法来检查正在加载哪个堆栈运行时无需猜测依赖关系。

We have a number of Spring-Boot applications that are built via Gradle and all have subtly different spring dependencies.

When trying to copy code between the two of them, I am highly suspicious that I'm seeing problems due to the donor service using Spring MVC and the new service using Spring WebFlux.

Is there a "simple" way to check which stack has been loaded? I've seen documentation implying that if both are present then it'll default to MVC, but short of deep diving all the dependencies I'm not sure how to check that.

I've checked the logs, and it doesn't say anything about which stack is being used.

Is there a way to query which stack (MVC or WebFlux) is being loaded at runtime? Something which can be enabled in logging, or a Bean that will only be loaded by one or the other?

Edit: to clarify - the initial responses talk about spring-boot-starter-web or spring-boot-starter-webflux. In my case it turns out that it was spring-boot-starter-gateway (which happens to work on WebFlux). Given that this is a case where it isn't immediately obvious dependencies and I've more than once seen people import both because they didn't know any better, I'm actually looking for a way to check which stack is being loaded at run-time without having to guess from the dependencies.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请叫√我孤独 2025-01-22 06:57:53

您可以使用 ConditionalOnWebApplication 注解根据应用程序类型创建 bean:

@Bean
@ConditionalOnWebApplication(type = REACTIVE)
CommandLineRunner commandLineRunner() {
    return args -> log.info("Reactive app.");
}

You can use ConditionalOnWebApplication annotation to create a bean based on application type:

@Bean
@ConditionalOnWebApplication(type = REACTIVE)
CommandLineRunner commandLineRunner() {
    return args -> log.info("Reactive app.");
}
南街女流氓 2025-01-22 06:57:53

你可以检查gradle文件。
如果它使用 spring-boot-starter-webflux,则堆栈是 webflux。
如果使用spring-boot-starter-web,则堆栈是mvc。

spring-boot-starter-web build.gradle

plugins {
    id "org.springframework.boot.starter"
}

description = "Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container"

dependencies {
    api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter"))
    api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-json"))
    api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-tomcat"))
    api("org.springframework:spring-web")
    api("org.springframework:spring-webmvc")
}

spring-boot-starter-webflux build.gradle

plugins {
    id "org.springframework.boot.starter"
}

description = "Starter for building WebFlux applications using Spring Framework's Reactive Web support"

dependencies {
    api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter"))
    api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-json"))
    api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-reactor-netty"))
    api("org.springframework:spring-web")
    api("org.springframework:spring-webflux")
    api("org.synchronoss.cloud:nio-multipart-parser")
}

You can check the gradle file.
if it uses spring-boot-starter-webflux, the stack is webflux.
if it uses spring-boot-starter-web, the stack is mvc.

spring-boot-starter-web build.gradle

plugins {
    id "org.springframework.boot.starter"
}

description = "Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container"

dependencies {
    api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter"))
    api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-json"))
    api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-tomcat"))
    api("org.springframework:spring-web")
    api("org.springframework:spring-webmvc")
}

spring-boot-starter-webflux build.gradle

plugins {
    id "org.springframework.boot.starter"
}

description = "Starter for building WebFlux applications using Spring Framework's Reactive Web support"

dependencies {
    api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter"))
    api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-json"))
    api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-reactor-netty"))
    api("org.springframework:spring-web")
    api("org.springframework:spring-webflux")
    api("org.synchronoss.cloud:nio-multipart-parser")
}
负佳期 2025-01-22 06:57:53

感谢 M. Deinum 提供了指向 --debug 的指针。

最后,我设法使用 --debug 标志运行应用程序。

最初对我来说,这导致了 JSON 消息的转储,我需要通过 jq '.["message"]' --raw-output 传递这些消息,以获得具有有用格式的内容。

然后,根据有根据的猜测,我会说这些是相当结论性的:

在 WebFlux 应用程序中:

   WebFluxAutoConfiguration matched:
      - @ConditionalOnClass found required class 'org.springframework.web.reactive.config.WebFluxConfigurer' (OnClassCondition)
      - found ConfigurableReactiveWebEnvironment (OnWebApplicationCondition)
      - @ConditionalOnMissingBean (types: org.springframework.web.reactive.config.WebFluxConfigurationSupport; SearchStrategy: all) did not find any beans (OnBeanCondition)

   WebMvcAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'javax.servlet.Servlet' (OnClassCondition)

在 WebMVC 应用程序中:

   WebMvcAutoConfiguration matched:
      - @ConditionalOnClass found required classes 'javax.servlet.Servlet', 'org.springframework.web.servlet.DispatcherServlet', 'org.springframework.web.servlet.config.annotation.WebMvcConfigurer' (OnClassCondition)
      - found 'session' scope (OnWebApplicationCondition)
      - @ConditionalOnMissingBean (types: org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; SearchStrategy: all) did not find any beans (OnBeanCondition)

   WebFluxAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'org.springframework.web.reactive.config.WebFluxConfigurer' (OnClassCondition)

Thanks to M. Deinum for the pointer to --debug.

In the end I managed to run the application with the --debug flag.

Initially for me that resulted in a dump of JSON messages which I needed to pass through jq '.["message"]' --raw-output in order to get something with useful formatting.

Then, on an educated guess I'd say that these were pretty conclusive:

In the WebFlux app:

   WebFluxAutoConfiguration matched:
      - @ConditionalOnClass found required class 'org.springframework.web.reactive.config.WebFluxConfigurer' (OnClassCondition)
      - found ConfigurableReactiveWebEnvironment (OnWebApplicationCondition)
      - @ConditionalOnMissingBean (types: org.springframework.web.reactive.config.WebFluxConfigurationSupport; SearchStrategy: all) did not find any beans (OnBeanCondition)

   WebMvcAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'javax.servlet.Servlet' (OnClassCondition)

In the WebMVC app:

   WebMvcAutoConfiguration matched:
      - @ConditionalOnClass found required classes 'javax.servlet.Servlet', 'org.springframework.web.servlet.DispatcherServlet', 'org.springframework.web.servlet.config.annotation.WebMvcConfigurer' (OnClassCondition)
      - found 'session' scope (OnWebApplicationCondition)
      - @ConditionalOnMissingBean (types: org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; SearchStrategy: all) did not find any beans (OnBeanCondition)

   WebFluxAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'org.springframework.web.reactive.config.WebFluxConfigurer' (OnClassCondition)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文