SpringBoot全家桶(2)
1. 静态资源导入探究
1.1 底层源码
静态资源底层源码在WebMvcAutoConfiguration最下面的有个添加静态资源addResourceHandlers()方法
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
} else {
Duration cachePeriod this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
if (!registry.hasMappingForPattern("/webjars/**")) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{
"/webjars/**"}).addResourceLocations(new String[]{
"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{
staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}}
第一个 if 表示如果静态资源被自定义了就直接 return 底层的默认无效了。
怎么自定义呢 首先在WebMvcAutoConfiguration中找到WebMvcProperties点进去找到this.staticPathPattern = “/**”;一看就知道关于静态资源的在application.properties写入spring.mvc.static-path-pattern=/wxz/非常不推荐自定义。
第二个if (第一种方式)表示如果patten中有webjars 就会在classpath:/META- INF/resources/webjars/这个路径下找。
百度搜索webjars 比如要用jquery就导入官方依赖:
<dependency>
<groupId>org.webjars.npm</groupId>
<artifactId>jquery</artifactId>
<version>3.7.0</version>
</dependency>
此时可以直接访问http://localhost8080/webjars/jquery/3.4.1/jquery.js
第三个if (第二种方式)表示如果符合底层指定的目录,“classpath:/META-INF/resources/”, 跟webjars一样。
下面三个是指在resources目录下可以创建的文件夹resources或static或public
“classpath:/resources/”,
“classpath:/static/”,
“classpath:/public/”
还有一个 "/"**
1.2 优先级
分别在resources,static,public 目录下创建1.jsp 分别输入hello1,hello2,hello3
结果优先级为 resources > static > public
1.3 首页定制
在WebMvcAutoConfiguration中找到:
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
welcomePageHandlerMapping.setInterceptors(this.getInterceptors(mvcConversionService, mvcResourceUrlProvider));
return welcomePageHandlerMapping;
}
private Optional<Resource> getWelcomePage() {
String[] locations = WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations());
return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
}
private Resource getIndexHtml(String location) {
return this.resourceLoader.getResource(location + "index.html");
}
private boolean isReadable(Resource resource) {
try {
return resource.exists() && resource.getURL() != null;
<