静态资源原理和欢迎页原理

静态资源原理和欢迎页原理,第1张

静态资源原理和欢迎页原理

文章目录
        • 欢迎页原理
        • 静态资源原理

关于静态资源原理和欢迎页原理,答案尽在spring-boot-autoconfigure-2.6.2.jar的org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration这个类里。

欢迎页原理
@Configuration(
    proxyBeanMethods = false
)
@EnableConfigurationProperties({WebProperties.class})
public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware {
    private final Resources resourceProperties;
    private final WebMvcProperties mvcProperties;
    private final WebProperties webProperties;
    private final ListableBeanFactory beanFactory;
    private final WebMvcRegistrations mvcRegistrations;
    private ResourceLoader resourceLoader;

    public EnableWebMvcConfiguration(WebMvcProperties mvcProperties, WebProperties webProperties, ObjectProvider mvcRegistrationsProvider, ObjectProvider resourceHandlerRegistrationCustomizerProvider, ListableBeanFactory beanFactory) {
        this.resourceProperties = webProperties.getResources();
        this.mvcProperties = mvcProperties;
        this.webProperties = webProperties;
        this.mvcRegistrations = (WebMvcRegistrations)mvcRegistrationsProvider.getIfUnique();
        this.beanFactory = beanFactory;
    }

    @Bean
    public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
        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;
    }
}
  • @Configuration,故 EnableWebMvcConfiguration 是一个配置类。
    配置类如果只有一个有参构造函数,则构造函数的参数值全部从容器中获取。即参数WebMvcProperties mvcProperties、WebProperties webProperties的值全部从容器中获取。
  • @EnableConfigurationProperties({WebProperties.class}),故EnableWebMvcConfiguration类的属性与类WebProperties绑定,可通过配置文件进行设置。

进入WebProperties,前缀为spring.web。

@ConfigurationProperties("spring.web")
public class WebProperties {}

配置文件(如application.yml)中可对如下属性进行配置。

其中,
spring.web.resources.add-mappings,默认是true,如果设置为false,将禁用静态资源。
spring.web.resources.static-locations,默认静态资源目录为/static(或/public或/resources或/meta-INF/resources),当然也可以自定义。
spring.web.resources.cache.period,设置静态资源缓存时长,单位是秒。

在方法welcomePageHandlerMapping处打断点,调试看看。

静态资源原理
@Configuration(
   proxyBeanMethods = false
)
@import({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class})
@EnableConfigurationProperties({WebMvcProperties.class, WebProperties.class})
@Order(0)
public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ServletContextAware {
   private static final Log logger = LogFactory.getLog(WebMvcConfigurer.class);
   private final Resources resourceProperties;
   private final WebMvcProperties mvcProperties;
   private final ListableBeanFactory beanFactory;
   private final ObjectProvider messageConvertersProvider;
   private final ObjectProvider dispatcherServletPath;
   private final ObjectProvider> servletRegistrations;
   private final WebMvcAutoConfiguration.ResourceHandlerRegistrationCustomizer resourceHandlerRegistrationCustomizer;
   private ServletContext servletContext;

   public WebMvcAutoConfigurationAdapter(WebProperties webProperties, WebMvcProperties mvcProperties, ListableBeanFactory beanFactory, ObjectProvider messageConvertersProvider, ObjectProvider resourceHandlerRegistrationCustomizerProvider, ObjectProvider dispatcherServletPath, ObjectProvider> servletRegistrations) {
       this.resourceProperties = webProperties.getResources();
       this.mvcProperties = mvcProperties;
       this.beanFactory = beanFactory;
       this.messageConvertersProvider = messageConvertersProvider;
       this.resourceHandlerRegistrationCustomizer = (WebMvcAutoConfiguration.ResourceHandlerRegistrationCustomizer)resourceHandlerRegistrationCustomizerProvider.getIfAvailable();
       this.dispatcherServletPath = dispatcherServletPath;
       this.servletRegistrations = servletRegistrations;
       this.mvcProperties.checkConfiguration();
   }

   public void addResourceHandlers(ResourceHandlerRegistry registry) {
       if (!this.resourceProperties.isAddMappings()) {
           logger.debug("Default resource handling disabled");
       } else {
           this.addResourceHandler(registry, "/webjars/**", "classpath:/meta-INF/resources/webjars/");
           this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
               registration.addResourceLocations(this.resourceProperties.getStaticLocations());
               if (this.servletContext != null) {
                   ServletContextResource resource = new ServletContextResource(this.servletContext, "/");
                   registration.addResourceLocations(new Resource[]{resource});
               }

           });
       }
   }

   private void addResourceHandler(ResourceHandlerRegistry registry, String pattern, Consumer customizer) {
       if (!registry.hasMappingForPattern(pattern)) {
           ResourceHandlerRegistration registration = registry.addResourceHandler(new String[]{pattern});
           customizer.accept(registration);
           registration.setCachePeriod(this.getSeconds(this.resourceProperties.getCache().getPeriod()));
           registration.setCacheControl(this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl());
           registration.setUseLastModified(this.resourceProperties.getCache().isUseLastModified());
           this.customizeResourceHandlerRegistration(registration);
       }
   }
}
  • @Configuration,故 WebMvcAutoConfigurationAdapter 是一个配置类。
    配置类如果只有一个有参构造函数,则构造函数的参数值全部从容器中获取。即参数WebProperties webProperties、WebMvcProperties mvcProperties等值全部从容器中获取。
  • @EnableConfigurationProperties({WebMvcProperties.class, WebProperties.class}),故WebMvcAutoConfigurationAdapter类的属性与类WebProperties、WebMvcProperties绑定,可通过配置文件进行设置。
    WebProperties,刚刚在欢迎页的时候看过了。
    接下来看看WebMvcProperties。

进入WebMvcProperties,前缀为spring.mvc。

@ConfigurationProperties(
    prefix = "spring.mvc"
)
public class WebMvcProperties {}

配置文件(如application.yml)可对如下属性进行配置。


其中,spring.mvc.static-path-pattern用来设置静态资源访问路径,默认是/**。

在方法addResourceHandlers处打断点,调试瞧瞧。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5683172.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存