Spring中WebMvcConfigurer用到的JDK8特性
闲来无聊,随便翻看项目,发现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特性的更多相关文章
- 利用Spring中同名Bean相互覆盖的特性,定制平台的类内容。
欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...
- Spring中可以复用的工具类&特性记录
Spring 里有用工具类: GenericTypeResolver 解析泛型类型.核心逻辑还是调用 ResolvableTypeResolvableType 解析泛型类型 BeanWrapper 利 ...
- 阶段3 2.Spring_10.Spring中事务控制_11 spring5新特性的介绍
jdk1.7和1.8的差别 准备好的一个maven工程 反射创建对象10亿次 ,用的时间 替换jdk的版本 选择为1.7 切换了1.7的版本以后呢执行的速度就变的非常的慢 两个版本的对比 响应式编程风 ...
- 事务特性,事务的隔离级别以及spring中定义的事务传播行为
.katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...
- 业余草分享 Spring Boot 2.0 正式发布的新特性
就在昨天Spring Boot2.0.0.RELEASE正式发布,今天早上在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误, ...
- Spring 4支持的Java 8新特性一览
有众多新特性和函数库的Java 8发布之后,Spring 4.x已经支持其中的大部分.有些Java 8的新特性对Spring无影响,可以直接使用,但另有些新特性需要Spring的支持.本文将带您浏览S ...
- Spring中文文档-第一部分
一. Spring 框架概述 Spring是为了构建企业应用的轻量级框架.然而,Spring是模块化的,允许你只是使用其中的一部分,不需要引入其他的.你可以在任何web框架上使用IoC容器,也可以只使 ...
- Spring中文文档
前一段时间翻译了Jetty的一部分文档,感觉对阅读英文没有大的提高(*^-^*),毕竟Jetty的受众面还是比较小的,而且翻译过程中发现Jetty的文档写的不是很好,所以呢翻译的兴趣慢慢就不大了,只能 ...
- Spring 中的 Bean 配置
内容提要 •IOC & DI 概述 •配置 bean –配置形式:基于 XML 文件的方式:基于注解的方式 –Bean 的配置方式:通过全类名(反射).通过工厂方法(静态工厂方法 & ...
随机推荐
- Quick Start NodeMCU / ESP8266 12E
先说明一下:本来想买常见的ESP 8266作为Arduinoi的WIFI模块,结果错买成ESP 8266 12E,发现网上的资料比较少. ESP8266是WIFI芯片,它只是一块芯片必须要搭配相应的电 ...
- Java验证jwt token
https://jwt.io/ RS256加密JWT生成.验证 https://blog.csdn.net/u011411069/article/details/79966226 How to loa ...
- c# 如何进行动态加载dll
最近遇到了在c#中如何进行动态加载dll的话,搞定了,下面介绍一下自己的步骤. 1,新建dll. 打开vs,新建project->Class Library->项目名为testdll1.在 ...
- 同余and乘法逆元学习笔记
目录 数学符号 快速幂 方法一 方法二 同余 概念 同余的性质 乘法逆元 概念: 求逆元的方法 扩展欧几里得 快速幂法\(o(n*log(n))\) 递推法\(o(n)\) sjp大佬让我写同余那就只 ...
- windows内核代码之进程操作
[toc] 一丶简介 整理一下windows内核中.常用的代码.这里只整理下进程的相关代码. 二丶 windows内核之遍历进程 内核中记录进程的结构体是EPROCESS结构.所以只需要遍历这个结构即 ...
- Spring通知,顾问,增强
1.AOP (Aspect Oriented Programming 面向切面编程) 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编 ...
- shell脚本获取传入参数的个数
ts.sh #!/bin/bash echo $# 输出 [root@redhat6 ~]# ./ts.sh para1 [root@redhat6 ~]# ./ts.sh para1 para2 [ ...
- [oracle/java/sql]用于上十万批量数据插入Oracle表的Java程序
程序下载:https://files.cnblogs.com/files/xiandedanteng/LeftInnerNotExist20191222.rar 原理:Oracle的Insert al ...
- [SQL]用于提取组内最新数据,左连接,内连接,not exist三种方案中,到底谁最快?
本作代码下载:https://files.cnblogs.com/files/xiandedanteng/LeftInnerNotExist20191222.rar 人们总是喜欢给出或是得到一个简单明 ...
- wikiquote
發現了一個很好玩的網站wikiquote,上面有很多引用的句子 比如關於編程語言的說法 https://en.m.wikiquote.org/wiki/Category:Programming_lan ...