SpringBoot中的静态资源访问的实现⼀、说在前⾯的话
我们之间介绍过SpringBoot⾃动配置的原理,基本上是如下:
xxxxAutoConfiguration:帮我们给容器中⾃动配置组件;
xxxxProperties:配置类来封装配置⽂件的内容;
⼆、静态资源映射规则
1、对哪些⽬录映射?
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
/:当前项⽬的根路径
2、什么意思?
3、为什么是那⼏个⽬录?
3.1、看源码
我们⼀起来读下源码,这个是SpringBoot⾃动配置的WebMvcAutoConfiguration.java类来帮我们⼲的。
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!sourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
Integer cachePeriod = CachePeriod();
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(
registry.addResourceHandler("/webjars/**")
.addResourceLocations(
"classpath:/META-INF/resources/webjars/")
.setCachePeriod(cachePeriod));
}
String staticPathPattern = StaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(
registry.addResourceHandler(staticPathPattern)
.java中index是什么意思
addResourceLocations(
.setCachePeriod(cachePeriod));
}
}
3.2、分析源码
我们重点分析后半截,前半截后⾯会介绍。
// staticPathPattern是/**
String staticPathPattern = StaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(
registry.addResourceHandler(staticPathPattern)
.addResourceLocations(
.setCachePeriod(cachePeriod));
}
========>
ResourceProperties
public String[] getStaticLocations() {
return this.staticLocations;
}
========>
private String[] staticLocations = RESOURCE_LOCATIONS;
========>
private static final String[] RESOURCE_LOCATIONS;
private static final String[] SERVLET_RESOURCE_LOCATIONS = { "/" };
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/" };
========>
static {
// 可以看到如下是对上⾯两个数组进⾏复制操作到⼀个新数组上,也就是合并。
RESOURCE_LOCATIONS = new String[CLASSPATH_RESOURCE_LOCATIONS.length
+ SERVLET_RESOURCE_LOCATIONS.length];
System.arraycopy(SERVLET_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS, 0,
SERVLET_RESOURCE_LOCATIONS.length);
System.arraycopy(CLASSPATH_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS,
SERVLET_RESOURCE_LOCATIONS.length, CLASSPATH_RESOURCE_LOCATIONS.length);
}
所以上述代码经过我的翻译后成为了如下样⼦:
registry.addResourceHandler("/**").addResourceLocations(
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/", "/")
/
/ 设置缓存时间
.setCachePeriod(cachePeriod));
3.3、⼀句话概括
WebMvcAutoConfiguration类⾃动为我们注册了如下⽬录为静态资源⽬录,也就是说直接可访问到资源的⽬录。
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
/:当前项⽬的根路径
优先级从上到下。
所以,如果static⾥⾯有个index.html,public下⾯也有个index.html,则优先会加载static下⾯的index.html,因为优先级!
4、默认⾸页
PS:就是直接输⼊ip:port/项⽬名称默认进⼊的页⾯。
4.1、看源码
WebMvcAutoConfiguration.java
public WelcomePageHandlerMapping welcomePageHandlerMapping(
ResourceProperties resourceProperties) {
return new WelcomePage(),
StaticPathPattern());
}
4.2、分析源码
========>
public Resource getWelcomePage() {
// 遍历默认静态资源⽬录后⾯拼接个index.html的数组
// ⽐如:[/static/index.html, /public/index.html等等]
for (String location : getStaticWelcomePageLocations()) {
Resource resource = Resource(location);
try {
if (ists()) {
return resource;
}
}
catch (Exception ex) {
// Ignore
}
}
return null;
}
========>
// 下⾯这段代码通俗易懂,就是给默认静态资源⽬录后⾯拼接个index.html并返回,⽐如:/static/index.html
private String[] getStaticWelcomePageLocations() {
String[] result = new String[this.staticLocations.length];
for (int i = 0; i < result.length; i++) {
String location = this.staticLocations[i];
if (!dsWith("/")) {
location = location + "/";
}
result[i] = location + "index.html";
}
return result;
}
所以上述代码经过我的翻译后成为了如下样⼦:
return new WelcomePageHandlerMapping(
"classpath:/META-INF/resources/index.html",
"classpath:/resources/index.html",
"classpath:/static/index.html",
"classpath:/public/index.html",
"/index.html"
, "/**");
4.3、⼀句话概括
WebMvcAutoConfiguration类⾃动为我们注册了如下⽂件为默认⾸页。
classpath:/META-INF/resources/index.html
classpath:/resources/index.html
classpath:/static/index.html
classpath:/public/index.html
/index.html
优先级从上到下。
所以,如果static⾥⾯有个index.html,public下⾯也有个index.html,则优先会加载static下⾯的index.html,因为优先级!
5、favicon.ico
PS:就是这个图标。
5.1、看源码
@Configuration
@ConditionalOnProperty(value = "spring.abled", matchIfMissing = true)
public static class FaviconConfiguration {
private final ResourceProperties resourceProperties;
public FaviconConfiguration(ResourceProperties resourceProperties) {
}
@Bean
public SimpleUrlHandlerMapping faviconHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico",
faviconRequestHandler()));
return mapping;
}
@Bean
public ResourceHttpRequestHandler faviconRequestHandler() {
ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
requestHandler
.FaviconLocations());
return requestHandler;
}
}
5.2、分析源码
// ⾸先可以看到的是可以设置是否⽣效,通过参数spring.abled来配置,若⽆此参数,则默认是⽣效的。
@ConditionalOnProperty(value = "spring.abled", matchIfMissing = true)
========》
// 可以看到所有的**/favicon.ico都是在faviconRequestHandler()这个⽅法⾥。
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", faviconRequestHandler()));
========》
faviconRequestHandler().FaviconLocations()
// 就是之前的五个静态资源⽂件夹。
List<Resource> getFaviconLocations() {
List<Resource> locations = new ArrayList<Resource>(
this.staticLocations.length + 1);
if (sourceLoader != null) {
for (String location : this.staticLocations) {
locations.Resource(location));
}
}
locations.add(new ClassPathResource("/"));
return Collections.unmodifiableList(locations);
5.3、⼀句话概括
只要把favicon.ico放到如下⽬录下,就会⾃动⽣效。
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
/:当前项⽬的根路径
6、webjars
6.1、看源码
WebMvcAutoConfiguration
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!sourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
Integer cachePeriod = CachePeriod();
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(
registry.addResourceHandler("/webjars/**")
.addResourceLocations(
"classpath:/META-INF/resources/webjars/")
.setCachePeriod(cachePeriod));
}
String staticPathPattern = StaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(
registry.addResourceHandler(staticPathPattern)
.addResourceLocations(
.setCachePeriod(cachePeriod));
}
}
6.2、分析源码
这次我们来分析前半截。
Integer cachePeriod = CachePeriod();
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(
registry.addResourceHandler("/webjars/**")
.addResourceLocations(
"classpath:/META-INF/resources/webjars/")
.setCachePeriod(cachePeriod));
}
6.3、⼀句话概括
所有/webjars/**都从classpath:/META-INF/resources/webjars/路径下去对应的静态资源。
6.4、什么是webjars?
就是以jar包的⽅式引⼊静态资源。
我们可以做个例⼦,将jquery引⼊到项⽬中
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>
</dependency>
看项⽬依赖
会⾃动为我们引⼊jquery,要怎么使⽤呢?我们上⾯说过:
所有/webjars/*都从classpath:/META-INF/resources/webjars/路径下去对应的静态资源。必须在这⼏个路径下SpringBoot才会扫描到!
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/":当前项⽬的根路径
到此这篇关于SpringBoot中的静态资源访问的实现的⽂章就介绍到这了,更多相关SpringBoot 静态资源访问内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。