SpringBoot static resource category

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/qq_34579060/article/details/89511800

SpringBoot for automated configuration SpringMVC are in  WebMvcAutoConfiguration class.

WebMvcAutoConfiguration class

There is a static inner classes  WebMvcAutoConfigurationAdapter realized

 

WebMvcConfigurer of addResourceHandlers to configure a static resource filter.

if (!registry.hasMappingForPattern(staticPathPattern)) {
   customizeResourceHandlerRegistration(
         registry.addResourceHandler(staticPathPattern)
               .addResourceLocations(getResourceLocations(
                     this.resourceProperties.getStaticLocations()))
               .setCachePeriod(getSeconds(cachePeriod))
               .setCacheControl(cacheControl));
}

Location static resource definition:

@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;
   }

   public void setStaticLocations(String[] staticLocations) {
      this.staticLocations = appendSlashIfNecessary(staticLocations);
   }

   private String[] appendSlashIfNecessary(String[] staticLocations) {
      String[] normalized = new String[staticLocations.length];
      for (int i = 0; i < staticLocations.length; i++) {
         String location = staticLocations[i];
         normalized[i] = location.endsWith("/") ? location : location + "/";
      }
      return normalized;
   }

Guess you like

Origin blog.csdn.net/qq_34579060/article/details/89511800