自动配置原理

  • xxxAutoConfiguration:给容器中自动配置组件
  • xxxProperties:封装配置文件的配置类

映射静态资源

Web自动配置类:WebMvcAutoConfiguration
Web配置的封装类:MvcProperties 和 ResourceProperties

配置和资源有关的参数:

  1. @ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
  2. public class ResourceProperties {
  3. private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
  4. "classpath:/resources/", "classpath:/static/", "classpath:/public/" };
  5. private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
  6. ...
  7. }

下面的代码是 WebMvcAutoConfiguration 类的静态资源加载方法的源码:

  1. // 静态资源配置类
  2. @Override
  3. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  4. if (!this.resourceProperties.isAddMappings()) {
  5. logger.debug("Default resource handling disabled");
  6. return;
  7. }
  8. Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
  9. CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
  10. if (!registry.hasMappingForPattern("/webjars/**")) {
  11. customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
  12. .addResourceLocations("classpath:/META-INF/resources/webjars/")
  13. .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
  14. }
  15. String staticPathPattern = this.mvcProperties.getStaticPathPattern();
  16. if (!registry.hasMappingForPattern(staticPathPattern)) {
  17. customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
  18. .addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
  19. .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
  20. }
  21. }

1、所有的 /webjars/** ,在 classpath:/META-INF/resources/webjars/ 中寻找资源:

  • webjars 网站:https://www.webjars.org/
  • WebJars 是将客户端(浏览器)网络资源库(例如 jQuery 或者 Bootstrap)打包成 JAR 文件,然后通过 maven 依赖导入,通过 localhost:8080/webjars/jquery/3.5.1/jquery.js 访问

image.png

2、String staticPathPattern = this.mvcProperties.getStaticPathPattern(); 在 MvcProperties 配置类找到该参数:

  1. /**
  2. * ResourceProperties 资源路径配置类中的一个参数
  3. */
  4. private String staticPathPattern = "/**";

getResourceLocations(this.resourceProperties.getStaticLocations()) 静态资源的默认位置在 ResourceProperties 的源码中可以找到:

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

自定义静态资源位置:通过 ResourceProperties 配置类中的 staticLocations 属性,因此,一旦自定义了资源位置,默认配置就会失效

  1. spring:
  2. resources:
  3. static-locations: /static/

3、欢迎页:静态资源文件夹的所有 index.html 页面,同时被 /** 映射

  1. /* WebMvcAutoConfiguration 的静态内部类 EnableWebMvcConfiguration 中的方法 */
  2. @Bean
  3. public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,
  4. FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
  5. WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(
  6. new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),
  7. this.mvcProperties.getStaticPathPattern());
  8. welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider));
  9. welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations());
  10. return welcomePageHandlerMapping;
  11. }
  12. private Optional<Resource> getWelcomePage() {
  13. String[] locations = getResourceLocations(this.resourceProperties.getStaticLocations());
  14. return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
  15. }
  16. private Resource getIndexHtml(String location) {
  17. // 拼凑出欢迎页的资源路径
  18. return this.resourceLoader.getResource(location + "index.html");
  19. }
  20. /* WelcomePageHandlerMapping */
  21. WelcomePageHandlerMapping(TemplateAvailabilityProviders templateAvailabilityProviders,
  22. ApplicationContext applicationContext, Optional<Resource> welcomePage, String staticPathPattern) {
  23. // 欢迎页的映射,/**会被重定向
  24. if (welcomePage.isPresent() && "/**".equals(staticPathPattern)) {
  25. logger.info("Adding welcome page: " + welcomePage.get());
  26. setRootViewName("forward:index.html");
  27. }
  28. else if (welcomeTemplateExists(templateAvailabilityProviders, applicationContext)) {
  29. logger.info("Adding welcome page template: index");
  30. setRootViewName("index");
  31. }
  32. }


4、图标资源:在静态资源文件下找
/favicon.ico