日常解决bug-springboot webMvcConfig 之代替自动配置

矮袋鼠

在SpringBoot1.X的版本中,我们可以继承自WebMvcConfigurerAdapter,覆盖想要实现的方法即可,但是在新版中建议停用。

spring boot2.0之后在构造spring配置文件时建议推荐直接实现WebMvcConfigurer或者直接继承WebMvcConfigurationSupport ,经测试实现WebMvcConfigurer是没问题,但继承WebMvcConfigurationSupport类是会导致自动配置失效的。

针对上述问题提供两种解决办法:

第一种:使用implements WebMvcConfigurer(推荐)
/**
 * web 配置
 * iukcy
 * 2021-02-23 14:45:53
 * 备注:会自动引入默认实现
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    static final String ORIGINS[] = new String[]{"GET", "POST", "PUT", "DELETE"};

    //静态资源映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/static/img/")
                .addResourceLocations("file:/Users/zhangzhufu/upload/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
    }

    // AJAX请求跨域
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/remote/**").allowedOrigins("*").allowCredentials(true).allowedMethods(ORIGINS).maxAge(3600);
    }
第二种:使用extends WebMvcConfigurationSupport
/**
 * web 配置
 * zhangzhufu
 * 2021-02-23 14:45:53
 * 备注:继承WebMvcConfigurationSupport 并不会将自动实现自动引入过来, 
 * 需要全部重写
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    static final String ORIGINS[] = new String[]{"GET", "POST", "PUT", "DELETE"};

    //静态资源映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/static/img/")
                .addResourceLocations("file:/Users/zhangzhufu/upload/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
    }

    // AJAX请求跨域
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/remote/**").allowedOrigins("*").allowCredentials(true).allowedMethods(ORIGINS).maxAge(3600);
    }
}
两者区别:(@ConditionalOnClass和@ConditionalOnMissingBean用法和区别)
@Configuration(
    proxyBeanMethods = false
)
@ConditionalOnWebApplication(
    type = Type.SERVLET
)
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {
    public static final String DEFAULT_PREFIX = "";
    public static final String DEFAULT_SUFFIX = "";
    private static final String[] SERVLET_LOCATIONS = new String[]{"/"};

    public WebMvcAutoConfiguration() {
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容