Spring Boot
<!-- --> sprint-boot-starter-parent <!-- 场景启动器;帮助导入web等模块正常运行所依赖的组件--> spring-boot-starter spring-boot-starter-web
<!-- @SpringBootApplication标注这个类是SpringBoot的主配置类,SpringBoot运行这个类的main方法来启动SpringBoot应用-->
Spring Boot配置文件
application.properties和application.yml都是全局配置文件
yaml语法
#1.字符串
1.1 双引号""
1.2 单引号'',表示忽略里面的转义字符,是什么则输出什么
#2.对象
friends:
lastName: zhangsan
age: 18
friends: {lastName: zhangsan,age: 18}
#3.数组(List,Set)
pets:
-cat
-dog
-pig
pets: [cat,dog,pig]
静态资源
优先级 : resources>static(默认)>public
配置文件
springboot启动时会加载大量的自动配置类
我们需要的功能有没有springboot默认写好的自动配置类
我们看看这个自动配置类使用类哪些组件
给容器中自动配置类添加组件, 会从properties类中获取某些属性,我们就可以在配置文件中指定这些属性的值
xxxAutoConfiguration : 自动配置类
给容器添加组件
xxxProperties : 封装配置文件中的相关属性
debug模式
在配置文件中debug=true
日志选择
日志门面(抽象层) : SLF4J
日志实现 : Logback
SpringBoot对静态资源的映射规则
WebAutoConfiguration
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));
}
}
}
所有的/webjars/**, 都去classpath://META_INF/resources/webjars/找资源
http://localhost:8080/webjars/jquery/3.5.0/jquery.js
在访问的时候只需要写webjars下面资源的名称即可
静态资源文件夹
ResourceProperties.java
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/",
"classpath:/public/" };
"/":当前项目的根路径
欢迎页: 静态资源文件夹下所有的index.html
在public文件夹下创建一个index.html文件, 则访问http://localhost:8080的时候默认访问index.html
引入模板引擎thymeleaf
只要我们把HTML页面放在classpath:/templates/ ,thymeleaf就能自动渲染
<html lang="en" xmlns:th="http://www.thymeleaf.org">
整合JDBC
springcloud
entities
主实体类
Json封装体CommonResult
外部化配置
应用场景:线上应用如何快速修改配置,并应用最新配置?
由于 SpringBoot 应用的配置优先级,只需要在 jar 应用所在的文件夹下放置一个 application.properties 配置文件,重启项目就可以自动应用最新的配置。
配置优先级
配置文件优先级
- jar 包外的配置文件 > jar 包内的配置文件
- config 目录的配置文件 > 非 config 目录的配置文件(包外>包内,距离越远优先级越高)
- {profile}的配置文件 > 默认的配置文件
- properties后缀的配置文件 > yml后缀的配置文件
导入文件