【问题标题】:what is the difference between using @EnableWebMvc + WebMvcConfigurer and WebMvcConfigurer?使用@EnableWebMvc + WebMvcConfigurer 和 WebMvcConfigurer 有什么区别?
【发布时间】:2023-02-05 23:23:24
【问题描述】:

使用@EnableWebMvc + WebMvcConfigurer 和 WebMvcConfigurer 有什么区别??

@Configuration
@EnableWebMvc
class WebMvcConfig(): WebMvcConfigurer {}
@Configuration
class WebMvcConfig(): WebMvcConfigurer {}

【问题讨论】:

  • 第一个使用默认值启用 web-mvc,第二个依赖于 DispatcherServlet 中的基本设置。如果您在 spring boot 应用程序中使用 @EnableWebMvc,它将禁用大部分与 Web 相关的功能的自动配置。
  • @M.Deinum 感谢 deinum!现在我明白了。我可以再问一个问题吗?如何禁用tomcat错误。我的意思是..错误来自像这样的过滤器Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception,我真的很想禁用打印错误!!

标签: spring


【解决方案1】:
@Configuration
class WebMvcConfig(): WebMvcConfigurer {}

这只有在使用@EnableWebMvc 时才会生效。 WebMvcConfigurer 实例由通过@EnableWebMvc 注册的DelegatingWebMvcConfiguration 实例检测。所以要让它工作,你需要@EnableWebMvc

笔记:使用 Spring Boot 时,当 Spring Boot 在类路径上检测到 Spring MVC 类时,会自动完成此操作!

没有@EnableWebMvc WebMvcConfigurer 除了占用内存外什么都不做。当不使用 @EnableWebMvc 时,DispatcherServlet 将安装一些默认值。这些默认值硬编码在DispatcherServlet.properties 文件中。如果不使用@EnableWebMvc,这些默认值很难修改。

警告:当使用 Spring Boot 并添加 @EnableWebMvc 时,实际上会禁用 Spring Boot 完成的大部分 MVC 自动配置,这可能会导致其他意外!

【讨论】:

    猜你喜欢
    • 2021-11-08
    • 2019-12-07
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 1970-01-01
    • 2019-10-18
    相关资源
    最近更新 更多