Springboot項目構(gòu)建時各種依賴詳細介紹與依賴關(guān)系說明詳解
一、spring-boot-dependencies
1.簡介
spring-boot-dependencies 是一個特殊的POM(Project Object Model)文件,它由Spring Boot團隊維護,其主要作用是為Spring Boot相關(guān)的依賴提供統(tǒng)一的版本管理。它定義了大量的依賴及其兼容版本,確保Spring Boot生態(tài)中的庫能夠協(xié)同工作。官方倉庫地址:
https://repo1.maven.org/maven2/org/springframework/boot/spring-boot-dependencies/3.1.0/spring-boot-dependencies-3.1.0.pom(版本可依據(jù)具體項目要求更改)
2. 內(nèi)容概覽
在spring-boot-dependencies中,主要包含以下內(nèi)容:
- 依賴管理(Dependency Management):定義了Spring Boot項目常用的第三方庫的版本號(如Spring Framework、Jackson、Log4j2、Hibernate等)。
- 插件管理(Plugin Management):定義了一些Maven插件的版本(如
maven-compiler-plugin,maven-surefire-plugin等)。 - 屬性(Properties):在
<properties>標簽下定義了大量依賴的版本號,然后通過屬性引用來統(tǒng)一版本。
3.核心內(nèi)容結(jié)構(gòu)

4. 依賴樹結(jié)構(gòu)
spring-boot-dependencies本身并不引入任何依賴,它只是聲明依賴版本。當我們在項目中引入一個starter(比如spring-boot-starter-web)時,Maven會根據(jù)spring-boot-dependencies中定義的版本去下載對應的依賴。
6. spring-boot-dependencies中的屬性
在spring-boot-dependencies的POM文件中,通過屬性定義了各個依賴的版本。例如:
<properties> <activemq.version>5.16.5</activemq.version> <antlr2.version>2.7.7</antlr2.version> <appengine-sdk.version>1.9.88</appengine-sdk.version> <artemis.version>2.19.1</artemis.version> <aspectj.version>1.9.7</aspectj.version> <assertj.version>3.19.0</assertj.version> <atomikos.version>4.0.6</atomikos.version> <awaitility.version>4.1.0</awaitility.version> ... 更多屬性 </properties>
然后在依賴管理中引用這些屬性:
<dependencyManagement>
<dependencies>
...
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-amqp</artifactId>
<version>${activemq.version}</version>
</dependency>
...
</dependencies>
</dependencyManagement>6. 如何查看完整的依賴樹?
在Maven項目中,可以使用以下命令查看依賴樹:
mvn dependency:tree
在Gradle項目中,可以使用:
gradle dependencies
7. 依賴沖突解決
由于spring-boot-dependencies已經(jīng)管理了版本,通常不會出現(xiàn)版本沖突。但如果需要覆蓋某個依賴的版本,可以在項目的pom.xml中顯式聲明該依賴并指定版本(會覆蓋spring-boot-dependencies中的版本)。例如:
<dependencies> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.5</version> </dependency> </dependencies>
8.企業(yè)級實踐建議

安全更新策略
版本鎖定:在父 POM 中固定 Spring Boot 版本
<properties>
<spring-boot.version>3.1.0</spring-boot.version>
</properties>漏洞掃描:定期檢查依賴漏洞
mvn org.owasp:dependency-check-maven:check
增量更新:使用版本范圍謹慎升級
<spring-boot.version>[3.0.0,3.2.0)</spring-boot.version>
通過 spring-boot-dependencies,Spring Boot 團隊為開發(fā)者提供了:
- 經(jīng)過嚴格測試的依賴兼容矩陣
- 一站式的版本管理入口
- 零配置的依賴沖突解決方案
- 靈活的版本覆蓋機制
9. 總結(jié)
spring-boot-dependencies是Spring Boot的版本管理POM,它統(tǒng)一管理了大量第三方依賴的版本。- 通過starter,我們可以輕松引入一組功能相關(guān)的依賴,并且這些依賴的版本都是經(jīng)過測試兼容的。
- 依賴樹結(jié)構(gòu)清晰,可以通過Maven或Gradle命令查看。
- 在需要時,可以覆蓋默認的依賴版本。
通過這種方式,Spring Boot極大地簡化了依賴管理,讓開發(fā)者可以專注于業(yè)務(wù)邏輯。
二、spring-boot-starter-parent
1.簡介
首先,spring-boot-starter-parent是一個父POM,它繼承自spring-boot-dependencies,并且添加了默認的構(gòu)建配置。
下面詳細說明其內(nèi)容以及依賴樹結(jié)構(gòu)(注意:依賴樹結(jié)構(gòu)通常是指項目依賴的傳遞性依賴,但這里我們更關(guān)注的是父POM的繼承結(jié)構(gòu)和它帶來的構(gòu)建配置)。
2 .結(jié)構(gòu)圖
結(jié)構(gòu)圖:
項目POM -> 繼承 spring-boot-starter-parent -> 繼承 spring-boot-dependencies
項目POM -> spring-boot-starter-parent -> spring-boot-dependencies
構(gòu)建配置的繼承關(guān)系(非依賴樹,而是POM繼承關(guān)系)可以用下圖表示:

