闲来无聊,随便翻看项目,发现WebMvcConfigurerAdapter已经过时了,它的作用也不用说了,就是起到适配器的作用,让实现类不用实现所有方法,可以根据实际需要去实现需要的方法。

@Deprecated
public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer { /**
* {@inheritDoc}
* <p>This implementation is empty.
*/
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
} ...
}

这个废弃了,那么推荐的方式呢?是实现类直接去继承 WebMvcConfigurer 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration
public class MyWebConfig implements WebMvcConfigurer { @Autowired
private CommonInterceptor commonInterceptor; /**
* 配置拦截器
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(commonInterceptor).addPathPatterns("/**").excludePathPatterns("/login");
}
}

那么问题来了,我们都知道,实现接口是要实现其中的所有方法的,它是怎样做到 适配 的呢?我们打开 WebMvcConfigurer 看一下

public interface WebMvcConfigurer {

    /**
* Helps with configuring HandlerMappings path matching options such as trailing slash match,
* suffix registration, path matcher and path helper.
* Configured path matcher and path helper instances are shared for:
* <ul>
* <li>RequestMappings</li>
* <li>ViewControllerMappings</li>
* <li>ResourcesMappings</li>
* </ul>
* @since 4.0.3
*/
default void configurePathMatch(PathMatchConfigurer configurer) {
}
...
}

我们都知道,刚开始接触Java的时候,定义接口是不允许有方法体的!可是,JDK8改变了这个规则,通过default或者static关键字。比如这样

public interface MyInterface {

    default void add(int a, int b) {
System.out.println(a + b);
} default void doSomething() {
} static void hello(String name) {
System.out.println("Hello! " + name);
}
}

接下来创建一个实现类(只实现了部分方法)

public class MyInterfaceImpl implements MyInterface {

    public static void main(String[] args){
MyInterface.hello("Tom");
new MyInterfaceImpl().add(1,2);
new MyInterfaceImpl().doSomething();
} @Override
public void doSomething() {
System.out.println("Do Something");
}
}

测试输出

Spring中WebMvcConfigurer用到的JDK8特性的更多相关文章

  1. 利用Spring中同名Bean相互覆盖的特性,定制平台的类内容。

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  2. Spring中可以复用的工具类&特性记录

    Spring 里有用工具类: GenericTypeResolver 解析泛型类型.核心逻辑还是调用 ResolvableTypeResolvableType 解析泛型类型 BeanWrapper 利 ...

  3. 阶段3 2.Spring_10.Spring中事务控制_11 spring5新特性的介绍

    jdk1.7和1.8的差别 准备好的一个maven工程 反射创建对象10亿次 ,用的时间 替换jdk的版本 选择为1.7 切换了1.7的版本以后呢执行的速度就变的非常的慢 两个版本的对比 响应式编程风 ...

  4. 事务特性,事务的隔离级别以及spring中定义的事务传播行为

    .katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...

  5. 业余草分享 Spring Boot 2.0 正式发布的新特性

    就在昨天Spring Boot2.0.0.RELEASE正式发布,今天早上在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误, ...

  6. Spring 4支持的Java 8新特性一览

    有众多新特性和函数库的Java 8发布之后,Spring 4.x已经支持其中的大部分.有些Java 8的新特性对Spring无影响,可以直接使用,但另有些新特性需要Spring的支持.本文将带您浏览S ...

  7. Spring中文文档-第一部分

    一. Spring 框架概述 Spring是为了构建企业应用的轻量级框架.然而,Spring是模块化的,允许你只是使用其中的一部分,不需要引入其他的.你可以在任何web框架上使用IoC容器,也可以只使 ...

  8. Spring中文文档

    前一段时间翻译了Jetty的一部分文档,感觉对阅读英文没有大的提高(*^-^*),毕竟Jetty的受众面还是比较小的,而且翻译过程中发现Jetty的文档写的不是很好,所以呢翻译的兴趣慢慢就不大了,只能 ...

  9. Spring 中的 Bean 配置

    内容提要 •IOC & DI 概述 •配置 bean –配置形式:基于 XML 文件的方式:基于注解的方式 –Bean 的配置方式:通过全类名(反射).通过工厂方法(静态工厂方法 & ...

随机推荐

  1. 硬币游戏1——打表&&记忆化搜索

    题目 Alice和Bo在玩这样一个游戏,给定 $k$ 个数字 $a_1, a_2,..,a_k$.一开始有 $x$ 枚硬币,Alice和Bob轮流取硬币.每次所取的硬币的枚数一定要在 $k$ 个数当中 ...

  2. 关于绿盟RSAS使用时遇到的问题

    本周在使用绿盟RSAS扫描工具时遇到了一些问题: 一.扫描工具在家测试可以正常工作,到了现场设置正确但Web端页面打不开: 二.扫描器可以正常进行扫描,并且成功扫描出结果,但显示目标主机没有问题: 原 ...

  3. HNOI2015总结

    // 此博文为迁移而来,写于2015年4月21日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vy9t.html 这次省 ...

  4. 同余方程组(EXCRT)(luogu4777)

    #include<cstdio> #include<algorithm> #define ll long long using namespace std; ll k; ll ...

  5. Kmeans文档聚类算法实现之python

    实现文档聚类的总体思想: 将每个文档的关键词提取,形成一个关键词集合N: 将每个文档向量化,可以参看计算余弦相似度那一章: 给定K个聚类中心,使用Kmeans算法处理向量: 分析每个聚类中心的相关文档 ...

  6. GIT-本地仓库

    用户配置 git config --global user.name "name" git config --global user.email "123@qq.com& ...

  7. 记住:永远不要在 MySQL 中使用 UTF-8

    阅读本文大概需要 3.6 分钟. 译文:http://suo.im/4zBuvs 来自:http://ju.outofmemory.cn 最近我遇到了一个bug,我试着通过Rails在以“utf8”编 ...

  8. java基础之 final

    参考文档: 内存模型&final:http://www.infoq.com/cn/articles/java-memory-model-6/   根据程序上下文环境,Java关键字final有 ...

  9. 【Beta】Scrum meeting 7

    目录 写在前面 进度情况 任务进度表 Beta-1阶段燃尽图 遇到的困难 照片 commit截图记录 后端代码仓库 技术博客 写在前面 例会时间:5.11 22:30-22:50 例会地点:微信群语音 ...

  10. python开发笔记-str转字典

    后台接收到post请求数据格式为json格式的字符串,不能直接用字典的get方法 909090909090909090909090909090909 Internal Server Error: /g ...