五、SpringBoot对静态资源的映射规则

1.所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找资源;

WebMvcAuotConfiguration.java
 public void addResourceHandlers(ResourceHandlerRegistry registry) {
            if (!this.resourceProperties.isAddMappings()) {
                logger.debug("Default resource handling disabled");
            } else {
                Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
                CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
                //静态资源文件夹映射,所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找资源
                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();
                //添加资源映射
                //private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
                if (!registry.hasMappingForPattern(staticPathPattern)) {
                    //setCachePeriod(this.getSeconds(cachePeriod))设置缓存时间
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
                }

            }
        }
@ConfigurationProperties(
    prefix = "spring.resources",
    ignoreUnknownFields = false
)
public class ResourceProperties {
  //可以设置和静态资源有关的参数,缓存时间等

​ webjars:以jar包的方式引入静态资源;

webjars官网


        
            org.webjars
            jquery
            3.3.1
        

五、SpringBoot对静态资源的映射规则_第1张图片

访问地址:http://localhost:8080/webjars/jquery/3.3.1/dist/jquery.js

2."/**" 访问当前项目的任何资源,都去静态资源的文件夹(staticLocations)找映射

"classpath:/META-INF/resources/", 
"classpath:/resources/",
"classpath:/static/", 
"classpath:/public/" 
"/":当前项目的根路径

访问地址:http://localhost:8080/asserts/js/Chart.min.js --->去静态资源文件夹里面找/asserts/js/Chart.min.js

3.欢迎页; 静态资源文件夹(staticLocations)下的所有index.html页面;被"/**"映射;

        //配置欢迎页映射
        @Bean
        public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext) {
            return new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
        }


        //getStaticPathPattern静态资源文件夹
        @Bean
        public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext) {
            return new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
        }

​ 访问地址:http://localhost:8080/   --->静态资源文件下找index页面

4.所有的 **/favicon.ico 都是在静态资源文件夹(staticLocations)下找;

        //配置图标映射   staticLocations
        @Configuration
        @ConditionalOnProperty(
            value = {"spring.mvc.favicon.enabled"},//可以用这个配置启用
            matchIfMissing = true
        )
        public static class FaviconConfiguration implements ResourceLoaderAware {
            private final ResourceProperties resourceProperties;
            private ResourceLoader resourceLoader;

            public FaviconConfiguration(ResourceProperties resourceProperties) {
                this.resourceProperties = resourceProperties;
            }

            public void setResourceLoader(ResourceLoader resourceLoader) {
                this.resourceLoader = resourceLoader;
            }

            @Bean
            public SimpleUrlHandlerMapping faviconHandlerMapping() {
                SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
                mapping.setOrder(-2147483647);
                mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", this.faviconRequestHandler()));
                return mapping;
            }

            @Bean
            public ResourceHttpRequestHandler faviconRequestHandler() {
                ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
                requestHandler.setLocations(this.resolveFaviconLocations());
                return requestHandler;
            }

            private List resolveFaviconLocations() {
                String[] staticLocations = WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter.getResourceLocations(this.resourceProperties.getStaticLocations());
                List locations = new ArrayList(staticLocations.length + 1);
                Stream var10000 = Arrays.stream(staticLocations);
                ResourceLoader var10001 = this.resourceLoader;
                this.resourceLoader.getClass();
                var10000.map(var10001::getResource).forEach(locations::add);
                locations.add(new ClassPathResource("/"));
                return Collections.unmodifiableList(locations);
            }
        }

5.staticLocations(静态资源文件夹)

默认:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/

也可以自己配置:在配置文件中用spring.resources.static-locations=classpath:/hello/,classpath:/atguigu/配置

你可能感兴趣的:(SpringBoot,SpringBoot内部原理)