具體內(nèi)容:
- 繼承
spring-boot-dependencies:因此獲得了所有依賴版本管理。 - 默認配置:包括資源過濾、插件配置(如
maven-compiler-plugin,maven-jar-plugin,maven-war-plugin,maven-surefire-plugin等)、資源文件處理(application.properties和application.yml的過濾)、默認的Java版本和編碼等。
3.主要內(nèi)容
由于spring-boot-starter-parent本身是一個POM,我們可以查看其內(nèi)容(以3.1.0版本為例):
官方地址:https://repo1.maven.org/maven2/org/springframework/boot/spring-boot-starter-parent/3.1.0/spring-boot-starter-parent-3.1.0.pom
該POM的主要部分如下:
- 繼承spring-boot-dependencies:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>3.1.0</version> </parent>
- 定義屬性(覆蓋或新增):
<properties> <java.version>17</java.version> <!-- 默認Java版本 --> <resource.delimiter>@</resource.delimiter> <!-- 資源過濾占位符分隔符 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- 編碼 --> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- 其他屬性 --> </properties>
- 構(gòu)建配置(build):
- 資源過濾配置:對src/main/resources和src/test/resources目錄進行過濾,使用上面的占位符分隔符。
- 插件管理:配置了多個插件的默認行為。
- 插件配置:
maven-compiler-plugin:設(shè)置源代碼和目標的Java版本。maven-failsafe-plugin:配置測試運行。spring-boot-maven-plugin:用于打包可執(zhí)行jar。
- 資源過濾(resources):
默認配置了資源過濾,使得在資源文件中可以使用占位符(如@variable@)進行替換。
注意:spring-boot-starter-parent本身不包含任何代碼,也不直接添加任何依賴(除了構(gòu)建插件),它的主要作用是提供構(gòu)建配置和依賴版本管理。
當我們創(chuàng)建一個項目并繼承spring-boot-starter-parent時,項目的POM結(jié)構(gòu)如下:
項目POM:
<project> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.1.0</version> </parent> ... 項目自己的定義 ... </project>
具體構(gòu)建配置的內(nèi)容:
在spring-boot-starter-parent中,配置了以下插件:
- maven-compiler-plugin:設(shè)置源代碼和目標版本為${java.version}。
- maven-failsafe-plugin:默認使用JUnit,并配置了必要的參數(shù)。
- spring-boot-maven-plugin:用于打包可執(zhí)行的jar或war。
- 其他插件:如maven-jar-plugin, maven-war-plugin等的基本配置。
另外,資源過濾配置如下:
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering> <!-- 啟用過濾 -->
<includes>
<include>**/application*.yml</include>
<include>**/application*.yaml</include>
<include>**/application*.properties</include>
</includes>
</resource>
<!-- 其他資源目錄,不啟用過濾 -->
<resource>
<directory>${basedir}/src/main/resources</directory>
<excludes>
<exclude>**/application*.yml</exclude>
<exclude>**/application*.yaml</exclude>
<exclude>**/application*.properties</exclude>
</excludes>
</resource>
</resources>注意:在spring-boot-starter-parent中,資源過濾默認只針對application*.properties, application*.yml, application*.yaml文件。
4.總結(jié):
spring-boot-starter-parent提供了:
- 依賴管理(通過繼承spring-boot-dependencies)。
- 默認的構(gòu)建配置(包括插件配置和資源過濾)。
- 默認的屬性配置(如Java版本、編碼等)。
這使得開發(fā)者無需關(guān)心構(gòu)建細節(jié),只需關(guān)注業(yè)務(wù)代碼。
示例:一個典型的Spring Boot項目的POM:
<project> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.1.0</version> </parent> <groupId>com.example</groupId> <artifactId>myproject</artifactId> <version>1.0.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
在這個項目中,我們不需要指定依賴的版本,因為父POM已經(jīng)管理了。同時,構(gòu)建時使用的Java版本、編碼、資源過濾等都已經(jīng)配置好。
注意:如果項目需要自定義父POM,則不能直接繼承spring-boot-starter-parent,這時可以采用之前介紹的方式,通過dependencyManagement導入spring-boot-dependencies,并手動配置插件和資源過濾。
三、spring-boot-starter-web
1.簡介
spring-boot-starter-web是 Spring Boot 的核心 Starter 之一,用于快速構(gòu)建 Web 應用程序。它提供了完整的 Spring MVC 框架支持和內(nèi)嵌 Servlet 容器,讓開發(fā)者能立即創(chuàng)建 RESTful 服務(wù)或傳統(tǒng) Web 應用。
2.結(jié)構(gòu)圖


