【Java劝退师】SpringBoot 知识脑图 - 全栈开源框架

1,389 阅读3分钟

SpringBoot

SpringBoot

全栈开源框架

一、解决的问题

  1. Spring 框架配置繁重 - 自动配置
  2. Jar 包版本管理困难 - 起步依赖
  3. 频繁重启项目 - 热布署

二、概念

  1. 约定优于配置 - 系统应假定合理的默认值,而非要求提供不必要的配置
  2. 自动配置 - 将配置类的 Bean 自动存储到 IOC 容器中,让我们可以通过 @Autoweird、@Resource 注解来使用
  3. 起步依赖 - 将具备某种功能的 Jar 包座标打包在一起

三、注解

  1. @SpringBootApplication - 标记该类为主进程启动类 - Spring Boot 只会去扫描当前主进程启动类所在的包及其子包
  2. @SpingBootTest - 标记该类为单元测试类,并加载项目的 ApplicationContext 环境
  3. @RunWith - 使用测试启动器,并加载 SpringBoot 的测试注解
    • SpringRunner.class - 继承 SpringJUnit4ClassRunner,表示当前的测试环境附在 Spring 的测试环境下
  4. @PropertySource("classpath:配置文档名称") - 引入自定义配置文档
  5. @ConfigurationProperties(prefix = "属性") - 将配置文档中,特定属性名称开头的属性注入到该类中 - 透过调用 set 方法实现
  6. @Value("${属性.属性}")
    • 注入配置文档属性 - 底层透过反射实现,无需生成 set 方法
    • 仅支持普通类型注入
  7. @EnableConfigurationProperties(配置类.class) - 开启对应配置类的属性注入功能 - 有使用 @Component 可省略

四、 YML 格式

Value 类型

  1. 普通类型

    1. 数字
    2. 字符串
    3. 布尔值
  2. 数组 或 单列集合

    person:
      hobby:
        - play
        - read
        - sleep
    
    person:
      hobby:
        play,
        read,
        sleep
    
    person:
      hobby: [play,read,sleep] # "[]"可省略
    
  3. Map 集合 或 对象

    person:
      map:
        k1: v1
        k2: v2
    
    person:
      map: {k1: v1, k2: v2}
    
  4. 随机数

    my.secret=${random.value}                     // 随机值(数字&字母)
    my.number=${random.int}                       // 随机整数
    my.bignumber=${random.long}                   // 随机long类型数
    my.uuid=${random.uuid}                        // 随机UUID
    my.number.less.than.ten=${random.int(10)}     // 小于10的随机整数
    my.number.in.range=${random.int[1024,65536]}  // 范围在[1024,65536]的随机数
    
  5. 引用

    app.name=MyApp
    app.description=${app.name} is a Spring Boot application
    
    # 随机值与引用结合
    tom.age=${random.int[10,20]}
    tom.description=tom的年龄可能是${tom.age}
    

五、配置文档优先级

  1. properties
  2. yaml
  3. yml

六、依赖管理

  1. spring-boot-starter-parent - SpringBoot 父工程 - 默认编码设置、JDK默认版本设置、引入配置文档、插件管理
  2. spring-boot-dependencies - SpringBoot 爷爷工程 - 依赖版本设置

七、集成

1. MyBatis

注解

  • @Mapper - 标记该接口是 MyBatis 的接口文档,让 SpringBoot 实例化代理对象,并存储到 IOC 容器中

  • @MapperScan("路径") - 扫描该路径下所有 MyBatis 接口文档,解决逐个标记 @Mapper 注解的困扰

配置文档

  • mybatis.mapper-locations=classpath:mapper/*.xml - MyBatis XML 配置文档路径

  • mybatis.type-aliases-package=com.lagou.pojo - 映射文档中,实体类别名路径

  • mybatis.configuration.map-underscore-to-camel-case=true - 驼峰命名匹配

2. Redis

注解

  • @RedisHash("空间名") - 指定实体类在 Redis 中的存储空间名

  • @Id - 标记实体类主键

  • @Indexed - 标记该属性要生成二级索引,用于条件查找 - 原理 : 在 Key 上添加字段

配置文档

  • spring.cache.redis.time-to-live=60000 - 默认有效期

接口类

  • CrudRepository<实体类, 主键类型> - 产生持久层代理对象

image-20201016114057050

3. 缓存

  • @EnableCaching - 开启基于注解的缓存管理

  • @Cacheable(cacheNames = "空间名") - 标明方法返回结果存储的命名空间

    • Key
      • 没有参数 - new SimpleKey()
      • 一个参数 - 参数值
      • 多个参数 - new SimpleKey(params)
    • Value - 缓存结果
属性名说明
value/cacheNames缓存空间名称,必填。二择一即可
key缓存 Key,默认为方法参数,可以使用 SpEL 表达式
keyGeneratorKey 生成器,与 key 属性二择一使用
cacheManager缓存管理器
cacheResolver缓存解析器,与 cacheManager 属性二择一使用
condition符合条件,进行缓存
unless不符合条件,进行缓存
sync是否使用异步,默认 false

SpEL 表达式

示例描述
root.mathodName当前被调用的方法名
root.mathod当前被调用的方法
root.target当前被调用的目标对象
root.targetClass当前被调用的目标对象类
root.args[0]第一个参数
#useruser参数
#result返回值
  • @CachePut - 更新数据后,重新将数据放到缓存中

  • @CacheEvict - 方法调用后,删除缓存数据

@Cacheable(cacheNames = "comment",unless = "#result==null")
public Comment findCommentById(Integer id)
    
@CachePut(cacheNames = "comment",key = "#result.id")
public Comment updateComment(Comment comment)
    
@CacheEvict(cacheNames = "comment")
public void deleteComment(int comment_id)