Java --- springboot3之web静态资源配置
目录
(图片来源网络,侵删)
一、静态资源规则
二、欢迎页规则
三、favicon.ioc规则
四、HTTP缓存机制
五、自定义静态资源规则
5.1、配置方式
(图片来源网络,侵删)
5.2、代码方式
一、静态资源规则
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { if (!this.resourceProperties.isAddMappings()) { logger.debug("Default resource handling disabled"); return; } //1、 addResourceHandler(registry, this.mvcProperties.getWebjarsPathPattern(), "classpath:/META-INF/resources/webjars/"); addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> { registration.addResourceLocations(this.resourceProperties.getStaticLocations()); if (this.servletContext != null) { ServletContextResource resource = new ServletContextResource(this.servletContext, SERVLET_LOCATION); registration.addResourceLocations(resource); } }); }
1、规则一:访问: /webjars/**路径就去 classpath:/META-INF/resources/webjars/下找资源。可通过maven导入
2、规则二:访问: /**路径就去 静态资源默认的四个位置找资源
3、规则三、静态资源默认都有缓存规则的设置
registration.setCachePeriod(getSeconds(this.resourceProperties.getCache().getPeriod())); registration.setCacheControl(this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl()); registration.setUseLastModified(this.resourceProperties.getCache().isUseLastModified());
①、所有缓存的设置,直接通过配置文件: spring.web
(图片来源网络,侵删)
②、cachePeriod: 缓存周期; 多久不用找服务器要新的。 默认没有,以s为单位
③、useLastModified:是否使用最后一次修改。配合HTTP Cache规则
④、cacheControl: HTTP缓存控制;
二、欢迎页规则
//SpringBoot 给容器中放 WebMvcConfigurationSupport 组件。 //我们如果自己放了 WebMvcConfigurationSupport 组件,Boot的WebMvcAutoConfiguration都会失效。 @Configuration(proxyBeanMethods = false) @EnableConfigurationProperties(WebProperties.class) public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware { }
三、favicon.ioc规则
四、HTTP缓存机制
只需要在springboot的配置文件中进行配置
server.port=9000 #1、spring.web: # 1.配置国际化的区域信息 # 2.静态资源策略(开启、处理链、缓存) #开启静态资源映射规则 spring.web.resources.add-mappings=true #设置缓存 #spring.web.resources.cache.period=3600 ##缓存详细合并项控制,覆盖period配置: ## 浏览器第一次请求服务器,服务器告诉浏览器此资源缓存7200秒,7200秒以内的所有此资源访问不用发给服务器请求,7200秒以后发请求给服务器 spring.web.resources.cache.cachecontrol.max-age=7200 #使用资源 last-modified 时间,来对比服务器和浏览器的资源是否相同没有变化。相同返回 304 spring.web.resources.cache.use-last-modified=true
五、自定义静态资源规则
5.1、配置方式
在springboot的配置文件中进行配置
测试结果:
也可以通过web配置:不仅可以配置静态资源文件夹位置还可以配置缓存策略
#自定义静态资源文件夹位置 spring.web.resources.static-locations=classpath:/a/,classpath:/b/,classpath:/static/
5.2、代码方式
实现WebMvcConfigurer方式
//@EnableWebMvc //禁用boot默认配置 @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { //保留以前的默认配置 //自定义配置 registry.addResourceHandler("/static/**") .addResourceLocations("classpath:/a/","classpath:/b/") .setCacheControl(CacheControl.maxAge(5000, TimeUnit.SECONDS)); } }
给容器中放一个WebMvcConfigurer组件
@Bean public WebMvcConfigurer webMvcConfigurer(){ return new WebMvcConfigurer(){ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { //保留以前的默认配置 //自定义配置 registry.addResourceHandler("/static/**") .addResourceLocations("classpath:/a/","classpath:/b/") .setCacheControl(CacheControl.maxAge(5000, TimeUnit.SECONDS)); } }; }
容器中放一个WebMvcConfigurer就能配置底层行为
- WebMvcAutoConfiguration 是一个自动配置类,它里面有一个 EnableWebMvcConfiguration
- EnableWebMvcConfiguration继承与 DelegatingWebMvcConfiguration,这两个都生效
- DelegatingWebMvcConfiguration利用 DI 把容器中 所有 WebMvcConfigurer 注入进来
- 别人调用 `DelegatingWebMvcConfiguration` 的方法配置底层规则,而它调用所有 WebMvcConfigurer的配置底层方法。
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。
还没有评论,来说两句吧...