3.使用
自動配置行為
當檢測到 spring-boot-starter-web 在類路徑中時,自動觸發(fā)以下配置:
1. Servlet 容器配置
java
@AutoConfiguration
@ConditionalOnWebApplication
@EnableConfigurationProperties(ServerProperties.class)
public class ServletWebServerFactoryAutoConfiguration {
// 自動配置內(nèi)嵌Tomcat
@Bean
public TomcatServletWebServerFactory tomcatServletWebServerFactory() {
return new TomcatServletWebServerFactory();
}
}2. Spring MVC 配置
@Configuration
@ConditionalOnClass(DispatcherServlet.class)
@AutoConfigureAfter(ServletWebServerFactoryAutoConfiguration.class)
public class WebMvcAutoConfiguration {
// 配置視圖解析器
@Bean
@ConditionalOnMissingBean
public InternalResourceViewResolver defaultViewResolver() {
// ...
}
// 配置消息轉(zhuǎn)換器
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
// ...
}
}3. 默認行為
- 端口:8080
- 上下文路徑:/
- 靜態(tài)資源目錄:
/static
/public
/resources
/META-INF/resources - 錯誤頁面:/error 自動映射
- JSON 支持:自動配置 Jackson
替換tomcat:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>3.http處理流程
4.應用示例
1. 基本配置
server:
port: 8081
servlet:
context-path: /api
tomcat:
max-threads: 2002. 靜態(tài)資源配置
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/custom/**")
.addResourceLocations("classpath:/custom-static/");
}
}3. 攔截器配置
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LogInterceptor())
.addPathPatterns("/api/**");
}
}4.RESTful服務(wù)開發(fā)
@RestController
@RequestMapping("/api/users")
public class UserController {
@GetMapping
public ResponseEntity<List<User>> getUsers() {
// 返回200 OK + JSON列表
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public User createUser(@Valid @RequestBody User user) {
// 參數(shù)自動驗證
}
}5.全局異常處理
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(UserNotFoundException.class)
public ResponseEntity<ErrorResponse> handleUserNotFound(
UserNotFoundException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(new ErrorResponse(ex.getMessage()));
}
}6. 性能優(yōu)化配置
spring:
mvc:
async:
request-timeout: 5000 # 異步超時時間
servlet:
multipart:
max-file-size: 10MB
max-request-size: 20MB
server:
compression:
enabled: true
mime-types: text/html,text/css,application/json與其他 Starter 的協(xié)作

@SpringBootApplication
@EnableJpaRepositories // 啟用JPA
@EnableWebSecurity // 啟用安全
public class FullStackApp {
public static void main(String[] args) {
SpringApplication.run(FullStackApp.class, args);
}
}7.總結(jié)
性能特性
| 場景 | 默認配置 | 優(yōu)化建議 |
|---|---|---|
| 線程池 | Tomcat 200線程 | 根據(jù)CPU核心數(shù)調(diào)整 |
| 連接超時 | 無限制 | 設(shè)置server.tomcat.connection-timeout |
| 響應壓縮 | 關(guān)閉 | 開啟server.compression.enabled=true |
| 靜態(tài)資源緩存 | 關(guān)閉 | 添加Cache-Control頭 |
| JSON序列化 | 全字段序列化 | 使用@JsonView控制字段 |
通過 spring-boot-starter-web,開發(fā)者能夠:
- 在 30 秒內(nèi)創(chuàng)建可運行的 Web 應用
- 獲得生產(chǎn)就緒的 HTTP 服務(wù)能力
- 自動處理 90% 的 Web 開發(fā)通用配置
- 靈活擴展 MVC 功能
- 無縫整合 Spring 生態(tài)系統(tǒng)組件
此 Starter 將傳統(tǒng) Spring MVC 應用的配置工作量減少 80%,同時保持完整的靈活性,是構(gòu)建現(xiàn)代 Java Web 應用的基石。
到此這篇關(guān)于Springboot項目構(gòu)建時各種依賴詳細介紹與依賴關(guān)系說明的文章就介紹到這了,更多相關(guān)Springboot依賴內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

