SpringBoot特点

SpringBoot 依赖管理 和 自动配置 的特性

一、依赖管理

  • 父项目做依赖管理

    1
    2
    3
    4
    5
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.5</version>
    </parent>

    spring-boot-starter-parent 的父项目:

    1
    2
    3
    4
    5
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.4.5</version>
    </parent>

    spring-boot-dependencies 中几乎声明了所有开发中常用的依赖的版本号,自动版本仲裁机制

  • 开发导入 starter 启动器

    Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop shop for all the Spring and related technologies that you need without having to hunt through sample code and copy-paste loads of dependency descriptors.

    • spring-boot-starter-** 就是某种场景

    • 只要引入 starter ,这个场景的所有常规需要的依赖都会自动引入

    • SpringBoot所有支持的场景

    • *-spring-boot-starter : 第三方提供的简化开发的场景启动器

    • 所有场景启动器最底层的依赖

      1
      2
      3
      4
      5
      6
      <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>2.4.5</version>
      <scope>compile</scope>
      </dependency>
  • 无需关注版本号,自动版本仲裁

    • 引入依赖默认都可以不写版本
    • 引入非版本仲裁的jar,要写版本号
  • 也修改默认版本号

    1. 查看 spring-boot-dependencies 里面规定当前依赖的版本用的 key。

    2. 在当前项目里面重写配置

      1
      2
      3
      <properties>
      <mysql.version>5.1.7</mysql.version>
      </properties>

二、自动配置

2.1 自动配好Tomcat

  • 自动引入了 Tomcat 的依赖并且自动配置

    1
    2
    3
    4
    5
    6
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <version>2.4.5</version>
    <scope>compile</scope>
    </dependency>

2.2 自动配好SpringMVC

  • 引入了SpringMVC全套组件
  • 自动配好了SpringMVC常用组件

2.3 自动配置好默认的编码功能,如字符编码问题

  • Spring Boot配置好了所有web开发相关的场景

2.4 默认的包结构

  • Spring Boot不需要任何特定的代码布局即可工作
  • Spring Boot建议我们将主应用程序类放在其他类之上的根包中。该 @SpringBootApplication 注解往往放在主类。
  • 主程序所在包及其下面的所有子包里面的组件都会被默认扫描进来。
  • 如果想要改变扫描路径,使用 @SpringBootApplication(scanBasePackages="packagename")
    • 或者使用 @EnableAutoConfiguration@ComponentScan 指定扫描路径

2.5 各种配置拥有默认值

  • 配置文件的值最终会绑定每个类上,这个类会在容器中创建对象

2.6 按需加载所有自动配置项

  • 非常多的 starter
  • 引入了哪些场景这个场景的自动配置才会开启
  • Spring Boot 所有的自动配置功能都在 spring-boot-autoconfigure 包里面
  • Copyrights © 2020-2023 夕子学姐
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信