(6)Spring Boot 与Web 开发

前提:    Spring Boot 2.2.5 与Web 开发  官网文档                                                                            

用好Spring Boot 的三板斧:

  •  创建Spring Boot 应用,选中我们需要的模块;
  • Spring Boot 已经默认将这些场景配置好了,只需要在配置文件中指定少量的配置就可以运行起来;
  • 自己编写业务代码;  

   官网目录参照:

                                                 

一、自动配置的原理?

  这个场景Spring Boot帮我们配置了什么? 能不能修改 ?能修改哪些配置? 能不能扩展? XXX

     

xxxxAutoConfiguration:帮我们给容器中自动配置组件;
xxxxProperties:配置类来封装配置文件的内容;

二、Spring Boot对静态资源的映射规则

设置和静态资源有关的参数;  WebMvcAutoConfiguration

@ConfigurationProperties(
    prefix = "spring.resources",
    ignoreUnknownFields = false
)
public class ResourceProperties {
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]
                         {"classpath:/META-INF/resources/",
                          "classpath:/resources/", 
                          "classpath:/static/",
                          "classpath:/public/"};
    private String[] staticLocations;

}


// 添加静态 资源文件路径配置
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(
                         WebMvcAutoConfiguration.getResourceLocations(
                         this.resourceProperties.getStaticLocations()))
                                 .setCachePeriod(this.getSeconds(cachePeriod))
                                 .setCacheControl(cacheControl));
                }

            }
        }


// 配置欢迎页 映射
  @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));
            return welcomePageHandlerMapping;
        }

(1)所有的  "/webjars/**"  ,都去 "classpath:/META-INF/resources/webjars/" 下找资源

         - webjars : 以jar包的形式引入静态资源                官网资源 

        <!--引入 Jquery 的webjars  在访问的时候只需要写webjars下面资源的名称即可-->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.4.1</version>
        </dependency>

               

(2)staticPathPattern = "/**"    访问当前项目的任何资源  【静态资源的文件夹】

  •  "classpath:/META-INF/resources/"
  •  "classpath:/resources/"
  •  "classpath:/static/",
  • "classpath:/public/"
  • "/" :当前项目的更路经

结论: 访问方式就是:localhost:8080/文件夹名称/***    去静态资源文件夹里面找 文件夹路径下面对应的静态资源

(3)欢迎页:静态资源文件夹下的所有index.html页面; 被 “ / ** ” 映射

      所以欢迎页就是在  localhost:8080/  找 index.html  

(4)favicon.ico图标配置   链接解析 springboot2.2.5 移除favicon.ico 图标的原因

        对此Spring Boot项目也提供了支持,但不同版本有所区别,在最新版本中的使用,网络上大多数文章已经失效,本篇文章带大家看一下Spring Boot 2.x版本中的使用情况。

Spring Boot不同版本对Favicon的支持

在早些版本中Spring Boot对Favicon进行了默认支持,并且通过如下配置进行关闭操作

spring.mvc.favicon.enabled=false ## 关闭

默认显示效果如下:

2. 关闭Favicon

3.自定义Favicon

将自定义的favicon.ico(文件名固定)放置在以下目录,可以自定义Favicon:

  • 类路径根目录
  • 类路径META-INF/resources/
  • 类路径resources/
  • 类路径static/
  • 类路径public/

总结: 

     但在Spring Boot项目的issues中提出,如果提供默认的Favicon可能会导致网站信息泄露。如果用户不进行自定义的Favicon的设置,而Spring Boot项目会提供默认的上图图标,那么势必会导致泄露网站的开发框架。

     因此,在Spring Boot2.2.x中,将默认的favicon.ico移除,同时也不再提供上述application.properties中的属性配置。更多详细信息可查看对应的issues:https://github.com/spring-projects/spring-boot/issues/17925

发布了108 篇原创文章 · 获赞 58 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_41893274/article/details/104761933