Spring Boot @EnableWebMvc 与 mvc 配置
注意:
1.小心使用 @EnableWebMvc 注解
根据官方文档,尽量不要使用 @EnableWebMvc 注解,因为它会关闭默认配置。
① 你希望关闭默认配置,自己完全重新实现一个
@EnableWebMvc
@Configuration
public class WebConfig implements WebMvcConfigurer {
② 你希望重写部分配置
//@EnableWebMvc
@Configuration
public class WebConfig implements WebMvcConfigurer {
或者
@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcAutoConfiguration {
2.关于静态资源的映射
有两个属性可以了解下:第一行定义匹配的路由地址,第二行定义该路由相匹配的资源的存放位置
spring.mvc.static-path-pattern=/** # Path pattern used for static resources.
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ # Locations of static resources.
举例:文件结构

对于 public, static 等文件夹下的静态文件,可以通过 /css/style.css的形式访问(不需要加前缀 static,比如 /static/css/style.css)
3.内容版本策略
ContentVersionStrategy,底层使用 md5 根据内容进行版本区分,从而避免过期缓存。
resources.chain.strategy.content.enabled=true
服务端开启该功能,前端模板如何生成一个带 md5版本的链接呢?
① 如果使用的是 Thymeleaf,可以使用 @bean 语法访问 ResourceUrlProvider Bean
<script type="application/javascript"
th:src="${@mvcResourceUrlProvider.getForLookupPath('/javascript/test.js')}">
</script>
② 使用 ControllerAdvice (控制器增强,用于拦截控制器方法)
@ControllerAdvice
public class ResourceUrlAdvice { @Inject
ResourceUrlProvider resourceUrlProvider; @ModelAttribute("urls")
public ResourceUrlProvider urls() {
return this.resourceUrlProvider;
}
}
这样我们可以使用如下的方式生成版本 url
<script type="application/javascript"
th:src="${urls.getForLookupPath('/javascript/test.js')}">
</script>
③ 添加一个 ResourceUrlEncodingFilter Bean,如果模板引擎的 response 调用了 encodeURL() 方法,那么 url将自动版本化(支持 JSPs, Thymeleaf, FreeMarker and Velocity.)
@Configuration
public class WebConfig implements WebMvcConfigurer { @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
VersionResourceResolver versionResourceResolver = new VersionResourceResolver()
.addVersionStrategy(new ContentVersionStrategy(), "/**");
registry.addResourceHandler("/javascript/*.js")
.addResourceLocations("classpath:/static/")
.setCachePeriod(60 * 60 * 24 * 365) /* one year */
.resourceChain(true)
.addResolver(versionResourceResolver);
} @Bean
public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
return new ResourceUrlEncodingFilter();
}
...
4.locale 设置
spring.mvc.locale.locale=en_US
spring.mvc.locale.locale-resolver=accept_header # Use the "Accept-Language" header or the configured locale if the header is not set
或者使用参数化的配置(比如 http://localhost:8080/?locale=fr)
@Configuration
public class WebConfig implements WebMvcConfigurer { @Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.FRENCH);
return slr;
} @Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
return new LocaleChangeInterceptor();
} @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
} }
5.application.properties 部分属性配置
spring:
thymeleaf:
mode: HTML5
cache: false
suffix: .html
encoding: UTF-8
resources:
chain:
cache: false # Whether to enable caching in the Resource chain.
html-application-cache: true # Whether to enable HTML5 application cache manifest rewriting.
strategy.content.enabled: true # Whether to enable the content Version Strategy.
mvc:
date-format: dd/MM/yyyy
locale: zh_CN
locale-resolver: accept_header
6. handler 参数解析器
用于对 handler 方法中的参数进行解析,比如注入 CurrentUser
@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
public class BladeWebMvcConfiguration implements WebMvcConfigurer { @Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(new TokenArgumentResolver());
} }
参考文章
https://www.mscharhag.com/spring/resource-versioning-with-spring-mvc
https://www.jianshu.com/p/917f9e8a94a6
https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/htmlsingle/#common-application-properties (Spring Boot 默认配置)
Spring Boot @EnableWebMvc 与 mvc 配置的更多相关文章
- Spring Boot 2.X(四):Spring Boot 自定义 Web MVC 配置
0.准备 Spring Boot 不仅提供了相当简单使用的自动配置功能,而且开放了非常自由灵活的配置类.Spring MVC 为我们提供了 WebMvcConfigurationSupport 类和一 ...
- Spring Boot2 系列教程(十八)Spring Boot 中自定义 SpringMVC 配置
用过 Spring Boot 的小伙伴都知道,我们只需要在项目中引入 spring-boot-starter-web 依赖,SpringMVC 的一整套东西就会自动给我们配置好,但是,真实的项目环境比 ...
- 【面试普通人VS高手系列】Spring Boot的约定优于配置,你的理解是什么?
对于Spring Boot约定优于配置这个问题,看看普通人和高手是如何回答的? 普通人的回答: 嗯, 在Spring Boot里面,通过约定优于配置这个思想,可以让我们少写很多的配置, 然后就只需要关 ...
- 学记:为spring boot写一个自动配置
spring boot遵循"约定优于配置"的原则,使用annotation对一些常规的配置项做默认配置,减少或不使用xml配置,让你的项目快速运行起来.spring boot的神奇 ...
- Spring Boot 探索系列 - 自动化配置篇
26. Logging Prev Part IV. Spring Boot features Next 26. Logging Spring Boot uses Commons Logging f ...
- Spring Boot 2.0 教程 | 配置 Undertow 容器
欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 文章首发于个人网站 https://ww ...
- Spring Boot之实现自动配置
GITHUB地址:https://github.com/zhangboqing/springboot-learning 一.Spring Boot自动配置原理 自动配置功能是由@SpringBootA ...
- Spring Boot实践——用外部配置填充Bean属性的几种方法
引用:https://blog.csdn.net/qq_17586821/article/details/79802320 spring boot允许我们把配置信息外部化.由此,我们就可以在不同的环境 ...
- spring boot(5)-properties参数配置
application.properties application.properties是spring boot默认的配置文件,spring boot默认会在以下两个路径搜索并加载这个文件 s ...
随机推荐
- Centos 7 图形安装笔记(超详细)
1. 下载虚拟机(VMware Workstation Pro) 2. 安装虚拟机(Windows下安装虚拟机,自行网上搜索) 3. 下载Centos 7.4系统(国内建议使用阿里云: http:// ...
- 如何使用多数据源,同时使用jpa和jdbctemplate
spring: datasource: first: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://xx.xx.xx.x ...
- UE4材质特别属生记录
tangent space normal 切线空间法线 材质默认使用的切线空间法线,可切换为对象空间法线(彩色贴图) Separate Translucency 单独半透明 允许半透明被渲染到一个单独 ...
- Git上传文件容量大小限制
使用Git LFS突破git的100M容量限制. 1.安装Git LFS https://git-lfs.github.com./ 2.安装后把里面的git-lfs.exe放到你要上传的项目文件夹 3 ...
- elk搭建日志系统
参考:https://www.cnblogs.com/yuhuLin/p/7018858.html 以上这篇文章已经写的很好很全了,之所以再自己写一遍大概就是记录一下,以后可能会有用吧 安装elast ...
- rocketmq 4.2.0集群配置
采用的是2m-2s-async模式 1. 修改每台机器的/etc/hosts 文件,增加如下内容 192.168.1.100 nameserver1192.168.1.102 nameserver ...
- saltstack 入门
1.Saltstack是什么? saltstack 是一个异构平台基础设施管理工具,具有远程执行.配置管理.云管理.只需花费数分钟就可以运行起来,扩展性足以支撑上万台服务器,速度快,服务器之间秒级通讯 ...
- 十一.keepalived高可用服务实践部署
期中集群架构-第十一章-keepalived高可用集群章节======================================================================0 ...
- JAVA 动态代理原理和实现
在 Java 中动态代理和代理都很常见,几乎是所有主流框架都用到过的知识.在面试中也是经常被提到的话题,于是便总结了本文. Java动态代理的基本原理为:被代理对象需要实现某个接口(这是前提),代理对 ...
- SVN 版本控制安装
客户端 一路点击next即可 注意事项: 这个一定要选,否则后面使用会出现问题. 选择下拉菜单的第一项