java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Springboot依赖

Springboot项目构建时各种依赖详细介绍与依赖关系说明详解

作者:yyangjun

SpringBoot通过spring-boot-dependencies统一依赖版本管理,spring-boot-starter-web简化Web开发流程,自动配置核心功能,提升开发效率与兼容性,下面通过本文介绍Springboot项目构建时各种依赖详细介绍与依赖关系说明,感兴趣的朋友一起看看吧

一、spring-boot-dependencies

1.简介

  spring-boot-dependencies 是一个特殊的POM(Project Object Model)文件,它由Spring Boot团队维护,其主要作用是为Spring Boot相关的依赖提供统一的版本管理。它定义了大量的依赖及其兼容版本,确保Spring Boot生态中的库能够协同工作。官方仓库地址:
https://repo1.maven.org/maven2/org/springframework/boot/spring-boot-dependencies/3.1.0/spring-boot-dependencies-3.1.0.pom(版本可依据具体项目要求更改)

2. 内容概览

spring-boot-dependencies中,主要包含以下内容:

3.核心内容结构

4. 依赖树结构

  spring-boot-dependencies本身并不引入任何依赖,它只是声明依赖版本。当我们在项目中引入一个starter(比如spring-boot-starter-web)时,Maven会根据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已经管理了版本,通常不会出现版本冲突。但如果需要覆盖某个依赖的版本,可以在项目的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.企业级实践建议

安全更新策略

版本锁定:在父 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 团队为开发者提供了:

9. 总结

通过这种方式,Spring Boot极大地简化了依赖管理,让开发者可以专注于业务逻辑。

二、spring-boot-starter-parent

1.简介

  首先,spring-boot-starter-parent是一个父POM,它继承自spring-boot-dependencies,并且添加了默认的构建配置。

  下面详细说明其内容以及依赖树结构(注意:依赖树结构通常是指项目依赖的传递性依赖,但这里我们更关注的是父POM的继承结构和它带来的构建配置)。

2 .结构图

结构图:

项目POM -> 继承 spring-boot-starter-parent -> 继承 spring-boot-dependencies

项目POM -> spring-boot-starter-parent -> spring-boot-dependencies

构建配置的继承关系(非依赖树,而是POM继承关系)可以用下图表示:

具体内容:

3.主要内容

由于spring-boot-starter-parent本身是一个POM,我们可以查看其内容(以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的主要部分如下

  1. 继承spring-boot-dependencies
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.1.0</version>
</parent>
  1. 定义属性(覆盖或新增)
<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>
  1. 构建配置(build)
  1. 插件配置
  1. 资源过滤(resources):

默认配置了资源过滤,使得在资源文件中可以使用占位符(如@variable@)进行替换。

注意:spring-boot-starter-parent本身不包含任何代码,也不直接添加任何依赖(除了构建插件),它的主要作用是提供构建配置和依赖版本管理。

当我们创建一个项目并继承spring-boot-starter-parent时,项目的POM结构如下:

项目POM:

<project>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.0</version>
</parent>
... 项目自己的定义 ...
</project>

具体构建配置的内容:

在spring-boot-starter-parent中,配置了以下插件:

另外,资源过滤配置如下:

<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.总结:

spring-boot-starter-parent提供了:

这使得开发者无需关心构建细节,只需关注业务代码。

示例:一个典型的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已经管理了。同时,构建时使用的Java版本、编码、资源过滤等都已经配置好。

注意:如果项目需要自定义父POM,则不能直接继承spring-boot-starter-parent,这时可以采用之前介绍的方式,通过dependencyManagement导入spring-boot-dependencies,并手动配置插件和资源过滤。

三、spring-boot-starter-web

1.简介

  spring-boot-starter-web是 Spring Boot 的核心 Starter 之一,用于快速构建 Web 应用程序。它提供了完整的 Spring MVC 框架支持和内嵌 Servlet 容器,让开发者能立即创建 RESTful 服务或传统 Web 应用。

2.结构图

3.使用

自动配置行为
当检测到 spring-boot-starter-web 在类路径中时,自动触发以下配置:

1. Servlet 容器配置

java
@AutoConfiguration
@ConditionalOnWebApplication
@EnableConfigurationProperties(ServerProperties.class)
public class ServletWebServerFactoryAutoConfiguration {
    // 自动配置内嵌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() {
        // ...
    }
    // 配置消息转换器
    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        // ...
    }
}

3. 默认行为

替换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处理流程

客户端 Tomcat 容器 DispatcherServlet @RestController HTTP 请求 请求分发 映射到处理方法 返回结果 响应处理 HTTP 响应 客户端 Tomcat 容器 DispatcherServlet @RestController

4.应用示例

1. 基本配置

server:
  port: 8081
  servlet:
    context-path: /api
  tomcat:
    max-threads: 200

2. 静态资源配置

@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服务开发

@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) {
        // 参数自动验证
    }
}

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. 性能优化配置

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 的协作

@SpringBootApplication
@EnableJpaRepositories // 启用JPA
@EnableWebSecurity // 启用安全
public class FullStackApp {
    public static void main(String[] args) {
        SpringApplication.run(FullStackApp.class, args);
    }
}

7.总结

性能特性

场景默认配置优化建议
线程池Tomcat 200线程根据CPU核心数调整
连接超时无限制设置server.tomcat.connection-timeout
响应压缩关闭开启server.compression.enabled=true
静态资源缓存关闭添加Cache-Control头
JSON序列化全字段序列化使用@JsonView控制字段

通过 spring-boot-starter-web,开发者能够:

此 Starter 将传统 Spring MVC 应用的配置工作量减少 80%,同时保持完整的灵活性,是构建现代 Java Web 应用的基石。

到此这篇关于Springboot项目构建时各种依赖详细介绍与依赖关系说明的文章就介绍到这了,更多相关Springboot依赖内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文