SpringBoot学习笔记 - web开发(web相关配置)

springboot为我们的web开发做了很多自动配置(见下左图,摘自springboot官网), 在  WbMvcAutoConfiguration中可以看出,在该类中有一个内部类: WebMvcAutoConfigurationAdapter,这个适配器做了大部分的工作。

       

 

这里介绍一些简单的配置信息,如下代码配置了类路径下的静态资源的访问和webjar的访问。

public void addResourceHandlers(ResourceHandlerRegistry registry) {
            if(!this.resourceProperties.isAddMappings()) {
                logger.debug("Default resource handling disabled");
            } else {
                Integer cachePeriod = this.resourceProperties.getCachePeriod();
                if(!registry.hasMappingForPattern("/webjars/**")) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(cachePeriod));
                }

                String staticPathPattern = this.mvcProperties.getStaticPathPattern();
                if(!registry.hasMappingForPattern(staticPathPattern)) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(this.resourceProperties.getStaticLocations()).setCachePeriod(cachePeriod));
                }

            }
        }

如下提供了favicon的配置(这里的icon就是网页标题旁边的图标),如果不想用spring自带的icon,可以在类路径下的static或者public或者其他有效目录中设置一个favicon.ico(注意文件名要一致)来代替默认的。

public static class FaviconConfiguration {
            private final ResourceProperties resourceProperties;

            public FaviconConfiguration(ResourceProperties resourceProperties) {
                this.resourceProperties = resourceProperties;
            }

            @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.resourceProperties.getFaviconLocations());
                return requestHandler;
            }
        }

我们也可以不用springboot内嵌的tomcat服务器,要实现这样的功能很简单,只要在pom.xml文件中的starter-web中的dependency节点中使用 exclusions   将默认的服务器剔除,在引入我们新的服务器即可,比如将tomcat换成jetty:

                <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-starter-tomcat</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jetty</artifactId>
		</dependency>

springboot为web应用还提供了很多其他配置,具体可以参考源码或者官网。


如有错误,欢迎指正,不胜感激。

posted @ 2017-09-07 16:48  JerryYJ  阅读(156)  评论(0编辑  收藏  举报