SpringBoot官方的场景 https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-starter 只要引入starter,这个场景的所有常规需要的依赖我们都自动引入

  • 我们开发会导入starter场景启动器,见到很多spring-boot-starter-* 这是 springboot官方场景启动器。
  • 另外,若见到 *-spring-boot-starter 这是第三方为我们提供的简化开发的场景启动器。

    以Springboot自带的Web开发场景为例

就是它<artifactId>spring-boot-starter-web</artifactId>,会自动帮我们配好5个功能

  • spring-boot-starter
  • spring-boot-starter-json
  • spring-boot-starter-tomcat
  • spring-web
  • spring-webmvc ```java <?xml version=”1.0” encoding=”UTF-8”?> <project xmlns=”http://maven.apache.org/POM/4.0.0

    1. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0

    com.atguigu boot-01-helloworld 1.0-SNAPSHOT

    jar
  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.3.4.RELEASE</version>
  5. </parent>
  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.projectlombok</groupId>
  8. <artifactId>lombok</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-devtools</artifactId>
  13. <optional>true</optional>
  14. </dependency>
  1. </dependencies>
  2. <build>
  3. <plugins>
  4. <plugin>
  5. <groupId>org.springframework.boot</groupId>
  6. <artifactId>spring-boot-maven-plugin</artifactId>
  7. </plugin>
  8. </plugins>
  9. </build>

  1. 点进`<artifactId>spring-boot-starter-web</artifactId>`发现,该Web场景启动器有5个依赖
  2. ```java
  3. <dependencies>
  4. <dependency>
  5. <groupId>org.springframework.boot</groupId>
  6. <artifactId>spring-boot-starter</artifactId>
  7. <version>2.4.0</version>
  8. <scope>compile</scope>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-json</artifactId>
  13. <version>2.4.0</version>
  14. <scope>compile</scope>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-tomcat</artifactId>
  19. <version>2.4.0</version>
  20. <scope>compile</scope>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework</groupId>
  24. <artifactId>spring-web</artifactId>
  25. <version>5.3.1</version>
  26. <scope>compile</scope>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework</groupId>
  30. <artifactId>spring-webmvc</artifactId>
  31. <version>5.3.1</version>
  32. <scope>compile</scope>
  33. </dependency>
  34. </dependencies>

boot-01-helloworld.png
image.png

Web场景自动注入autoconfigure.jar/web

starter都与springboot的 spring-boot-autoconfigure相对应,即spring-boot-autoconfigure-2.3.4.RELEASE.jar,SpringBoot所有的自动配置功能都在spring-boot-autoconfigure-2.3.4.RELEASE.jar包里面,里面有springboot全场景
image.png
默认配置最终都是映射到某个类上,这个类由@ConfigurationProperties指定,如文件上传相关的:MultipartProperties,yaml/properties配置文件的值最终会绑定某个类上,这个类会在容器中创建对象。比如我们引入了web场景,则位于spring-boot-autoconfigure-2.3.4.RELEASE.jar包下的web配置都会生效了:
@ConfigurationProperties(prefix = "spring.servlet.multipart", ignoreUnknownFields = false)
image.png
autoconfigure.jar当中的类并不是全部生效
需要注意的是,引入了哪些场景这个场景的spring-boot-autoconfigure-2.3.4.RELEASE.jar自动配置才会开启,不引入场景则该场景对应的自动配置就不会生效:比如我们没有引入batch批处理场景

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.atguigu</groupId>
  7. <artifactId>boot-01-helloworld</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <packaging>jar</packaging>
  10. <parent>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-parent</artifactId>
  13. <version>2.3.4.RELEASE</version>
  14. </parent>
  15. <!-- <properties>-->
  16. <!-- <mysql.version>5.1.43</mysql.version>-->
  17. <!-- </properties>-->
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-web</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.projectlombok</groupId>
  25. <artifactId>lombok</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-devtools</artifactId>
  30. <optional>true</optional>
  31. </dependency>
  32. <!-- <dependency>-->
  33. <!-- <groupId>org.springframework.boot</groupId>-->
  34. <!-- <artifactId>spring-boot-starter-batch</artifactId>-->
  35. <!-- </dependency>-->
  36. <!-- <dependency>-->
  37. <!-- <groupId>mysql</groupId>-->
  38. <!-- <artifactId>mysql-connector-java</artifactId>-->
  39. <!-- </dependency>-->
  40. </dependencies>
  41. <build>
  42. <plugins>
  43. <plugin>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-maven-plugin</artifactId>
  46. </plugin>
  47. </plugins>
  48. </build>
  49. </project>

image.png