SpringBoot自学好几天 中途开始写笔记 SpringBoot Web开发 静态资源默认地址 和地址修改 20190120

版权声明:本文为博主原创文章,未经博主允许不得转载,如需转载请在明显处标明出处! https://blog.csdn.net/qq_36291682/article/details/86560653

SpringBoot web开发

springboot使用:
1)创建SpringBoot应用,选择需要的模块
在这里插入图片描述
2)SpringBoot 已经默认将这些选中的场景配置好了(之前学的自动配置功能),只需要在配置文件中指定少量配置就可以运行起来
3)自己编写业务代码

自动配置原理?
某个SpringBoot场景能为我们配置什么,能不能修改,能修改哪些配置,能不能扩展

xxxxxAutoConfiguration 帮我们给容器中自动配置组件 
xxxxxProperties 配置类 来封装配置文件的内容

SpringBoot对静态资源的映射规则

@ConfigurationProperties(
    prefix = "spring.resources",
    ignoreUnknownFields = false
)
public class ResourceProperties {
//可以设置和静态资源有关的参数,缓存时间等 
	    //WebMvcAutoConfiguration 类中的
        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();
                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();
                if (!registry.hasMappingForPattern(staticPathPattern)) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
                }

            }
        }

1) 所有/webjars/* 下的所有请求都去classpath:/META-INF/resources/webjars/ 这里边找资源
webjars:以jar包的方式 引入静态资源 https://www.webjars.org/
在这里插入图片描述
访问:http://127.0.0.1:8080/webjars/jquery/3.0.0/jquery.js 就能访问到jquery
2)/** 访问当前的任何资源 如果没有人处理(我理解没人处理的意思就是不是请求后台方法那种请求 没有映射到后台方法的请求)就会去找 下边这几个路径
静态资源文件夹:

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

在这里插入图片描述
http://127.0.0.1:8080/asserts/js/bootstrap.min.js

		 //WebMvcAutoConfiguration类中的方法
		 //欢迎页映射
		 @Bean
        public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext) {
            return new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
        }

静态资源文件夹下的所有 index.html 页面 被"/**" 映射

http://127.0.0.1:8080 就会去找index
在这里插入图片描述
访问:http://127.0.0.1:8080
在这里插入图片描述

		
			//WebMvcAutoConfiguration类中的方法
			//配置网站图标
			@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<Resource> resolveFaviconLocations() {
                String[] staticLocations = WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter.getResourceLocations(this.resourceProperties.getStaticLocations());
                List<Resource> 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);
            }
        }

**/favicon.ico 图标方式的位置还是静态资源文件夹下 与 index首页一样

如果想使用自己配置的文件夹:
spring.resources.static-locations=classpath:/test/,classpath:/static2/
自己定义之后就不能使用默认的了,就需要把所有静态资源放到自己定义的路径下

猜你喜欢

转载自blog.csdn.net/qq_36291682/article/details/86560653