Springboot2.x——静态资源的访问

Springboot需要访问静态资源,主要看看资源文件的储存路径

springboot项目:找到该依赖自动配置Jar包

Springboot2.x——静态资源的访问_第1张图片

打开spring.factories,找到WebMvcAutoConfiguration

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
........
org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration,\
# web Auto Configure,web自动配置内容
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.reactive.WebSocketReactiveAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketMessagingAutoConfiguration,\
org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration,\
org.springframework.boot.autoconfigure.webservices.client.WebServiceTemplateAutoConfiguration

进入org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\

查看源码:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
   if (!this.resourceProperties.isAddMappings()) {
      logger.debug("Default resource handling disabled");
      return;
   }
   Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
   CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
   
   // 访问路径"/webjars/**"  文件在 classpath:/META-INF/resources/webjars/中
   if (!registry.hasMappingForPattern("/webjars/**")) {
      customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/")
            .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
   }
   // 访问路径"/**" 静态资源进行设置,getStaticLocations()
   String staticPathPattern = this.mvcProperties.getStaticPathPattern();
   if (!registry.hasMappingForPattern(staticPathPattern)) {
      customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
            .addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
            .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
   }
}

查看getStaticLocations()方法,staticLocations = CLASSPATH_RESOURCE_LOCATIONS,可以看到就是默认的几种路径

@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties {

   private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
         "classpath:/resources/", "classpath:/static/", "classpath:/public/" };

   /**
    * Locations of static resources. Defaults to classpath:[/META-INF/resources/,
    * /resources/, /static/, /public/].
    */
   private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;

   public String[] getStaticLocations() {
      return this.staticLocations;
   }
   .......
}  

1、测试默认路径静态资源访问:

可以看到springboot默认读取spring.resources开头以下路径的的这些资源,默认顺序就是这个顺序,我这边暂时不验证了

classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/

下面进行测试,将本地一些资源文件放入:

Springboot2.x——静态资源的访问_第2张图片

启动springboot,使用浏览器依次访问:

Springboot2.x——静态资源的访问_第3张图片

测试通过

 

2、修改默认静态资源路径

修改application.properties

spring.resources.static-locations=classpath:/public,classpath:/static

再次尝试访问各个资源文件,classpath:/META-INF/resources/并没影响访问(待确认),classpath:/resources/不能访问了

Springboot2.x——静态资源的访问_第4张图片

 

3、其他一些路径设置

#请求路径前面统一添加/WxTesting
server.servlet.context-path=/WxTesting

#将静态资源设置到本地磁盘路径
spring.resources.static-locations=file:/Users/wuxi/Downloads/jmeter/reports/

重启springboot 测试一下,只有meta-inf.html

Springboot2.x——静态资源的访问_第5张图片 

 

 

 

你可能感兴趣的:(SpringBoot)