WebMvcConfigurerAdapter已过时,替换接口或类
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已过时,替换接口或类的更多相关文章
- WebMvcConfigurerAdapter过时替换接口或类
(注意!)WebMvcConfigurerAdapter 在spring 5.0中已经弃用了. 原来的使用方式 @Deprecated public abstract class WebMvcConf ...
- WebMvcConfigurerAdapter已过时
Spring Boot2.0的版本(创建的时候自动选择的这个版本),然后编译器告诉我WebMvcConfigurerAdapter已过时了 @Deprecated public abstract cl ...
- SpringBoot从1.5.1→2.2.4项目加包扫雷三:org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter已过时
@Configuration@Slf4j@PropertySource({"classpath:/config.properties"})public class MyWebApp ...
- java 多线程:Thread类常用方法:setPriority优先级、interrupt中断标记、suspend暂停与唤醒resume(已过时);daemon守护线程
常用方法: boolean isAlive() 测试此线程是否存活. boolean isDaemon() 测试此线程是否为守护程序线程. static void sleep?(long millis ...
- Servlet API遍程常用接口和类
本文主要总结Servlet API遍程常用接口和类 Servlet API http://tomcat.apache.org/tomcat-5.5-doc/servletapi/index.html ...
- JAVA中的数据结构——集合类(线性表:Vector、Stack、LinkedList、set接口;键值对:Hashtable、Map接口<HashMap类、TreeMap类>)
Java的集合可以分为两种,第一种是以数组为代表的线性表,基类是Collection:第二种是以Hashtable为代表的键值对. ... 线性表,基类是Collection: 数组类: person ...
- Servlet基本用法(二)接口和类
一.摘要 本文主要简单介绍开发Servlet需要用到的接口和类. 二.ServletRequest和ServletResponse接口 当客户请求到来时,由容器创建一个ServletRequest对象 ...
- 常见的接口与类 -- Comparator
接口Comparator 1.1 前面我们讲过Java提供了一个用于比较的接口Comparable,提供了一个比较的方法,所有实现该接口的类,都动态的实现了该比较方法.实际上Java中除了比较一个接口 ...
- Servlet基本用法二接口和类
转自:http://www.cnblogs.com/xujian2014/p/4536168.html 一.摘要 本文主要简单介绍开发Servlet需要用到的接口和类. 二.ServletReques ...
随机推荐
- [转帖]Tomcat目录结构详解
Tomcat目录结构详解 https://www.cnblogs.com/veggiegfei/p/8474484.html 之前应该是知道一点 但是没有这么系统 感谢原作者的描述. 1.bin: 该 ...
- U 盘安装 CentOS的方法
1. 刻录U盘 我使用的工具是 UltralISO 2. 打开ISO 3. 使用<启动>-<写入磁盘映像> 根据U盘的性能 可能好事 5min-15min左右 4.找到想要安装 ...
- [工作相关] GS产品使用LInux下Oracle数据库以及ASM存储时的数据文件路径写法.
1. 自从公司的GS5版本就已经支持Linux下的oracle数据库通过安装工具自动安装注册了, 只不过路径需要使用linux的命名规则, 如图: /home/oracle/ 注意 最后是有一个 斜线 ...
- centos安装图形化界面
用下面命令查看查看是哪个模式 systemctl get-default 开机启动图形界面 systemctl set-default graphical.target(图形界面模式) reboot( ...
- Bootstrap缩略图
前面的话 缩略图在网站中最常用的地方就是产品列表页面,一行显示几张图片,有的在图片底部(左侧或右侧)带有标题.描述等信息.Bootstrap框架将这一部独立成一个模块组件,本文将详细介绍Bootstr ...
- maven 父工程 消除重复 对子模块进行管理 主要继承依赖
子类继承父类的 可以不需要groupid与versionid
- UVALive5874 - Social Holidaying-二分图匹配/匈牙利算法
有n个家庭,m个房间,一个房间只能两个家庭住.求最大匹配. 比较标准的二分图问题.先初始化把可能的家庭建边,然后跑一边匈牙利算法. 最后的答案是最大匹配数/2,因为建图时有重复. #include & ...
- day30 hashlib模块
hashlib 提供摘要算法 最常见的就是MD5,当然一般来说MD5已经被足够了 不管算法多不一样,但是摘要的功能是不变的 对于相同的字符串进行摘要,使用同一个算法得到的值总是不变的 不同算法的话,会 ...
- day14 装饰器模拟验证附加功能
user_list=[ {'}, {'}, {'}, {'}, ] current_dic={'username':None,'login':False} def auth_func(func): d ...
- apache 限制连接并发数和下载速度
apache 限制并发数 需要安装:mod_limitipconn模块 安装方法:yum install mod_limitipconn 安装完成后: 检查 Apache 模块目录,看是否含有 mod ...