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“xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
点进`<artifactId>spring-boot-starter-web</artifactId>`发现,该Web场景启动器有5个依赖
```java
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.4.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.4.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.4.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
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全场景
默认配置最终都是映射到某个类上,这个类由@ConfigurationProperties
指定,如文件上传相关的:MultipartProperties,yaml/properties配置文件的值最终会绑定某个类上,这个类会在容器中创建对象。比如我们引入了web场景,则位于spring-boot-autoconfigure-2.3.4.RELEASE.jar包下的web配置都会生效了:@ConfigurationProperties(prefix = "spring.servlet.multipart", ignoreUnknownFields = false)
autoconfigure.jar当中的类并不是全部生效
需要注意的是,引入了哪些场景这个场景的spring-boot-autoconfigure-2.3.4.RELEASE.jar自动配置才会开启,不引入场景则该场景对应的自动配置就不会生效:比如我们没有引入batch批处理场景
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.atguigu</groupId>
<artifactId>boot-01-helloworld</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
<!-- <properties>-->
<!-- <mysql.version>5.1.43</mysql.version>-->
<!-- </properties>-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-batch</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>mysql</groupId>-->
<!-- <artifactId>mysql-connector-java</artifactId>-->
<!-- </dependency>-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>