我有一个相当简单的 spring-boot 应用程序,在调试缓存破坏资源 URL 生成的问题时发现 WebMvcAutoConfiguration 在应用程序启动时没有被触发。
以下是相关日志输出:
WebMvcAutoConfiguration:
Did not match:
- @ConditionalOnMissingBean (types: org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; SearchStrategy: all) found beans of type 'org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport' org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration (OnBeanCondition)
Matched:
- @ConditionalOnClass found required classes 'javax.servlet.Servlet', 'org.springframework.web.servlet.DispatcherServlet', 'org.springframework.web.servlet.config.annotation.WebMvcConfigurer'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
- found ConfigurableWebEnvironment (OnWebApplicationCondition)
经过几个小时的调试并将空应用程序的行为与我的进行比较后,我发现的唯一区别是我的应用程序中有更多的 MvcConfigurer 实例。即:
- 我的自定义 WebMvcConfigurer(用于 cors 等)
- Spring-JPA 配置器
- Spring 数据配置器
我认为这些东西能够并存,但显然还有其他东西在起作用。
现在我不知所措,因为我花了几个小时调试这个问题。
我的问题是如何找到禁用 WebMvcAutoConfiguration 的缺失 DelegatingWebMvcConfiguration bean 的来源?
最佳答案
我只是在 SpringBoot 2.1.3.RELEASE 中遇到这个问题,它在 anwser 之前很长时间,但也许可以帮助其他人。
解决方案只是删除@EnableWebMvc。
如果您查看 EnableWebMvc 类,它会直接导入 DelegatingWebMvcConfiguration,因此当 WebMvcAutoConfiguration 验证条件时,它会找到 DelegatingWebMvcConfiguration beans。
如果没有 EnableWebMvc,则采用 WebMvcAutoConfiguration 并且它具有静态配置:
@Configuration
public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration
也许这会产生其他影响,但我刚刚开始我的项目,无法验证。
关于java - WebMvcAutoConfiguration 未激活,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52646861/