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 的配置方式:通过全类名(反射).通过工厂方法(静态工厂方法 & ...
随机推荐
- Mophues HDU - 4746 (莫比乌斯反演)
Mophues \[ Time Limit: 10000 ms\quad Memory Limit: 262144 kB \] 题意 求出满足 \(gcd\left(a,b\right) = k\), ...
- nginx之TCP反向代理
实现Nginx tcp负载均衡 Nginx在1.9.0版本开始支持tcp模式的负载均衡,在1.9.13版本开始支持udp协议的负载,udp主要用于DNS的域名解析,其配置方式和指令和http 代理类似 ...
- vue 百度地图多标注展示和点击标注进行的提示
index.html中加入script <script type="text/javascript" src="http://api.map.baidu.com/a ...
- glew的安装
下载链接: https://sourceforge.net/project/downloading.php?group_id=67586&filename=glew-1.5.1-win32.z ...
- Mongoose 入门以及实现数据的增、删、改、查
mongoose 介绍 Mongoose 是在 node.js 异步环境下对 mongodb 进行便捷操作的对象模型工具.Mongoose 是 NodeJS 的驱动,不能作为其他语言的驱动. Mong ...
- exception The absolute uri: [http://java.sun.com/jsp/jstl/core] cannot be resolved in either web.xml or the jar files deployed with this application
1.情景展示 eclipse,运行web项目时,报错信息如下: The absolute uri: [http://java.sun.com/jsp/jstl/core] cannot be ...
- quartz 1.6.2之前的版本,定时任务自动停掉问题
https://searchcode.com/codesearch/view/28831622/ Quartz 1.6.2 Release Notes This release contains a ...
- 使用Rome读取RSS报错,org.xml.sax.SAXParseException: 前言中不允许有内容。
这是我遇到过的最奇葩的错误 new URL的时候,使用静态变量就会报错org.xml.sax.SAXParseException: 前言中不允许有内容. URL url = new URL(Strin ...
- 解析生效测试方法 执行命令 ping 域名 得不到 IP 主要有如下几个原因:
https://help.aliyun.com/knowledge_detail/39834.html dig https://cloud.tencent.com/document/product/3 ...
- @JsonInclude(Include.NON_NULL) resttemplate 传递实体参数时 序列化为json时 空字符串不参与序列化
@JsonInclude(Include.NON_NULL) resttemplate 传递实体参数时 序列化为json时 空字符串不参与序列化 https://www.cnblogs.com/sup ...