2.3.2 网页小图标
不管是默认的还是自定义的静态资源路径,只要在下面添加一个命名为favicon.ico的图片,即可将该图片设置成网页的小图标。同时需要注意的是,不能自定义静态资源的访问方式,否则就会导致上述效果失效。
2.4 web场景源码浅析
生效自动配置类:
SpringBoot启动的时候默认加载xxxAutoConfiguration类(自动配置类)是相关场景生效,SpringMVC功能的自动配置类是 WebMvcAutoConfiguration
配置绑定:
@EnableConfigurationProperties注解将配置文件的相关属性和xxx配置类进行了绑定,可以用于更改默认的配置信息。而通过分析源码可知在WebMvcAutoConfiguration自动配置类里的WebMvcAutoConfigurationAdapter静态类上有如下注解,注解参数中有两个配置类,其中配置类与配置文件中的属性绑定关系如下:WebMvcProperties.class --> spring.mvc WebProperties.class --> spring.web
@EnableConfigurationProperties({WebMvcProperties.class, WebProperties.class})
源码中资源处理器的解析:
在SpringBoot的源码中有一个规则:有参构造器所有参数的值都会从容器中拿,于是在WebMvcAutoConfigurationAdapter静态类里的有参构造器中,所有的参数都将直接从容器中获取。
//ResourceProperties resourceProperties --> 获取和spring.resources绑定的所有的值的对象 //WebMvcProperties mvcProperties --> 获取和spring.mvc绑定的所有的值的对象 //ListableBeanFactory beanFactory --> Spring的beanFactory //HttpMessageConverters --> 找到所有的HttpMessageConverters //ResourceHandlerRegistrationCustomizer --> 找到资源处理器的自定义(重点解析) //DispatcherServletPath --> 找到资源的路径 //ServletRegistrationBean --> 给应用注册Servlet、Filter.... public WebMvcAutoConfigurationAdapter(ResourceProperties resourceProperties, WebMvcProperties mvcProperties, ListableBeanFactory beanFactory, ObjectProvider<HttpMessageConverters> messageConvertersProvider, ObjectProvider<ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizerProvider, ObjectProvider<DispatcherServletPath> dispatcherServletPath, ObjectProvider<ServletRegistrationBean<?>> servletRegistrations) { this.resourceProperties = resourceProperties; this.mvcProperties = mvcProperties; this.beanFactory = beanFactory; this.messageConvertersProvider = messageConvertersProvider; this.resourceHandlerRegistrationCustomizer = resourceHandlerRegistrationCustomizerProvider.getIfAvailable(); this.dispatcherServletPath = dispatcherServletPath; this.servletRegistrations = servletRegistrations; }
通过对WebMvcAutoConfigurationAdapter静态类里的addResourceHandlers方法解析,得知资源解析器中资源处理的默认规则
public void addResourceHandlers(ResourceHandlerRegistry registry) { // 判断resource.add-mappings的值true/false(默认为true,可以在配置文件修改配置项) if (!this.resourceProperties.isAddMappings()) { // resource.add-mappings的值为false,则禁用所有的静态资源的访问规则,debug日志输出下面这句话 logger.debug("Default resource handling disabled"); return; } // resource.add-mappings的值true的话,定义规则 // 定义缓存的相关配置 Duration cachePeriod = this.resourceProperties.getCache().getPeriod(); CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl(); // 定义webjars的资源存储规则和缓存时间 if (!registry.hasMappingForPattern("/webjars/**")) { customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/") .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl)); } // 定义静态资源的访问规则 // this.mvcProperties.getStaticPathPattern() --> 获取静态资源的访问方式,默认是\/** String staticPathPattern = this.mvcProperties.getStaticPathPattern(); if (!registry.hasMappingForPattern(staticPathPattern)) { customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern) // getStaticLocations()方法中 .addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())) .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl)); } }
源码中欢迎页的解析:
@Bean public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) { // 下面 welcomePageHandlerMapping 的有参函数在下面图片 WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern()); welcomePageHandlerMapping.setInterceptors(this.getInterceptors(mvcConversionService, mvcResourceUrlProvider)); welcomePageHandlerMapping.setCorsConfigurations(this.getCorsConfigurations()); return welcomePageHandlerMapping; }
下图解释了为什么:自定义静态资源的访问方式会导致欢迎页无法起到作用。SpringBoot的源码中已经将这部分条件写死了。