SpringBoot进阶 (3)

eg:数据库jdbc开发:去Springboot- autoConfigtuation包下看DataSourceAutoConfiguration,头上有注解@EnableConfigurationProperties({DataSourceProperties.class})开启properties映射对象生效,DataSourceProperties是个映射properties的bean类


我们需要熟悉自动配置原理,然后才可以很好地书写配置文件


静态资源的映射规则

在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/**")) { // Webjars/**下的资源请求都去 classpath:/META-INF/resources/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)); } } }

webjars:以jar包方式引入静态资源(juery、BootStrap等打包成jar,用maven导入)

访问Webjars/**下的资源请求都去 classpath:/META-INF/resources/webjars/ 找资源,

导包后直接写名字访问就行eg: /webjars/jquery


1)addResourceHandlers

java哪些编写代码的文件和资源文件夹下的文件编译后都放在target的classes下,classes才是类路径。根路径是个特例,不在资源文件夹下,但编译后放在classes内

/** 默认去classpath找;Springboot的resourcers是默认的classpath

"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/" "http://www.likecs.com/": 当前项目的根路径,

3)欢迎页配置

欢迎页;静态资源文件夹下的所有Index

@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; } // 方法引用在静态资源下找index.html,那个方法引用不用看了 return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();

所以: localhost:8080/ 默认找资源类路径下的 index.html文件


图标也一样;静态资源下找 /favicon.ico







8. 模板引擎thymeleaf

1)加入依赖

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>

资源放在classpath:/templates/下,就自动渲染html


2)导入thymeleaf的名称空间、有语法提示

<html lang="en" xmlns:th="http://www.thymeleaf.org">

3)语法

th:text th:each 这个标签每次遍历都会生成 ${} 获取值 *{}





9. SpringMVC 自动配置

配置了ViewResolver视图解析器

配置了webjars解析

support 静态资源文件路径、webjars

support 静态首页访问 index.html

support favicon.ico

自动注册了Converter、Formatter的beans

Support th:each







10. 修改SpringBoot的默认配置

模式:

1)自动配置组件时(组合使用,互补),先看容器中有没有用户自己配置的@Component,@Bean,如果有就用用户配置,如果没有才自己创建自动配置

@ConditionalOnMissingBean({FormContentFilter.class}) public OrderedFormContentFilter formContentFilter() { return new OrderedFormContentFilter(); }

2)扩展SpringMVC(eg:Interceptors、formatters)

编写一个配置类加上注解@Configuration,实现WebMvcConfigurer接口要重写的配置即可(接口有方法体了),这里不能加@EnableWebMvc(加了就是自己的生效,自动配置失效)


内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zydsdg.html