WebMvcConfigurerAdapter已经过时,在新版本2.x中被废弃,原因是springboot2.0以后,引用的是spring5.0,而spring5.0取消了WebMvcConfigurerAdapter 

以下WebMvcConfigurerAdapter 比较常用的重写接口

/** 解决跨域问题 **/
public void addCorsMappings(CorsRegistry registry) ;
/** 添加拦截器 **/
void addInterceptors(InterceptorRegistry registry);
/** 这里配置视图解析器 **/
void configureViewResolvers(ViewResolverRegistry registry);
/** 配置内容裁决的一些选项 **/
void configureContentNegotiation(ContentNegotiationConfigurer configurer);
/** 视图跳转控制器 **/
void addViewControllers(ViewControllerRegistry registry);
/** 静态资源处理 **/
void addResourceHandlers(ResourceHandlerRegistry registry);
/** 默认静态资源处理器 **/
void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);

新的版本解决方案目前有两种
方案1 直接实现WebMvcConfigurer  (推荐)

* <p>{@code @EnableWebMvc}-annotated configuration classes may implement
* this interface to be called back and given a chance to customize the
* default configuration. --默认配置
@Configuration
public class MyWebMvcConfg implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/index").setViewName("index");
}
}

方案2 直接继承WebMvcConfigurationSupport

继承WebMvcConfigurationSupport类,在扩展的类中重写父类的方法即可,但这种方式会有问题的,这种方式会屏蔽Spring Boot的@EnableAutoConfiguration中的设置。这时候启动项目时会发现映射根本没有加载成功,读取不到静态的资源也就是说application.properties中添加配置的映射配置没有起作用,然后我们会想到重写来进行重新映射

@Configuration
public class MyMvcConfig extends WebMvcConfigurationSupport{ @Bean
public WebMvcConfigurationSupport webMvcConfigurationSupport(){
WebMvcConfigurationSupport support = new WebMvcConfigurationSupport(){
@Override
protected void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/main.html").setViewName("dashboard");
// registry.addViewController("/login.html").setViewName("login");
} @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//registry.addResourceHandler("/resources/static/**").addResourceLocations("classpath:/static/");
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/resources/static/");
super.addResourceHandlers(registry);
}
};
return support;
}

或是

@Configuration
public class MyMvcConfig extends WebMvcConfigurationSupport { @Override
protected void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/main.html").setViewName("dashboard");
// registry.addViewController("/login.html").setViewName("login");
}
}

WebMvcConfigurerAdapter已过时,替换接口或类的更多相关文章

  1. WebMvcConfigurerAdapter过时替换接口或类

    (注意!)WebMvcConfigurerAdapter 在spring 5.0中已经弃用了. 原来的使用方式 @Deprecated public abstract class WebMvcConf ...

  2. WebMvcConfigurerAdapter已过时

    Spring Boot2.0的版本(创建的时候自动选择的这个版本),然后编译器告诉我WebMvcConfigurerAdapter已过时了 @Deprecated public abstract cl ...

  3. SpringBoot从1.5.1→2.2.4项目加包扫雷三:org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter已过时

    @Configuration@Slf4j@PropertySource({"classpath:/config.properties"})public class MyWebApp ...

  4. java 多线程:Thread类常用方法:setPriority优先级、interrupt中断标记、suspend暂停与唤醒resume(已过时);daemon守护线程

    常用方法: boolean isAlive() 测试此线程是否存活. boolean isDaemon() 测试此线程是否为守护程序线程. static void sleep?(long millis ...

  5. Servlet API遍程常用接口和类

    本文主要总结Servlet  API遍程常用接口和类 Servlet API http://tomcat.apache.org/tomcat-5.5-doc/servletapi/index.html ...

  6. JAVA中的数据结构——集合类(线性表:Vector、Stack、LinkedList、set接口;键值对:Hashtable、Map接口<HashMap类、TreeMap类>)

    Java的集合可以分为两种,第一种是以数组为代表的线性表,基类是Collection:第二种是以Hashtable为代表的键值对. ... 线性表,基类是Collection: 数组类: person ...

  7. Servlet基本用法(二)接口和类

    一.摘要 本文主要简单介绍开发Servlet需要用到的接口和类. 二.ServletRequest和ServletResponse接口 当客户请求到来时,由容器创建一个ServletRequest对象 ...

  8. 常见的接口与类 -- Comparator

    接口Comparator 1.1 前面我们讲过Java提供了一个用于比较的接口Comparable,提供了一个比较的方法,所有实现该接口的类,都动态的实现了该比较方法.实际上Java中除了比较一个接口 ...

  9. Servlet基本用法二接口和类

    转自:http://www.cnblogs.com/xujian2014/p/4536168.html 一.摘要 本文主要简单介绍开发Servlet需要用到的接口和类. 二.ServletReques ...

随机推荐

  1. Java使用HTTPClient3.0.1开发的公众平台消息模板的推送功能

    package com.company.product.manager.busniess.impl; import java.io.IOException;import java.nio.charse ...

  2. Oracle 通过触发器实现ID自增

    Oracle不像Mysql,SQLServer能够直接设置ID自增,但是可以通过触发器实现ID自增. 1 创建测试表 create table t_goods(id number primary ke ...

  3. [自学]Docker system 命令 查看docker镜像磁盘占用情况 Docker volume 相关

    内容From https://docs.docker.com/engine/reference/commandline/system_df/ docker的image和docker的container ...

  4. 好文章之——PHP系列(一)

    注:最近实习的公司是一家做电商企业,后台主要是php开发,好久不怎么接触php的我看了几篇相关文章,提高下对它的认识与理解,发现里面的学习思路还是非常好的,当然也会重新拾一下基础知识啦! 其实自己心中 ...

  5. 转《js闭包与内存泄漏》

    首先,能导致内存泄漏的一定是引用类型的变量,比如函数和其他自定义对象.而值类型的变量是不存在内存泄漏的,比如字符串.数字.布尔值等.因为值类型是靠复制来传递的,而引用类型是靠类似c语言中的指针来传递的 ...

  6. HashMap的实现原理--链表散列

    1.    HashMap概述 HashMap是基于哈希表的Map接口的非同步实现.此实现提供所有可选的映射操作,并允许使用null值和null键.此类不保证映射的顺序,特别是它不保证该顺序恒久不变. ...

  7. Database testing test scenarios

    1 check if correct data is getting saved is database upon successful page submit2 check values for c ...

  8. ImportError: cannot import name descriptor_pb2

    重新编译protobuf 下载地址:https://github.com/google/protobuf $cd /path/protobuf/python $python setup.py buil ...

  9. if --else的注意点

  10. BZOJ3881[Coci2015]Divljak——AC自动机+树状数组+LCA+dfs序+树链的并

    题目描述 Alice有n个字符串S_1,S_2...S_n,Bob有一个字符串集合T,一开始集合是空的. 接下来会发生q个操作,操作有两种形式: “1 P”,Bob往自己的集合里添加了一个字符串P. ...