zoukankan      html  css  js  c++  java
  • SpringBoot学习笔记

    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应用还提供了很多其他配置,具体可以参考源码或者官网。


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

  • 相关阅读:
    git branch用法总结
    vue-router异步加载组件
    vue错误提示 Cannot read property 'beforeRouteEnter' of undefined,刷新后跳到首页
    websocket常见错误
    Websocket原理
    怎么在overflow-y:sroll的情况下 隐藏滚动条
    URI和URL有什么区别
    确定浏览器是否支持某些DOM模块
    将nodeList转换为数组(兼容性)
    软件的三种版本
  • 原文地址:https://www.cnblogs.com/jerryyj/p/9621569.html
Copyright © 2011-2022 走看看