SpringBoot2.0学习笔记:(七) Spring Boot弃用的WebMvcConfigurerAdapter

版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/liujun03/article/details/82779358

我们都知道,在Spring Boot程序引入Web模块之后,会自动进行一系列有关Spring Mvc的配置,其自动配置类为:org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration

在这个类中,Spring Boot给我们配置好了视图解析器、静态资源、消息转换器、区域信息解析器、首页、欢迎页等等内容。

这里并不分析WebMvcAutoConfiguration这个类,要说的是WebMvcConfigurerAdapter这个适配类。

Spring Boot并不仅仅给我们自动配置好了关于Spring MVC的配置,而且为了方便的进行扩展,还给我们提供了

WebMvcConfigurerAdapter这个适配类,只需要我们自己实现了这个类,就可以自定义比如视图解析器、区域信息解析器这些组件。

在Spring Boot2.0版本中,WebMvcConfigurerAdapter这个类被弃用了。

@Deprecated
public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {

那么我们如何来扩展关于MVC的配置呢?

1.继承WebMvcConfigurationSupport

仔细看一下WebMvcConfigurationSupport这个类的话,可以发现其中有很多add…方法:

/**
* Override this method to add Spring MVC interceptors for
* pre- and post-processing of controller invocation.
* @see InterceptorRegistry
 */
protected void addInterceptors(InterceptorRegistry registry) {
}

/**
 * Override this method to add view controllers.
 * @see ViewControllerRegistry
*/
protected void addViewControllers(ViewControllerRegistry registry) {
}

......

继承WebMvcConfigurationSupport之后,可以使用这些add…方法添加自定义的拦截器、试图解析器等等这些组件。如下:

@Configuration
public class NativeMvcConfig extends WebMvcConfigurationSupport {

    @Override
    protected void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/login.html").setViewName("login");
    }
}

但这里有个问题就是,当你继承了WebMvcConfigurationSupport并将之注册到容器中之后,Spring Boot有关MVC的自动配置就不生效了。

可以看一下Spring Boot启动WebMVC自动配置的条件:

@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
		ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {

其中一个条件就是**@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)**,只有当容器中没有WebMvcConfigurationSupport这个类型的组件的时候,才会启动自动配置。

所以当我们继承WebMvcConfigurationSupport之后,除非你自己对代码把控的相当的好,在继承类中重写了一系列有关WebMVC的配置,否则可能就会遇到静态资源访问不到,返回数据不成功这些一系列问题了。

2. 实现WebMvcConfigurer接口

我们知道,Spring Boot2.0是基于Java8的,Java8有个重大的改变就是接口中可以有default方法,而default方法是不需要强制实现的。上述的WebMvcConfigurerAdapter类就是实现了WebMvcConfigurer这个接口,所以我们不需要继承WebMvcConfigurerAdapter类,可以直接实现WebMvcConfigurer接口,用法与继承这个适配类是一样的。如:

@Configuration
public class WebMVCConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/login.html").setViewName("login");
    }

    @Bean
    public LocaleResolver localeResolver(){
        return new NativeLocaleResolver();
    }

    protected static class NativeLocaleResolver implements LocaleResolver{

        @Override
        public Locale resolveLocale(HttpServletRequest request) {
            String language = request.getParameter("language");
            Locale locale = Locale.getDefault();
            if(!StringUtils.isEmpty(language)){
                String[] split = language.split("_");
                locale = new Locale(split[0],split[1]);
            }
            return locale;
        }

        @Override
        public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

        }
    }
}

这两种方法都可以作为WebMVC的扩展,去自定义配置。区别就是继承WebMvcConfigurationSupport会使Spring Boot关于WebMVC的自动配置失效,需要自己去实现全部关于WebMVC的配置,而实现WebMvcConfigurer接口的话,Spring Boot的自动配置不会失效,可以有选择的实现关于WebMVC的配置。

猜你喜欢

转载自blog.csdn.net/liujun03/article/details/82779358
今日推荐