游侠的博客 游侠的博客
首页
  • 论文笔记
  • 一些小知识点

    • pytorch、numpy、pandas函数简易解释
  • 《深度学习500问》
开发
技术
更多
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Ranger

一名在校研究生
首页
  • 论文笔记
  • 一些小知识点

    • pytorch、numpy、pandas函数简易解释
  • 《深度学习500问》
开发
技术
更多
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Vue

  • SpringBoot2

  • JavaWeb

  • SSM

  • SpringBoot3

    • 一、SpringBoot3快速入门
    • 二、SpringBoot核心机制了解
    • 三、SpringBoot的核心技能
    • 四、WebMvcAutoConfiguration原理
      • 1. 生效条件
      • 2. 效果
      • 3. WebMvcConfigurer接口
      • 4. 静态资源规则源码
        • 缓存实验
      • 5. EnableWebMvcConfiguration源码
      • 6. 为什们容器中放一个WebMvcConfigurer就能配置底层行为
      • 7. WebMvcConfigurationSupport
    • 五、SpringBootWeb开发-Web场景
    • 六、SpringBootWeb开发-静态资源与路径匹配
    • 七、SpringBootWeb开发-内容协商
    • 八、SpringBootWeb开发-模板引擎
    • 九、SpringBootWeb开发-国际化与错误处理
    • 十、SpringBootWeb开发-嵌入式容器
    • 十一、SpringBootWeb开发-SpringMVC所有常用特性配置
    • 十二、SpringBootWeb开发-Web新特性
    • 十三、SpringBoot-整合SSM场景
    • 十四、SpringBoot-基础特性
    • 十五、SpringBoot-核心原理
    • 十六、SpringBoot场景整合-环境准备
    • 十七、SpringBoot场景整合-Redis
    • 十八、SpringBoot场景整合-接口文档
    • 十九、SpringBoot场景整合-远程调用
    • 二十、SpringBoot场景整合-消息服务
    • 二十一、SpringBoot场景整合-Web安全
    • 二十二、SpringBoot场景整合-可观测性
    • 二十三、SpringBoot场景整合-AOT
  • 技术
  • SpringBoot3
yangzhixuan
2023-06-05
目录

四、WebMvcAutoConfiguration原理

# 1. 生效条件

@AutoConfiguration(after = { DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
		ValidationAutoConfiguration.class }) //在这些自动配置之后
@ConditionalOnWebApplication(type = Type.SERVLET) //如果是SERVLET类型web应用就生效。相对的还有REACTIVE 响应式web,这个就不行
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class) //容器中没有这个Bean,才生效。默认就是没有
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)//优先级
@ImportRuntimeHints(WebResourcesRuntimeHints.class)
public class WebMvcAutoConfiguration { 
    
}
1
2
3
4
5
6
7
8
9
10

# 2. 效果

  1. 放了两个Filter:

    1. HiddenHttpMethodFilter;页面表单提交Rest请求(GET、POST、PUT、DELETE)
    2. FormContentFilter: 表单内容Filter,GET(数据放URL后面)、POST(数据放请求体)请求可以携带数据,PUT、DELETE 的请求体数据会被忽略
  2. 给容器中放了WebMvcConfigurer组件(代码如下,创建了一个实现了WebMvcConfigurer接口的配置类组件);给SpringMVC添加各种定制功能

    1. 所有的功能最终会和配置文件进行绑定
    2. WebMvcProperties: spring.mvc配置文件
    3. WebProperties: spring.web配置文件
	@Configuration(proxyBeanMethods = false)
	@Import(EnableWebMvcConfiguration.class) //额外导入了其他配置
	@EnableConfigurationProperties({ WebMvcProperties.class, WebProperties.class })
	@Order(0)
	public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ServletContextAware{
        
    }
1
2
3
4
5
6
7

# 3. WebMvcConfigurer接口

提供了配置SpringMVC底层的所有组件入口

image

# 4. 静态资源规则源码

@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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  1. 规则一:访问: /webjars/**路径就去 classpath:/META-INF/resources/webjars/下找资源.

    1. maven 导入依赖
    2. 直接在浏览器里访问包里的js等静态文件
  2. 规则二:访问: /**路径就去 静态资源默认的四个位置找资源

    1. classpath:/META-INF/resources/
    2. classpath:/resources/
    3. classpath:/static/
    4. classpath:/public/
  3. 规则三:静态资源默认都有缓存规则的设置

    1. 所有缓存的设置,直接通过配置文件: spring.web
    2. cachePeriod: 缓存周期; 多久不用找服务器要新的。 默认没有,以s为单位
    3. cacheControl: HTTP缓存控制;https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Caching (opens new window)
    4. useLastModified:是否使用最后一次修改。配合HTTP Cache规则

如果浏览器访问了一个静态资源 index.js,如果服务这个资源没有发生变化,下次访问的时候就可以直接让浏览器用自己缓存中的东西,而不用给服务器发请求。但这中间还是要发一次请求询问一下资源是否修改,根据修改的时间判断。

registration.setCachePeriod(getSeconds(this.resourceProperties.getCache().getPeriod()));// 对应配置文件中的spring.web.resources.cache.period
registration.setCacheControl(this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl());// 对应配置文件中的spring.web.resources.cache.cachecontrol
registration.setUseLastModified(this.resourceProperties.getCache().isUseLastModified());// 对应配置文件中的spring.web.resources.cache.use-last-modified
1
2
3

# 缓存实验

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 5. EnableWebMvcConfiguration源码

//SpringBoot 给容器中放 WebMvcConfigurationSupport 组件。
//我们如果自己放了 WebMvcConfigurationSupport 组件,Boot的WebMvcAutoConfiguration都会失效。
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(WebProperties.class)
public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware 
{

    
}
1
2
3
4
5
6
7
8
9
  1. HandlerMapping: 根据请求路径 /a 找那个handler能处理请求

    例如WelcomePageHandlerMapping:

    1. 访问 /**路径下的所有请求,都在以前四个静态资源路径下找,欢迎页也一样
    2. 找index.html:只要静态资源的位置有一个 index.html页面,项目启动默认访问

# 6. 为什们容器中放一个WebMvcConfigurer就能配置底层行为

  1. WebMvcAutoConfiguration 是一个自动配置类,它里面有一个 EnableWebMvcConfiguration
  2. EnableWebMvcConfiguration继承于 DelegatingWebMvcConfiguration,这两个都生效
  3. DelegatingWebMvcConfiguration利用 DI 把容器中 所有 WebMvcConfigurer注入进来,包括我们自己写的
  4. 别人调用 DelegatingWebMvcConfiguration 的方法配置底层规则,而它调用所有 WebMvcConfigurer的配置底层方法。

# 7. WebMvcConfigurationSupport

提供了很多的默认设置。

判断系统中是否有相应的类:如果有,就加入相应的HttpMessageConverter

jackson2Present = ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", classLoader) &&
				ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", classLoader);
jackson2XmlPresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.xml.XmlMapper", classLoader);
jackson2SmilePresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.smile.SmileFactory", classLoader);
1
2
3
4
编辑 (opens new window)
上次更新: 2024/05/30, 07:49:34
三、SpringBoot的核心技能
五、SpringBootWeb开发-Web场景

← 三、SpringBoot的核心技能 五、SpringBootWeb开发-Web场景→

最近更新
01
tensor比较大小函数
05-30
02
Large Language Models can Deliver Accurate and Interpretable Time Series Anomaly Detection
05-27
03
半监督学习经典方法 Π-model、Mean Teacher
04-10
更多文章>
Theme by Vdoing | Copyright © 2023-2024 Ranger | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式