java - WebMvcAutoConfiguration 未激活

标签 java spring spring-mvc spring-boot

我有一个相当简单的 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/

相关文章:

java - 为什么我的项目需要使用 JavaConfig 的 Spring Security 的 applicationContext.xml

java - Spring MVC - 来自过滤器的内容类型感知响应

java - 在 Java 中通过构造函数传递一个对象?

java - 如何知道方法是否使用反射将数组作为参数

java - 如何从java中的方法重定向到jsp?

java - Spring集成+Spring AMQP : How can I passing MessageProperties to int:service-activator?

java - 将 spring-data-rest 添加到 gs-rest-service 示例项目后出现依赖问题

java - 如何将自定义编码文件转换为 UTF-8(在 Java 中或使用专用工具)

java - 如何 hibernate 批量插入实时数据?使用还是不使用@Transactional?

java - Spring security 登录后如何跳转到另一个页面