淘宝导购网站备案,北京app定制开发公司,数据库网站建设软件,装修网站设计案例这里写目录标题 一.介绍1.为什么依赖不需要写版本#xff1f;2.启动器(Starter)是何方神圣#xff1f;3.SpringBootApplication注解的功效#xff1f;4.启动源码5.如何学好SpringBoot 二.SpringBoot3配置文件2.1属性配置文件使用2.2 YAML配置文件使用2.3 YAML配置文件使用2.… 这里写目录标题 一.介绍1.为什么依赖不需要写版本2.启动器(Starter)是何方神圣3.SpringBootApplication注解的功效4.启动源码5.如何学好SpringBoot 二.SpringBoot3配置文件2.1属性配置文件使用2.2 YAML配置文件使用2.3 YAML配置文件使用2.3 批量配置文件注入2.4 多环境配置和使用 三.整合SpringMVC3.1自动配置3.2默认配置3.3 web相关配置3.4 静态资源处理WebMvcAutoConfiguration原理1. 生效条件2.效果3.WebMvcConfigurer接口4.静态资源规则源码 3.4 自定义拦截器(SpringMVC配置)3.5内容协商3.6错误处理最佳实战 3.7嵌入容器1.自动配置原理2.最佳实践  四.整合Druid数据源五.整合Mybatis5.1自动配置5.3 声明式事务整合配置5.4 AOP整合配置 六.基础特性6.1SpringApplication1.自定义banner2.自定义SpringApplication3.FluentBuilder API启动 6.2profile1. 使用2. Profile 分组3. Profile 配置文件 6.3外部化配置1.优先级配置2.外部配置3.导入配置4. 属性占位符 6.4单元测试组件测试注解    一.介绍 
但是无论使用XML、注解、Java配置类还是他们的混合用法你都会觉得配置文件过于复杂和繁琐让人头疼 
SpringBoot 帮我们简单、快速地创建一个独立的、生产级别的 Spring 应用说明SpringBoot底层是Spring大多数 SpringBoot 应用只需要编写少量配置即可快速整合 Spring 平台以及第三方技术 
SpringBoot的主要目标是 
为所有 Spring 开发提供更快速、可广泛访问的入门体验。开箱即用设置合理的默认值但是也可以根据需求进行适当的调整。提供一系列大型项目通用的非功能性程序如嵌入式服务器、安全性、指标、运行检查等。约定大于配置基本不需要主动编写配置类、也不需要 XML 配置文件。 
总结简化开发简化配置简化整合简化部署简化监控简化运维。 
1.为什么依赖不需要写版本 
每个boot项目都有一个父项目spring-boot-starter-parentparent的父项目是spring-boot-dependencies父项目 版本仲裁中心把所有常见的jar的依赖版本都声明好了。比如mysql-connector-j 
2.启动器(Starter)是何方神圣 
导入starter-web导入了web开发场景 
1、场景启动器导入了相关场景的所有依赖starter-json、starter-tomcat、springmvc2、每个场景启动器都引入了一个spring-boot-starter核心场景启动器。3、核心场景启动器引入了spring-boot-autoconfigure包。4、spring-boot-autoconfigure里面囊括了所有场景的所有配置。5、只要这个包下的所有类都能生效那么相当于SpringBoot官方写好的整合功能就生效了。6、SpringBoot默认却扫描不到 spring-boot-autoconfigure下写好的所有配置类。这些配置类给我们做了整合操作默认只扫描主程序所在的包。 
3.SpringBootApplication注解的功效 
 SpringBootApplication添加到启动类上是一个组合注解他的功效由具体的子注解实现 
SpringBootConfiguration//1.配置类
EnableAutoConfiguration//2.自动加载其他的配置类
ComponentScan//3.扫描包
public interface SpringBootApplication {} 
SpringBootApplication注解起到以下几个主要作用 
1.自动配置SpringBootApplication注解包含了EnableAutoConfiguration注解用于启用Spring Boot的自动配置机制。 SpringBoot默认只能扫描自己主程序所在的包及其下面的子包扫描不到 spring-boot-autoconfigure包中官方写好的配置类  EnableAutoConfigurationSpringBoot 开启自动配置的核心。   是由Import(AutoConfigurationImportSelector.class)提供功能批量给容器中导入组件。  SpringBoot启动会默认加载 152个配置类。  这152个配置类来自于spring-boot-autoconfigure下 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件指定的 项目启动的时候利用 Import 批量导入组件机制把 autoconfigure 包下的152 xxxxAutoConfiguration类导入进来自动配置类虽然导入了152个自动配置类  按需生效   并不是这152个自动配置类都能生效  每一个自动配置类都有条件注解ConditionalOnxxx只有条件成立才能生效   
那么多的自动配置类必须在一定的条件下才能生效也就是说我们加载了这么多的配置类但不是所有的都生效了。 
xxxxAutoConfiguration自动配置类 
1、给容器中使用Bean 放一堆组件。2、每个自动配置类都可能有这个注解EnableConfigurationProperties(ServerProperties.class)用来把配置文件中配的指定前缀的属性值封装到 xxxProperties属性类中3、以Tomcat为例把服务器的所有配置都是以server开头的。配置都封装到了属性类中。4、给容器中放的所有组件的一些核心参数都来自于xxxProperties。**xxxProperties**都是和配置文件绑定。只需要改配置文件的值核心组件的底层参数都能修改 核心流程 
导入starter就会导入autoconfigure包。autoconfigure 包里面 有一个文件 META-INF/spring/**org.springframework.boot.autoconfigure.AutoConfiguration**.imports,里面指定的所有启动要加载的自动配置类EnableAutoConfiguration 会自动的把上面文件里面写的所有自动配置类都导入进来。xxxAutoConfiguration 是有条件注解进行按需加载xxxAutoConfiguration给容器中导入一堆组件组件都是从 xxxProperties中提取属性值xxxProperties又是和配置文件进行了绑定 
**效果**导入starter、修改配置文件就能修改底层行为。 
2.组件扫描SpringBootApplication注解包含了ComponentScan注解用于自动扫描并加载应用程序中的组件例如控制器Controllers、服务Services、存储库Repositories等。它默认会扫描SpringBootApplication注解所在类的包及其子包中的组件并将它们纳入Spring Boot应用程序的上下文中使它们可被自动注入和使用。 
//默认扫描的路径是主程序所在子包
SpringBootApplication(scanBasePackages  com.neu)//通过scanBasePackages修改默认扫描路径3.声明配置类SpringBootApplication注解本身就是一个组合注解它包含了Configuration注解将被标注的类声明为配置类。配置类可以包含Spring框架相关的配置、Bean定义以及其他的自定义配置。通过SpringBootApplication注解开发者可以将配置类与启动类合并在一起使得配置和启动可以同时发生。 
总的来说SpringBootApplication注解的主要作用是简化Spring Boot应用程序的配置和启动过程。它自动配置应用程序、扫描并加载组件并将配置和启动类合二为一简化了开发者的工作量提高了开发效率。 
4.启动源码 
//这是一个springboot应用
SpringBootApplication
public class SpringbootdemoApplication {public static void main(String[] args) {//1.返回ioc容器ConfigurableApplicationContext run  SpringApplication.run(SpringbootdemoApplication.class, args);//2.接收ioc容器名称输出String[] names  run.getBeanDefinitionNames();for (String s :names) {System.out.println(s);}}
}5.如何学好SpringBoot 
整合redis 选场景spring-boot-starter-data-redis  场景AutoConfiguration 就是这个场景的自动配置类  写配置  分析到这个场景的自动配置类开启了哪些属性绑定关系 EnableConfigurationProperties(RedisProperties.class) 修改redis相关的配置   分析组件  分析到 RedisAutoConfiguration 给容器中放了 StringRedisTemplate 给业务代码中自动装配 StringRedisTemplate   定制化  修改配置文件 自定义组件自己给容器中放一个 StringRedisTemplate   
二.SpringBoot3配置文件 
2.1属性配置文件使用 配置文件 在 resource 文件夹下面新建 application.properties 配置文件  
# application.properties 为统一配置文件
# 内部包含: 固定功能的key,自定义的key
# 此处的配置信息,我们都可以在程序中Value等注解读取# 固定的key
# 启动端口号
server.port80 # 自定义
spring.jdbc.datasource.driverClassNamecom.mysql.cj.jdbc.driver
spring.jdbc.datasource.urljdbc:mysql:///springboot_01
spring.jdbc.datasource.usernameroot
spring.jdbc.datasource.passwordroot2. 读取配置文件package com.atguigu.properties;import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;Component
public class DataSourceProperties {Value(${spring.jdbc.datasource.driverClassName})private String driverClassName;Value(${spring.jdbc.datasource.url})private String url;Value(${spring.jdbc.datasource.username})private String username;Value(${spring.jdbc.datasource.password})private String password;2.2 YAML配置文件使用 
YAML格式相对于.properties文件具有更好的层次结构表示、自我描述性、注释支持、多行文本表示、复杂数据类型支持和更好的可读性。这些特点使YAML成为一种有力的配置文件格式尤其适用于复杂的配置需求和人类可读的场景。然而选择使用YAML还是.properties取决于实际需求和团队的偏好简单的配置可以使用.properties而复杂的配置可以选择YAML以获得更多的灵活性和可读性 
1. yaml语法说明1. 数据结构用树形结构呈现通过缩进来表示层级2. 连续的项目集合通过减号 ” - ” 来表示3. 键值结构里面的key/value对用冒号 ” : ” 来分隔。4. YAML配置文件的扩展名是yaml 或 yml5. 例如# YAML配置文件示例
app_name: 我的应用程序
version: 1.0.0
author: 张三database:host: localhostport: 5432username: adminpassword: password123features:- 登录- 注册- 仪表盘settings:analytics: truetheme: dark3. 配置文件spring:jdbc:datasource:driverClassName: com.mysql.jdbc.Driverurl: jdbc:mysql:///springboot_02username: rootpassword: rootserver:port: 80读取配置文件  读取方式和properties一致   
package com.atguigu.properties;import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;Component
public class DataSourceProperties {Value(${spring.jdbc.datasource.driverClassName})private String driverClassName;Value(${spring.jdbc.datasource.url})private String url;Value(${spring.jdbc.datasource.username})private String username;Value(${spring.jdbc.datasource.password})private String password;2.3 YAML配置文件使用 yaml格式介绍 YAMLYAML Ain’t Markup Language是一种基于层次结构的数据序列化格式旨在提供一种易读、人类友好的数据表示方式。 与.properties文件相比YAML格式有以下优势 层次结构YAML文件使用缩进和冒号来表示层次结构使得数据之间的关系更加清晰和直观。这样可以更容易理解和维护复杂的配置特别适用于深层次嵌套的配置情况。自我描述性YAML文件具有自我描述性字段和值之间使用冒号分隔并使用缩进表示层级关系。这使得配置文件更易于阅读和理解并且可以减少冗余的标点符号和引号。注释支持YAML格式支持注释可以在配置文件中添加说明性的注释使配置更具可读性和可维护性。相比之下.properties文件不支持注释无法提供类似的解释和说明。多行文本YAML格式支持多行文本的表示可以更方便地表示长文本或数据块。相比之下.properties文件需要使用转义符或将长文本拆分为多行。类型支持YAML格式天然支持复杂的数据类型如列表、映射等。这使得在配置文件中表示嵌套结构或数据集合更加容易而不需要进行额外的解析或转换。更好的可读性由于YAML格式的特点它更容易被人类读懂和解释。它减少了配置文件中需要的特殊字符和语法让配置更加清晰明了从而减少了错误和歧义。 综上所述YAML格式相对于.properties文件具有更好的层次结构表示、自我描述性、注释支持、多行文本表示、复杂数据类型支持和更好的可读性。这些特点使YAML成为一种有力的配置文件格式尤其适用于复杂的配置需求和人类可读的场景。然而选择使用YAML还是.properties取决于实际需求和团队的偏好简单的配置可以使用.properties而复杂的配置可以选择YAML以获得更多的灵活性和可读性  yaml语法说明 数据结构用树形结构呈现通过缩进来表示层级连续的项目集合通过减号 ” - ” 来表示键值结构里面的key/value对用冒号 ” : ” 来分隔。YAML配置文件的扩展名是yaml 或 yml例如  
# YAML配置文件示例
app_name: 我的应用程序
version: 1.0.0
author: 张三database:host: localhostport: 5432username: adminpassword: password123features:- 登录- 注册- 仪表盘settings:analytics: truetheme: dark3. 配置文件spring:jdbc:datasource:driverClassName: com.mysql.jdbc.Driverurl: jdbc:mysql:///springboot_02username: rootpassword: rootserver:port: 80读取配置文件  读取方式和properties一致   
package com.atguigu.properties;import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;Component
public class DataSourceProperties {Value(${spring.jdbc.datasource.driverClassName})private String driverClassName;Value(${spring.jdbc.datasource.url})private String url;Value(${spring.jdbc.datasource.username})private String username;Value(${spring.jdbc.datasource.password})private String password;// 生成get set 和 toString方法public String getDriverClassName() {return driverClassName;}public void setDriverClassName(String driverClassName) {this.driverClassName  driverClassName;}public String getUrl() {return url;}public void setUrl(String url) {this.url  url;}public String getUsername() {return username;}public void setUsername(String username) {this.username  username;}public String getPassword() {return password;}public void setPassword(String password) {this.password  password;}Overridepublic String toString() {return DataSourceProperties{ driverClassName  driverClassName  \ , url  url  \ , username  username  \ , password  password  \ };}
}2.3 批量配置文件注入 ConfigurationProperties是SpringBoot提供的重要注解, 他可以将一些配置属性批量注入到bean对象。 创建类添加属性和注解 在类上通过ConfigurationProperties注解声明该类要读取属性配置 prefix“spring.jdbc.datasource” 读取属性文件中前缀为spring.jdbc.datasource的值。前缀和属性名称和配置文件中的key必须要保持一致才可以注入成功  
package com.atguigu.properties;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;Component
ConfigurationProperties(prefix  spring.jdbc.datasource)
public class DataSourceConfigurationProperties {private String driverClassName;private String url;private String username;private String password;
}2.4 多环境配置和使用 需求 在Spring Boot中可以使用多环境配置来根据不同的运行环境如开发、测试、生产加载不同的配置。SpringBoot支持多环境配置让应用程序在不同的环境中使用不同的配置参数例如数据库连接信息、日志级别、缓存配置等。 以下是实现Spring Boot多环境配置的常见方法 属性文件分离将应用程序的配置参数分离到不同的属性文件中每个环境对应一个属性文件。例如可以创建application-dev.properties、application-prod.properties和application-test.properties等文件。在这些文件中可以定义各自环境的配置参数如数据库连接信息、端口号等。然后在application.properties中通过spring.profiles.active属性指定当前使用的环境。Spring Boot会根据该属性来加载对应环境的属性文件覆盖默认的配置。YAML配置文件与属性文件类似可以将配置参数分离到不同的YAML文件中每个环境对应一个文件。例如可以创建application-dev.yml、application-prod.yml和application-test.yml等文件。在这些文件中可以使用YAML语法定义各自环境的配置参数。同样通过spring.profiles.active属性指定当前的环境Spring Boot会加载相应的YAML文件。命令行参数(动态)可以通过命令行参数来指定当前的环境。例如可以使用--spring.profiles.activedev来指定使用开发环境的配置。 通过上述方法Spring Boot会根据当前指定的环境来加载相应的配置文件或参数从而实现多环境配置。这样可以简化在不同环境之间的配置切换并且确保应用程序在不同环境中具有正确的配置。  多环境配置基于方式b实践 创建开发、测试、生产三个环境的配置文件 application-dev.yml开发  
spring:jdbc:datasource:driverClassName: com.mysql.cj.jdbc.Driverurl: jdbc:mysql:///devusername: rootpassword: rootapplication-test.yml测试 
spring:jdbc:datasource:driverClassName: com.mysql.cj.jdbc.Driverurl: jdbc:mysql:///testusername: rootpassword: rootapplication-prod.yml生产 
spring:jdbc:datasource:driverClassName: com.mysql.cj.jdbc.Driverurl: jdbc:mysql:///produsername: rootpassword: root3. 环境激活#在application.yaml
spring:profiles:active: dev三.整合SpringMVC 
3.1自动配置 
1、整合web场景 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency2、引入了 autoconfigure功能 
3、EnableAutoConfiguration注解使用Import(AutoConfigurationImportSelector.class)批量导入组件 
4、加载 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 文件中配置的所有组件 
5、所有自动配置类如下 
org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration
org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration
以下是响应式web场景和现在的没关系
org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration
org.springframework.boot.autoconfigure.web.reactive.ReactiveMultipartAutoConfiguration
org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryAutoConfiguration
org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration
org.springframework.boot.autoconfigure.web.reactive.WebSessionIdResolverAutoConfiguration
org.springframework.boot.autoconfigure.web.reactive.error.ErrorWebFluxAutoConfiguration
org.springframework.boot.autoconfigure.web.reactive.function.client.ClientHttpConnectorAutoConfiguration
org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration
以上没关系
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration6、绑定了配置文件的一堆配置项 
1、SpringMVC的所有配置 spring.mvc2、Web场景通用配置 spring.web3、文件上传配置 spring.servlet.multipart4、服务器的配置 server: 比如编码方式 
3.2默认配置 
默认配置 
包含了 ContentNegotiatingViewResolver 和 BeanNameViewResolver 组件方便视图解析默认的静态资源处理机制 静态资源放在 static 文件夹下即可直接访问自动注册了 Converter,GenericConverter,Formatter组件适配常见数据类型转换和格式化需求支持 HttpMessageConverters可以方便返回json等数据类型注册 MessageCodesResolver方便国际化及错误消息处理支持 静态 index.html自动使用ConfigurableWebBindingInitializer实现消息处理、数据绑定、类型转化、数据校验等功能 
重要 
如果想保持 boot mvc 的默认配置*并且自定义更多的 mvc 配置如interceptors,formatters,view controllers\等。可以使用Configuration**注解添加一个* WebMvcConfigurer 类型的配置类并不要标注 EnableWebMvc如果想保持 boot mvc 的默认配置但要自定义核心组件实例比如RequestMappingHandlerMapping,RequestMappingHandlerAdapter, 或ExceptionHandlerExceptionResolver给容器中放一个 WebMvcRegistrations 组件即可如果想全面接管 Spring MVCConfiguration 标注一个配置类并加上 EnableWebMvc注解实现 WebMvcConfigurer 接口 总结 
给容器中写一个配置类Configuration实现 WebMvcConfigurer但是不要标注 **EnableWebMvc**注解实现手自一体的效果。 
3.3 web相关配置 
位置application.yml 
# web相关的配置
# https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#appendix.application-properties.server
server:# 端口号设置port: 80# 项目根路径servlet:context-path: /boot当涉及Spring Boot的Web应用程序配置时以下是五个重要的配置参数 
1. server.port: 指定应用程序的HTTP服务器端口号。默认情况下Spring Boot使用8080作为默认端口。您可以通过在配置文件中设置server.port来更改端口号。
2. server.servlet.context-path: 设置应用程序的上下文路径。这是应用程序在URL中的基本路径。默认情况下上下文路径为空。您可以通过在配置文件中设置server.servlet.context-path属性来指定自定义的上下文路径。
3. spring.mvc.view.prefix和spring.mvc.view.suffix: 这两个属性用于配置视图解析器的前缀和后缀。视图解析器用于解析控制器返回的视图名称并将其映射到实际的视图页面。spring.mvc.view.prefix定义视图的前缀spring.mvc.view.suffix定义视图的后缀。
4. spring.resources.static-locations: 配置静态资源的位置。静态资源可以是CSS、JavaScript、图像等。默认情况下Spring Boot会将静态资源放在classpath:/static目录下。您可以通过在配置文件中设置spring.resources.static-locations属性来自定义静态资源的位置。
5. spring.http.encoding.charset和spring.http.encoding.enabled: 这两个属性用于配置HTTP请求和响应的字符编码。spring.http.encoding.charset定义字符编码的名称例如UTF-8spring.http.encoding.enabled用于启用或禁用字符编码的自动配置。这些是在Spring Boot的配置文件中与Web应用程序相关的一些重要配置参数。根据您的需求您可以在配置文件中设置这些参数来定制和配置您的Web应用程序 
3.4 静态资源处理 
WebMvcAutoConfiguration原理 
1. 生效条件 
AutoConfiguration(after  { DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,ValidationAutoConfiguration.class }) //在这些自动配置之后
ConditionalOnWebApplication(type  Type.SERVLET) //如果是web应用就生效类型SERVLET、REACTIVE 响应式web
ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
ConditionalOnMissingBean(WebMvcConfigurationSupport.class) //容器中没有这个Bean才生效。默认就是没有
AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE  10)//优先级
ImportRuntimeHints(WebResourcesRuntimeHints.class)
public class WebMvcAutoConfiguration { 
}2.效果 放了两个Filter  HiddenHttpMethodFilter页面表单提交Rest请求GET、POST、PUT、DELETEFormContentFilter 表单内容FilterGET数据放URL后面、POST数据放请求体请求可以携带数据PUT、DELETE 的请求体数据会被忽略  给容器中放了WebMvcConfigurer组件给SpringMVC添加各种定制功能  所有的功能最终会和配置文件进行绑定WebMvcProperties spring.mvc配置文件WebProperties spring.web配置文件  Configuration(proxyBeanMethods  false)Import(EnableWebMvcConfiguration.class) //额外导入了其他配置EnableConfigurationProperties({ WebMvcProperties.class, WebProperties.class })Order(0)public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ServletContextAware{}3.WebMvcConfigurer接口 
提供了配置SpringMVC底层的所有组件入口 4.静态资源规则源码 
Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {if (!this.resourceProperties.isAddMappings()) {logger.debug(Default resource handling disabled);return;}//1、addResourceHandler(registry, this.mvcProperties.getWebjarsPathPattern(),classpath:/META-INF/resources/webjars/);addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) - {registration.addResourceLocations(this.resourceProperties.getStaticLocations());if (this.servletContext ! null) {ServletContextResource resource  new ServletContextResource(this.servletContext, SERVLET_LOCATION);registration.addResourceLocations(resource);}});
}规则一访问 /webjars/**路径就去 classpath:/META-INF/resources/webjars/下找资源.  maven 导入依赖  规则二访问 /**路径就去 静态资源默认的四个位置找资源  classpath:/META-INF/resources/classpath:/resources/classpath:/static/classpath:/public/  规则三静态资源默认都有缓存规则的设置  所有缓存的设置直接通过配置文件 spring.webcachePeriod 缓存周期 多久不用找服务器要新的。 默认没有以s为单位cacheControl HTTP缓存控制https://developer.mozilla.org/zh-CN/docs/Web/HTTP/CachinguseLastModified是否使用最后一次修改。配合HTTP Cache规则  
如果浏览器访问了一个静态资源 index.js如果服务这个资源没有发生变化下次访问的时候就可以直接让浏览器用自己缓存中的东西而不用给服务器发请求。 
registration.setCachePeriod(getSeconds(this.resourceProperties.getCache().getPeriod()));
registration.setCacheControl(this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl());
registration.setUseLastModified(this.resourceProperties.getCache().isUseLastModified());在WEB开发中我们需要引入一些静态资源 , 例如 : HTML , CSS , JS , 图片等 , 如果是普通的项目静态资源可以放在项目的webapp目录下。现在使用Spring Boot做开发 , 项目中没有webapp目录 , 我们的项目是一个jar工程那么就没有webapp我们的静态资源该放哪里呢 默认路径 在springboot中就定义了静态资源的默认查找路径  
package org.springframework.boot.autoconfigure.web;
//..................
public static class Resources {private static final String[] CLASSPATH_RESOURCE_LOCATIONS  new String[]{classpath:/META-INF/resources/, classpath:/resources/, classpath:/static/, classpath:/public/};private String[] staticLocations;private boolean addMappings;private boolean customized;private final Chain chain;private final Cache cache;public Resources() {this.staticLocations  CLASSPATH_RESOURCE_LOCATIONS;this.addMappings  true;this.customized  false;this.chain  new Chain();this.cache  new Cache();}
//...........        默认的静态资源路径为 
· classpath:/META-INF/resources/ 
· classpath:/resources/ 
· classpath:/static/ 
· classpath:/public/ 
我们只要静态资源放在这些目录中任何一个SpringMVC都会帮我们处理。 我们习惯会把静态资源放在classpath:/static/ 目录下。在resources目录下创建index.html文件 
打开浏览器输入 : http://localhost:8080/index.html 
2. 覆盖路径# web相关的配置
# https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#appendix.application-properties.server
server:# 端口号设置port: 80# 项目根路径servlet:context-path: /boot
spring:web:resources:# 配置静态资源地址,如果设置,会覆盖默认值static-locations: classpath:/webapp访问地址http://localhost/boot/login.html 
3.4 自定义拦截器(SpringMVC配置) 
1. 拦截器声明package com.atguigu.interceptor;import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;Component
public class MyInterceptor implements HandlerInterceptor {Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println(MyInterceptor拦截器的preHandle方法执行....);return true;}Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println(MyInterceptor拦截器的postHandle方法执行....);}Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println(MyInterceptor拦截器的afterCompletion方法执行....);}
}拦截器配置 正常使用配置类只要保证配置类要在启动类的同包或者子包方可生效  
package com.atguigu.config;import com.atguigu.interceptor.MyInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;Configuration
public class MvcConfig implements WebMvcConfigurer {Autowiredprivate MyInterceptor myInterceptor ;/*** /**  拦截当前目录及子目录下的所有路径 /user/**   /user/findAll  /user/order/findAll* /*   拦截当前目录下的以及子路径   /user/*     /user/findAll* param registry*/Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(myInterceptor).addPathPatterns(/**);}
}3.5内容协商 
一套系统适配多端数据返回 默认规则 SpringBoot 多端内容适配。  基于请求头内容协商默认开启  客户端向服务端发送请求携带HTTP标准的Accept请求头。  Accept: application/json、text/xml、text/yaml 2. 服务端根据客户端请求头期望的数据类型进行动态返回  基于请求参数内容协商需要开启  发送请求 GET /projects/spring-boot?formatjson 2. 匹配到 GetMapping(“/projects/spring-boot”) 3. 根据参数协商优先返回 json 类型数据【需要开启参数匹配设置】 4. 发送请求 GET /projects/spring-boot?formatxml,优先返回 xml 类型数据  
3.6错误处理 
错误处理的自动配置都在ErrorMvcAutoConfiguration中两大核心机制 SpringBoot 会自适应****处理错误响应页面或**JSON数据  SpringMVC的错误处理机制依然保留MVC处理不了才会交给boot进行处理  发生错误以后转发给/error路径SpringBoot在底层写好一个 BasicErrorController的组件专门处理这个请求 RequestMapping(produces  MediaType.TEXT_HTML_VALUE) //返回HTMLpublic ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {HttpStatus status  getStatus(request);MapString, Object model  Collections.unmodifiableMap(getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.TEXT_HTML)));response.setStatus(status.value());ModelAndView modelAndView  resolveErrorView(request, response, status, model);return (modelAndView ! null) ? modelAndView : new ModelAndView(error, model);}RequestMapping  //返回 ResponseEntity, JSONpublic ResponseEntityMapString, Object error(HttpServletRequest request) {HttpStatus status  getStatus(request);if (status  HttpStatus.NO_CONTENT) {return new ResponseEntity(status);}MapString, Object body  getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL));return new ResponseEntity(body, status);}错误页面是这么解析到的 
//1、解析错误的自定义视图地址
ModelAndView modelAndView  resolveErrorView(request, response, status, model);
//2、如果解析不到错误页面的地址默认的错误页就是 error
return (modelAndView ! null) ? modelAndView : new ModelAndView(error, model);容器中专门有一个错误视图解析器 
Bean
ConditionalOnBean(DispatcherServlet.class)
ConditionalOnMissingBean(ErrorViewResolver.class)
DefaultErrorViewResolver conventionErrorViewResolver() {return new DefaultErrorViewResolver(this.applicationContext, this.resources);
}SpringBoot解析自定义错误页的默认规则 Overridepublic ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, MapString, Object model) {ModelAndView modelAndView  resolve(String.valueOf(status.value()), model);if (modelAndView  null  SERIES_VIEWS.containsKey(status.series())) {modelAndView  resolve(SERIES_VIEWS.get(status.series()), model);}return modelAndView;}private ModelAndView resolve(String viewName, MapString, Object model) {String errorViewName  error/  viewName;TemplateAvailabilityProvider provider  this.templateAvailabilityProviders.getProvider(errorViewName,this.applicationContext);if (provider ! null) {return new ModelAndView(errorViewName, model);}return resolveResource(errorViewName, model);}private ModelAndView resolveResource(String viewName, MapString, Object model) {for (String location : this.resources.getStaticLocations()) {try {Resource resource  this.applicationContext.getResource(location);resource  resource.createRelative(viewName  .html);if (resource.exists()) {return new ModelAndView(new HtmlResourceView(resource), model);}}catch (Exception ex) {}}return null;}容器中有一个默认的名为 error 的 view 提供了默认白页功能 
Bean(name  error)
ConditionalOnMissingBean(name  error)
public View defaultErrorView() {return this.defaultErrorView;
}封装了JSON格式的错误信息 BeanConditionalOnMissingBean(value  ErrorAttributes.class, search  SearchStrategy.CURRENT)public DefaultErrorAttributes errorAttributes() {return new DefaultErrorAttributes();}规则 解析一个错误页  如果发生了500、404、503、403 这些错误   如果有模板引擎默认在 classpath:/templates/error/精确码.html如果没有模板引擎在静态资源文件夹下找 精确码.html   如果匹配不到精确码.html这些精确的错误页就去找5xx.html4xx.html模糊匹配   如果有模板引擎默认在 classpath:/templates/error/5xx.html如果没有模板引擎在静态资源文件夹下找 5xx.html   如果模板引擎路径templates下有 error.html页面就直接渲染  
最佳实战 前后分离  后台发生的所有错误ControllerAdvice  ExceptionHandler进行统一异常处理。  服务端页面渲染  不可预知的一些HTTP码表示的服务器或客户端错误   给classpath:/templates/error/下面放常用精确的错误码页面。500.html404.html给classpath:/templates/error/下面放通用模糊匹配的错误码页面。 5xx.html4xx.html   发生业务错误   核心业务每一种错误都应该代码控制跳转到自己定制的错误页。通用业务classpath:/templates/error.html页面显示错误信息。   
页面JSON可用的Model数据如下 3.7嵌入容器 
Servlet容器管理、运行Servlet组件Servlet、Filter、Listener的环境一般指服务器 
1.自动配置原理 
SpringBoot 默认嵌入Tomcat作为Servlet容器。自动配置类是ServletWebServerFactoryAutoConfigurationEmbeddedWebServerFactoryCustomizerAutoConfiguration自动配置类开始分析功能。xxxxAutoConfiguration 
AutoConfiguration(after  {SslAutoConfiguration.class}
)
AutoConfigureOrder(Integer.MIN_VALUE)
ConditionalOnClass({ServletRequest.class})
ConditionalOnWebApplication(type  Type.SERVLET
)
EnableConfigurationProperties({ServerProperties.class})
Import({BeanPostProcessorsRegistrar.class, ServletWebServerFactoryConfiguration.EmbeddedTomcat.class, ServletWebServerFactoryConfiguration.EmbeddedJetty.class, ServletWebServerFactoryConfiguration.EmbeddedUndertow.class})
public class ServletWebServerFactoryAutoConfiguration {ServletWebServerFactoryAutoConfiguration 自动配置了嵌入式容器场景  绑定了ServerProperties配置类所有和服务器有关的配置 server  ServletWebServerFactoryAutoConfiguration 导入了 嵌入式的三大服务器 Tomcat、Jetty、Undertow  导入 Tomcat、Jetty、Undertow 都有条件注解。系统中有这个类才行也就是导了包默认 Tomcat配置生效。给容器中放 TomcatServletWebServerFactory都给容器中 ServletWebServerFactory放了一个 web服务器工厂造web服务器的web服务器工厂 都有一个功能getWebServer获取web服务器TomcatServletWebServerFactory 创建了 tomcat。  ServletWebServerFactory 什么时候会创建 webServer出来。  ServletWebServerApplicationContextioc容器启动的时候会调用创建web服务器  Spring**容器刷新启动**的时候会预留一个时机刷新子容器。onRefresh()  refresh() 容器刷新 十二大步的刷新子容器会调用 onRefresh()  Overrideprotected void onRefresh() {super.onRefresh();try {createWebServer();}catch (Throwable ex) {throw new ApplicationContextException(Unable to start web server, ex);}}Web场景的Spring容器启动在onRefresh的时候会调用创建web服务器的方法。 
Web服务器的创建是通过WebServerFactory搞定的。容器中又会根据导了什么包条件注解启动相关的 服务器配置默认EmbeddedTomcat会给容器中放一个 TomcatServletWebServerFactory导致项目启动自动创建出Tomcat。 
2.最佳实践 
用法 
修改server下的相关配置就可以修改服务器参数通过给容器中放一个**ServletWebServerFactory**来禁用掉SpringBoot默认放的服务器工厂实现自定义嵌入任意服务器。 
四.整合Druid数据源 
引入依赖 
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion3.0.5/version/parentgroupIdcom.atguigu/groupIdartifactIdspringboot-starter-druid-04/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependencies!--  web开发的场景启动器 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!-- 数据库相关配置启动器 jdbctemplate 事务相关--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-jdbc/artifactId/dependency!-- druid启动器的依赖  --dependencygroupIdcom.alibaba/groupIdartifactIddruid-spring-boot-3-starter/artifactIdversion1.2.18/version/dependency!-- 驱动类--dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.28/version/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.28/version/dependency/dependencies!--    SpringBoot应用打包插件--buildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project启动类 
SpringBootApplication
public class MainApplication {public static void main(String[] args) {SpringApplication.run(MainApplication.class,args);}
} 配置文件编写  添加druid连接池的基本配置   
spring:datasource:# 连接池类型 type: com.alibaba.druid.pool.DruidDataSource# Druid的其他属性配置 springboot3整合情况下,数据库连接信息必须在Druid属性下!druid:url: jdbc:mysql://localhost:3306/day01username: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Driver# 初始化时建立物理连接的个数initial-size: 5# 连接池的最小空闲数量min-idle: 5# 连接池最大连接数量max-active: 20# 获取连接时最大等待时间单位毫秒max-wait: 60000# 申请连接的时候检测如果空闲时间大于timeBetweenEvictionRunsMillis执行validationQuery检测连接是否有效。test-while-idle: true# 既作为检测的间隔时间又作为testWhileIdel执行的依据time-between-eviction-runs-millis: 60000# 销毁线程时检测当前连接的最后活动时间和当前时间差大于该值时关闭当前连接(配置连接在池中的最小生存时间)min-evictable-idle-time-millis: 30000# 用来检测数据库连接是否有效的sql 必须是一个查询语句(oracle中为 select 1 from dual)validation-query: select 1# 申请连接时会执行validationQuery检测连接是否有效,开启会降低性能,默认为truetest-on-borrow: false# 归还连接时会执行validationQuery检测连接是否有效,开启会降低性能,默认为truetest-on-return: false# 是否缓存preparedStatement, 也就是PSCache,PSCache对支持游标的数据库性能提升巨大比如说oracle,在mysql下建议关闭。pool-prepared-statements: false# 要启用PSCache必须配置大于0当大于0时poolPreparedStatements自动触发修改为true。在Druid中不会存在Oracle下PSCache占用内存过多的问题可以把这个数值配置大一些比如说100max-pool-prepared-statement-per-connection-size: -1# 合并多个DruidDataSource的监控数据use-global-data-source-stat: truelogging:level:root: debug五.整合Mybatis 
5.1自动配置 
SSM整合总结 导入 mybatis-spring-boot-starter  配置数据源信息  配置mybatis的**mapper接口扫描**与**xml映射文件扫描**  编写beanmapper生成xml编写sql 进行crud。事务等操作依然和Spring中用法一样  效果  所有sql写在xml中所有mybatis配置写在application.properties下面  jdbc场景的自动配置  mybatis-spring-boot-starter导入 spring-boot-starter-jdbcjdbc是操作数据库的场景Jdbc场景的几个自动配置   org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration     数据源的自动配置所有和数据源有关的配置都绑定在DataSourceProperties默认使用 HikariDataSource     org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration     给容器中放了JdbcTemplate操作数据库     org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfigurationorg.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration     基于XA二阶提交协议的分布式事务数据源     org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration   支持事务  具有的底层能力数据源、JdbcTemplate、事务  MyBatisAutoConfiguration配置了MyBatis的整合流程  mybatis-spring-boot-starter导入 mybatis-spring-boot-autoconfiguremybatis的自动配置包默认加载两个自动配置类  org.mybatis.spring.boot.autoconfigure.MybatisLanguageDriverAutoConfiguration org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration  必须在数据源配置好之后才配置 给容器中SqlSessionFactory组件。创建和数据库的一次会话给容器中SqlSessionTemplate组件。操作数据库   MyBatis的所有配置绑定在MybatisProperties 每个Mapper接口的代理对象是怎么创建放到容器中。详见**MapperScan**原理  利用Import(MapperScannerRegistrar.class)批量给容器中注册组件。解析指定的包路径里面的每一个类为每一个Mapper接口类创建Bean定义信息注册到容器中。  
如何分析哪个场景导入以后开启了哪些自动配置类。 
找classpath:/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件中配置的所有值就是要开启的自动配置类但是每个类可能有条件注解基于条件注解判断哪个自动配置类生效了。 
导入依赖 
parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion3.0.5/version
/parentdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion3.0.1/version/dependency!-- 数据库相关配置启动器 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-jdbc/artifactId/dependency!-- druid启动器的依赖  --dependencygroupIdcom.alibaba/groupIdartifactIddruid-spring-boot-3-starter/artifactIdversion1.2.18/version/dependency!-- 驱动类--dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.28/version/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.28/version/dependency/dependencies配置文件 
server:port: 80servlet:context-path: /
spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedruid:url: jdbc:mysql:///day01username: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Drivermybatis:configuration:  # setting配置auto-mapping-behavior: fullmap-underscore-to-camel-case: truelog-impl: org.apache.ibatis.logging.slf4j.Slf4jImpltype-aliases-package: com.neu.pojo # 配置别名mapper-locations: classpath:/mapper/*.xml # mapperxml位置5.3 声明式事务整合配置 
依赖导入: dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-jdbc/artifactId
/dependency注SpringBoot项目会自动配置一个 DataSourceTransactionManager所以我们只需在方法或者类加上 Transactional 注解就自动纳入 Spring 的事务管理了 
Transactional
public void update(){User user  new User();user.setId(1);user.setPassword(test2);user.setAccount(test2);userMapper.update(user);
}5.4 AOP整合配置 
依赖导入: 
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-aop/artifactId
/dependency直接使用aop注解即可: 
Component
Aspect
public class LogAdvice {Before(execution(* com..service.*.*(..)))public void before(JoinPoint joinPoint){System.out.println(LogAdvice.before);System.out.println(joinPoint    joinPoint);}}六.基础特性 
6.1SpringApplication 
1.自定义banner 
在banner转换网站生成banner文件 
在application.propreties配置 
spring.banner.locationbanner.txt2.自定义SpringApplication 
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplication
public class MyApplication {public static void main(String[] args) {//1.自定义SpringApplication底层配置SpringApplication application  new SpringApplication(MyApplication.class);//调整参数配置文件的优先级高于程序application.setBannerMode(Banner.Mode.OFF);//配置application相当于在application.propreties中修改//2.运行application.run(args);}}3.FluentBuilder API启动 
new SpringApplicationBuilder().sources(Parent.class).child(Application.class).bannerMode(Banner.Mode.OFF).run(args);6.2profile 
环境隔离能力快速切换开发、测试、生产环境 
步骤 
标识环境指定哪些组件、配置在哪个环境生效切换环境这个环境对应的所有组件和配置就应该生效 
1. 使用 
指定环境 
Spring Profiles 提供一种隔离配置的方式使其仅在特定环境生效任何Component, Configuration 或 ConfigurationProperties 可以使用 Profile 标记来指定何时被加载。【容器中的组件都可以被 Profile标记】 
环境激活 
配置激活指定环境 配置文件 
spring.profiles.activeproduction,hsqldb也可以使用命令行激活。–spring.profiles.activedev,hsqldb  还可以配置默认环境 不标注Profile 的组件永远都存在。  以前默认环境叫defaultspring.profiles.defaulttest  推荐使用激活方式激活指定环境  
环境包含 
注意 
spring.profiles.active 和spring.profiles.default 只能用到 无 profile 的文件中如果在application-dev.yaml中编写就是无效的也可以额外添加生效文件而不是激活替换。比如 
spring.profiles.include[0]common
spring.profiles.include[1]local最佳实战 生效的环境  激活的环境/默认环境  包含的环境  项目里面这么用  基础的配置mybatis、log、xxx写到包含环境中需要动态切换变化的 db、redis写到激活的环境中  
2. Profile 分组 
创建prod组指定包含db和mq配置 
spring.profiles.group.prod[0]db
spring.profiles.group.prod[1]mq使用–spring.profiles.activeprod 就会激活proddbmq配置文件 
3. Profile 配置文件 application-{profile}.properties可以作为指定环境的配置文件。  激活这个环境配置就会生效。最终生效的所有配置是  application.properties主配置文件任意时候都生效application-{profile}.properties指定环境配置文件激活指定环境生效  
profile优先级  application 
6.3外部化配置 
场景线上应用如何快速修改配置并应用最新配置 
SpringBoot 使用 配置优先级  外部配置 简化配置更新、简化运维。只需要给jar应用所在的文件夹放一个application.properties最新配置文件重启项目就能自动应用最新配置 
1.优先级配置 
结论配置可以写到很多位置常见的优先级顺序 
命令行 配置文件 springapplication配置 
结论包外  包内 同级情况profile配置  application配置 
所有参数均可由命令行传入使用--参数项参数值将会被添加到环境变量中并优先于配置文件。 
比如java -jar app.jar --nameSpring,可以使用Value(${name})获取 启动端口命令行  9001  8001  9000  8000 
2.外部配置 
SpringBoot 应用启动时会自动寻找application.properties和application.yaml位置进行加载。顺序如下后面覆盖前面 类路径: 内部  类根路径  类下/config包  当前路径项目所在的位置  当前路径当前下/config子目录/config目录的直接子目录  规律最外层的最优先。 
命令行  所有包外  包内config目录  根目录profile  application 
配置不同就都生效互补配置相同高优先级覆盖低优先级 
3.导入配置 
使用spring.config.import可以导入额外配置 
spring.config.importmy.properties
my.propertyvalue无论以上写法的先后顺序my.properties的值总是优先于直接在文件中编写的my.property。 
4. 属性占位符 
配置文件中可以使用 ${name:default}形式取出之前配置过的值。 
app.nameMyApp
app.description${app.name} is a Spring Boot application written by ${username:Unknown}6.4单元测试 
SpringBoot 提供一系列测试工具集及注解方便我们进行测试。 spring-boot-test提供核心测试能力spring-boot-test-autoconfigure 提供测试的一些自动配置。 我们只需要导入spring-boot-starter-test 即可整合测试 
组件测试 
直接Autowired容器中的组件进行测试 
注解 
**Test *表示方法是测试方法。但是与JUnit4的Test不同他的职责非常单一不能声明任何属性拓展的测试将会由Jupiter提供额外测试**ParameterizedTest *表示方法是参数化测试下方会有详细介绍**RepeatedTest *表示方法可重复执行下方会有详细介绍**DisplayName *为测试类或者测试方法设置展示名称**BeforeEach *表示在每个单元测试之前执行**AfterEach *表示在每个单元测试之后执行**BeforeAll *表示在所有单元测试之前执行**AfterAll *表示在所有单元测试之后执行**Tag *表示单元测试类别类似于JUnit4中的Categories**Disabled *表示测试类或测试方法不执行类似于JUnit4中的Ignore**Timeout *表示测试方法运行如果超过了指定时间将会返回错误**ExtendWith *为测试类或测试方法提供扩展类引用 
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeTrue;import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;class StandardTests {BeforeAllstatic void initAll() {}BeforeEachvoid init() {}DisplayName()Testvoid succeedingTest() {}Testvoid failingTest() {fail(a failing test);}TestDisabled(for demonstration purposes)void skippedTest() {// not executed}Testvoid abortedTest() {assumeTrue(abc.contains(Z));fail(test should have been aborted);}AfterEachvoid tearDown() {}AfterAllstatic void tearDownAll() {}}