spring-source


springboot源码分析

yml加载流程

基础配置yml的导入

  1. 双击shift搜索web,auto,找到源码后进入分析
  1. 快捷键 ctrl + k + ctrl + 1 折叠代码
  2. 找到addResourceHandlers类,分析源码
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
    super.addResourceHandlers(registry);
    if (!this.resourceProperties.isAddMappings()) {
        logger.debug("Default resource handling disabled");
    } else {
        ServletContext servletContext = this.getServletContext();
        this.addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
        this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
            registration.addResourceLocations(this.resourceProperties.getStaticLocations());
            if (servletContext != null) {
                registration.addResourceLocations(new Resource[]{new ServletContextResource(servletContext, "/")});
            }

        });
    }
}

其作用域默认如下:

new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"}

满足yml自动装配流程:

继续分析可以发现在spring.web包下

@ConfigurationProperties("spring.web")

yml配置静态文件位置,达到:从指定位置获得静态文件 效果

spring.mvc.static-path-pattern=/**

文章作者: Hui3c
版权声明: 本博客所有文章除特別声明外,均采用 CC BY-ND 4.0 许可协议。转载请注明来源 Hui3c !
  目录