路还在继续,梦还在期许
1、静态资源访问
1.1、静态资源目录
默认静态资源放在类路径下: resources 下 /static 或 /public 或 /resources 或 /META-INF/resources 目录下
访问地址: 当前项目根路径/ + 静态资源名
原理: 写静态资源名自动找到静态资源,静态资源映射/**。
spring:
# 默认
mvc:
static-path-pattern: /**
/**的意思是所有文件夹及里面的子文件夹
/*是所有文件夹,不含子文件夹
1.2、如果静态资源与controller资源重名
请求进来,先去找Controller看能不能处理。
不能处理的所有请求又都交给静态资源处理器。
静态资源也找不到则响应404页面
1.3、改变默认的静态资源路径
spring:
# 默认
mvc:
static-path-pattern: /**
# 改变默认的静态资源路径
resources:
static-locations: [classpath:/haha/]
1.4、修改静态资源访问前缀
添加前缀
spring:
# 添加前缀
mvc:
static-path-pattern: /res/**
访问地址:当前项目 + static-path-pattern的值 + 静态资源名 = 静态资源文件夹下找
1.5、webjar
可以自动映射 /webjars/** 下的资源
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.5.1</version>
</dependency>
存放路径:META-INF/resources/webjars/jquery/3.5.1/jquery.js
访问地址:http://localhost:8080/webjars/jquery/3.5.1/jquery.js 后面地址要按照依赖里面的包路径
2、欢迎页支持
- 静态资源路径下 放 index.html
- 可以配置静态资源路径
- 但是不可以配置静态资源的访问前缀。否则导致 index.html不能被默认访问
spring:
# mvc:
# static-path-pattern: /res/** 这个会导致welcome page功能失效
resources:
static-locations: [classpath:/haha/]
- controller能处理/index
3、自定义 Favicon
favicon.ico 放在静态资源目录下,spring boot 会自动将这个名字的图标当成应用的图标。
spring:
# mvc:
# static-path-pattern: /res/** 这个会导致 Favicon 功能失效
4、静态资源配置原理
spring boot 启动时,会默认加载 xxxAutoConfiguration 类(自动配置类),这里从配置类入手查看源码。
4.1、与Web开发有关的相关自动配置类
DispatcherServletAutoConfiguration: 配置SpringNVC中DispatcherServlet
HttpEncodingAutoConfiguration: 配置编解码
MultipartAutoConfiguration: 配置文件上传
ServletWebServerFactoryAutoConfiguration: 配置服务器
WebMvcAutoConfiguration: SpringMVC功能的自动配置类
4.2、WebMvcAutoConfiguration 注解介绍
// 当前类为配置类,组件之间无依赖关系
@Configuration(proxyBeanMethods = false)
// 程序是Servlet应用
@ConditionalOnWebApplication(type = Type.SERVLET)
// 导入Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class 这三个类
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
// 容器中没有 WebMvcConfigurationSupport.class 组件,当前类中配置才生效
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {}
4.3、WebMvcAutoConfiguration 功能介绍
位置:org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
// SpringMVC用于兼容REST风格
@Bean
@ConditionalOnMissingBean(HiddenHttpMethodFilter.class)
@ConditionalOnProperty(prefix = "spring.mvc.hiddenmethod.filter", name = "enabled", matchIfMissing = false)
public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {
return new OrderedHiddenHttpMethodFilter();
}
// 表单内容过滤器
@Bean
@ConditionalOnMissingBean(FormContentFilter.class)
@ConditionalOnProperty(prefix = "spring.mvc.formcontent.filter", name = "enabled", matchIfMissing = true)
public OrderedFormContentFilter formContentFilter() {
return new OrderedFormContentFilter();
}
4.4 WebMvcAutoConfigurationAdapter 内部类
位置:org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter
// Defined as a nested config to ensure WebMvcConfigurer is not read when not
// on the classpath
@Configuration(proxyBeanMethods = false)
@Import(EnableWebMvcConfiguration.class)
// 把配置文件的相关属性与WebMvcProperties=(spring.mvc)、ResourceProperties进行绑定=(spring.resources)
@EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class })
@Order(0)
public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer {}
1、唯一有参构造器
有参构造器所有参数的值都会从容器中确定
形参 | 作用 |
---|---|
ResourceProperties resourceProperties; | 获取和spring.resources绑定的所有的值的对象 |
WebMvcProperties mvcProperties | 获取和spring.mvc绑定的所有的值的对象 |
ListableBeanFactory beanFactory | Spring的beanFactory(spring的容器) |
HttpMessageConverters | 找到所有的HttpMessageConverters |
ResourceHandlerRegistrationCustomizer | 找到 资源处理器的自定义器。重点 |
DispatcherServletPath | DispatcherServlet处理的路径 |
ServletRegistrationBean | 给应用注册Servlet、Filter… |
public WebMvcAutoConfigurationAdapter(ResourceProperties resourceProperties, WebMvcProperties mvcProperties,
ListableBeanFactory beanFactory, ObjectProvider<HttpMessageConverters> messageConvertersProvider,
ObjectProvider<ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizerProvider,
ObjectProvider<DispatcherServletPath> dispatcherServletPath,
ObjectProvider<ServletRegistrationBean<?>> servletRegistrations) {
this.resourceProperties = resourceProperties;
this.mvcProperties = mvcProperties;
this.beanFactory = beanFactory;
this.messageConvertersProvider = messageConvertersProvider;
this.resourceHandlerRegistrationCustomizer = resourceHandlerRegistrationCustomizerProvider.getIfAvailable();
this.dispatcherServletPath = dispatcherServletPath;
this.servletRegistrations = servletRegistrations;
}
2、资源处理的默认规则
添加资源处理器
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// 拿到与配置文件绑定的属性spring.resources.add-mappings=true(false禁用静态资源路径映射)
if (!this.resourceProperties.isAddMappings()) {//true不进入,false进入
logger.debug("Default resource handling disabled");
return;
}
// 获取到配置文件中,静态资源的缓存策略,所有的静态资源在浏览器默认存多少秒
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
// 注册第一种访问规则:/webjars/**
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
// 在项目中寻找静态资源
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/")
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
// 获取配置文件中spring.mvc.static-path-pattern的值,没有配置就使用默认值:/**
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
// 在项目中寻找默认位置:{ "classpath:/META-INF/resources/","classpath:/resources/", "classpath:/static/", "classpath:/public/" };
// 默认的位置也可以修改
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
.addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}
spring:
resources:
add-mappings: false #禁用所有静态资源规则
cache:
period: 1100 # 以秒为单位
4.5 EnableWebMvcConfiguration 内部类
/**
* Configuration equivalent to {@code @EnableWebMvc}.
*/
@Configuration(proxyBeanMethods = false)
public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware {}
1、欢迎页的处理规则
HandlerMapping:处理器映射,保存了每一个Handler能处理哪些请求。
利用反射向容器中注入WelcomePageHandlerMapping ,作用:寻找谁可以处理欢迎页的映射。
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,
FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(
new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),
this.mvcProperties.getStaticPathPattern()); // 获取spring.mvc.static-path-pattern 的值
welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider));
welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations());
return welcomePageHandlerMapping;
}
位置:org.springframework.boot.autoconfigure.web.servlet.WelcomePageHandlerMapping
final class WelcomePageHandlerMapping extends AbstractUrlHandlerMapping {
WelcomePageHandlerMapping(TemplateAvailabilityProviders templateAvailabilityProviders,
ApplicationContext applicationContext, Optional<Resource> welcomePage, String staticPathPattern) {
// 条件:欢迎页存在,并且路径是默认/**
if (welcomePage.isPresent() && "/**".equals(staticPathPattern)) {
logger.info("Adding welcome page: " + welcomePage.get());
setRootViewName("forward:index.html");
}
else if (welcomeTemplateExists(templateAvailabilityProviders, applicationContext)) {
// 调用Controller看谁能处理/index
logger.info("Adding welcome page template: index");
setRootViewName("index");
}
}
}
4.6、favicon
与代码无关,浏览器默认发送当前项目下的favicon功能,当添加访问静态资源前缀,就会找不到。