zoukankan      html  css  js  c++  java
  • springboot关于webmvc配置问题记录

    在之前的文章(springboot配置静态资源访问路径)中说过,springboot默认的加载静态资源的地方是在resources目录下的static文件夹下,其实除了resources目录下得static文件夹可以被访问,在resources目录下创建resources文件夹、public文件夹、META-INF/resources文件夹都是可以被访问到的,只不过springboot默认推荐我们使用static文件夹,而且查找的优先级是META-INF/resources》public》resources》static。

    当我们要修改springboot默认的静态资源加载路径的时候,我们可以直接在配置文件properties、yml中直接设置,或者找个配置类(使用@configuration注解的类)使其继承org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter或者org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport并重写addResourceHandlers(ResourceHandlerRegistry registry)方法,这个方法在WebMvcConfigurerAdapter和WebMvcConfigurationSupport中都是空实现。

    在此,建议在springboot 1.x版本使用WebMvcConfigurerAdapter这个类,2.x版本实现WebMvcConfigurer 接口,不推荐使用WebMvcConfigurationSupport和@EnableWebMvc注解。因为springboot默认会给我们添加一个配置类WebMvcAutoConfiguration,但是这个配置类的加载是有条件的( @ConditionalOnMissingBean(WebMvcConfigurationSupport.class)),只有缺少WebMvcConfigurationSupport配置类才会生效,当我们使用继承WebMvcConfigurationSupport或者@EnableWebMvc注解(这个注解会导入DelegatingWebMvcConfiguration,这个类继承自WebMvcConfigurationSupport

    )时,springboot的就不会帮我们加载WebMvcAutoConfiguration这个配置类了,这个时候就需要我们进行一些webmvc的配置。而且比较坑的是如果我们使用继承WebMvcConfigurationSupport这种方式,不能多次使用,也就是说如果我们有两个及以上配置类继承WebMvcConfigurationSupport时,只会有一个生效;当我们即使用@EnableWebMvc又有配置类继承WebMvcConfigurationSupport时,我们的配置类是会生效的。

    @EnableWebMvc、WebMvcConfigurationSupport、WebMvcConfigurerAdapter这三个使用效果:

    1. 使用WebMvcConfigurerAdapter  =====》全部生效
    2. 使用WebMvcConfigurationSupport(多个)   ====》某一个生效,其他不生效
    3. 使用WebMvcConfigurerAdapter + WebMvcConfigurationSupport       ====》WebMvcConfigurationSupport生效
    4. 使用@EnableWebMvc+ WebMvcConfigurationSupport       ====》WebMvcConfigurationSupport生效
    5. 使用@EnableWebMvc+ WebMvcConfigurerAdapter     ====》WebMvcConfigurerAdapter 不生效
  • 相关阅读:
    PostgreSQL数据库中的常见错误
    postgresql相关命令
    Linux系统查看公网IP地址
    TCP/IP TIME_WAIT状态原理
    TCP连接状态详解及TIME_WAIT过多的解决方法
    让你提升命令行效率的 Bash 快捷键 [完整版]
    linux 如何显示一个文件的某几行(中间几行)
    linux中内核的一个不错的参数somaxconn
    Linux crontab 实现每秒执行
    Linux tar This does not look like a tar archive
  • 原文地址:https://www.cnblogs.com/zzw-blog/p/11457417.html
Copyright © 2011-2022 走看看