如何判断正在运行的是 Spring Web 堆栈,Spring WebFlux 还是 Spring MVC?
我们有许多通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
ConditionalOnWebApplication
注解根据应用程序类型创建 bean:You can use
ConditionalOnWebApplication
annotation to create a bean based on application type:你可以检查gradle文件。
如果它使用 spring-boot-starter-webflux,则堆栈是 webflux。
如果使用spring-boot-starter-web,则堆栈是mvc。
spring-boot-starter-web build.gradle
spring-boot-starter-webflux build.gradle
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
spring-boot-starter-webflux build.gradle
感谢 M. Deinum 提供了指向
--debug
的指针。最后,我设法使用
--debug
标志运行应用程序。最初对我来说,这导致了 JSON 消息的转储,我需要通过 jq '.["message"]' --raw-output 传递这些消息,以获得具有有用格式的内容。
然后,根据有根据的猜测,我会说这些是相当结论性的:
在 WebFlux 应用程序中:
在 WebMVC 应用程序中:
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:
In the WebMVC app: