SpringBoot

一 特点

  1. 其最主要作用就是帮助开发人员快速的构建庞大的spring项目,并且尽可能的减少一切xml配置,做到****开箱即用****,迅速上手,让开发人员关注业务而非配置。
  2. 自动配置 : 不需要再关注各个框架的整合配置, springboot全部已经配置好了
    起步依赖 : 我们在需要使用某个框架的时候, 直接添加这个框架的启动器依赖即可 , 不需要在关注jar包的冲突和整合
    习惯优于配置 约定大于配置

二 快速搭建项目

  1. 依赖
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.6.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
  1. 启动类
@SpringBootApplication
@SpringApplication.run(Application.class,orgs);
  1. 配置文件
  1. @Value(“${xxx}”)
  2. properties
  3. yml YAML是一种配置文件格式
spring:
  jdbc:
    datasource:
      driverClassName: com.mysql.jdbc.Driver
      url: jdbc:mysql:///springboot_01
      username: root
      password: root
  1. profile
spring:
  profiles:
    active: dev 激活不同配置文件

三 SpringBoot自动配置原理

  1. @SpringBootApplication注解
    · @SpringBootConfiguration : 代表这个类就是一个配置类 , 本质上就是一个@Configuration注解
    · @ComponentScan : 组件扫描, 默认扫描启动类所在包及子包下的类身上的注解
    · @EnableAutoConfiguration : 自动配置注解 , 添加了此注解会自动去读取spring.factories配置文件中的自动配置类
  2. @ConfigurationProperties 将一些配置属性批量注入到bean对象
  3. @ConfigurationProperties(prefix = “xxx”) A类 prefix读取属性文件中前缀为xxx的值 前缀的属性名称和配置文件中的key必须保持一致才可以注入成功
    @EnableConfigurationProperties(A类.class)
  4. 条件注解配置
  1. @ConditionOnBean 存在某个Bean 配置生效
  2. @ConditionOnProperty 存在某个Property配置生效
  3. @ConditionOnClass 存在某个类 配置生效
  1. 自动配置原理 重点
spring.factories 这个里面有127个配置文件 
具体流程
    SpringApplication 类构建过程中
    setInitialzers() //初始化和加载配置文件的信息
  getspringFactoriesgetInstance(ApplicationContextInitializer.class);
    loadFactoryNames 加载FactoryName;
    利用createSpringFactoriesInstances将这些加载到的类名进行实例化
    createSpringFactoriesInstances 将加载的类名进行实例化
    继续跟进loadFactoryNames:return loadSpringFactories(classLoader).getOrderFactories() ->classLoader.getResources(FACTORIES_RESOURCE_LOCATION);
    此处会利用类加载器加载一个文件:META-INF/spring.factories
    ClassLoader默认是从classpath下读取文件
    SpringBoot进行初始化会加载所有的META-INF/spring.factroies文件,包括jar包

Quzrtz 定时任务

  1. 读取自动配置类:
  1. 打开Spring.factories
  2. EnableAutoConfiguration接口为key 的一系列配置 ,key对应的值 就是所有的自动配置类 在jar包中可以找到。
  1. 默认属性配置
  1. 我们配置视图解析器的时候需要配置前缀和后缀 , 那么这些配置在哪配置的呢:通过源码发现, 这个配置是从this.mvcProperties.getView()中读取的 ,this.mvcProperties
  2. 这个mvcProperties就是个变量,变量中又有一个view类型的变量
  3. 这个变量中配置的就是前缀和后缀
  1. 覆盖默认属性配置
  1. 配置类使用ConfigurationProperties进行读取文件进行配置
  2. 所以在配置文件中修改配置文件的前缀就可以进行修改信息:spring.mvc.xxxx;
  1. @SpringBootTest:自动侦测并加载@SpringBootApplication 或者@SpringBootConfiguration中的配置 默认web环境为MOCK 不监听任务端口

四 搭建SpringBoot项目

  1. 导入依赖
<dependency>
    <groupId>com.atguigu</groupId>
    <artifactId>spring-boot-jdbc-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

application.yml

spring:
  jdbc:
    datasource:
      driverClassName: com.mysql.jdbc.Driver
      url: jdbc:mysql:///springboot_01
      username: root
      password: root
  profiles:
    active: datasource
  1. 自动配置类
@SpringBootConfiguration
@EnableConfigurationProperties(DataSource.class) 根据配置属性类装配对象的属性
  1. 属性配置类
@ConfigurationProperties(prefix = "spring.jdbc.datasource") 读取配置文件
  1. 编写自动配置属性文件
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=xx.xxx.xx.DataSourceAutoConfiguration
  1. 配置连接池信息

五 SpringBoot常用启动器

  • 静态资源目录
  1. classpath:/META/INF/resources/
  2. classpath:/resources/
  3. classpath:/static/
  4. classpath:/public/
  • 自定义拦截器
  1. 编写一个拦截器 实现HandlerInterceptor
  2. 通过实现WebMvcConfigures注册拦截器
@Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor).addPathPatterns("/**");
    }
  • SpringBoot整合JPA
  1. application.yml
logging:
  level:
    com.atguigu.dao: debug # 配置日志
spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    database: mysql
    show-sql: true
    generate-ddl: true
    hibernate:
      ddl-auto: update
      naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy
server:
  port: 18081
  1. 配置实体类
@Entity @Entity说明这个class是实体类,并且使用默认的orm规则,即class名就是数据库表中表明,class字段名即表中字段名。@Entity注解指明这是一个实体Bean。
@Table注解默认情况下只会完成表和实体之间的映射,声明才对象映射到数据库的数据表,通过它可以为实体指定表(table)
@GeneratedValue注解存在的意义主要就是为一个实体生成一个唯一标识的主键、@GeneratedValue提供了主键的生成策略。
@Id 标识为主键
@Column 标识实体类中的属性与数据表的字段的对应关系

六 SpringBoot综合案例

  • 启动类
@SpringBootApplication
@MapperSacn(basePackes="") 扫描dao层 Mapper接口以及映射配置
@EnableTransactionManagement 配置自动注解
  • application.yml
  • 实体类user
  • Mapper接口以及映射配置
  • service serviceImpl

七 SpringBoot其他组件

  1. SpringBoot Actuator
  • 介绍:Spring Boot Actuator是SpringBoot自带的一个组件 , 可以帮助我们监控和管理Spring Boot应用,比如健康检查、审计、统计和HTTP追踪等
  • 配置信息
management:
  endpoints:
    web:
      exposure:
        include: '*'  # 对外暴露的访问入口 , 默认是/health和/info
      base-path: /monitor # 默认是actuator
  endpoint:
    health:
      show-details: ALWAYS	# 显示所有健康状态
  server:
    port: 9999
  1. SpringBoot Admin服务端
  • application.yml
spring:
  application:
    name: admin-server
server:	
  port: 8769
  • 启动类:@EnableAdminServer
  1. SpringBoot Admin客户端
  • application.yml
server:
  port: 9999
spring:
  application:
    name: admin-client
  boot:
    admin:
      client:
        url: http://localhost:8769   # 指定注册地址 , Spring Boot Admin Server地址
management:
  endpoints:
    web:
      exposure:
        include: '*' 
  endpoint:
    health:
      show-details: ALWAYS
  1. 部署打包
  • maven ->package
  • java -jar springboot_01-SNAPASHOT.jar 运行命令