我們有許多通過 Gradle 構建的 Spring-Boot 應用程式,它們都具有細微不同的 spring 依賴項。
在嘗試在兩者之間復制代碼時,我非常懷疑我看到的問題是由于使用 Spring MVC 的捐助者服務和使用 Spring WebFlux 的新服務。
是否有一種“簡單”的方法來檢查已加載哪個堆疊?我已經看到檔案暗示如果兩者都存在,那么它將默認為 MVC,但沒有深入研究所有依賴項,我不確定如何檢查。
我檢查了日志,它沒有說明正在使用哪個堆疊。
有沒有辦法查詢在運行時正在加載哪個堆疊(MVC 或 WebFlux)?可以在日志記錄中啟用的東西,或者只能由一個或另一個加載的 Bean?
編輯:澄清 - 最初的回應談論spring-boot-starter-webor spring-boot-starter-webflux。在我的情況下,事實證明它是spring-boot-starter-gateway(這恰好適用于 WebFlux)。鑒于這種情況不是立即明顯的依賴關系,而且我不止一次看到人們匯入兩者,因為他們不知道更好,我實際上正在尋找一種方法來檢查哪個堆疊正在加載運行時,而不必從依賴項中猜測。
uj5u.com熱心網友回復:
您可以檢查 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")
}
uj5u.com熱心網友回復:
感謝 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)
uj5u.com熱心網友回復:
您可以使用ConditionalOnWebApplication注解根據應用程式型別創建 bean:
@Bean
@ConditionalOnWebApplication(type = REACTIVE)
CommandLineRunner commandLineRunner() {
return args -> log.info("Reactive app.");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/450854.html
標籤:爪哇 春天 弹簧靴 弹簧MVC spring-webflux
