【问题标题】:Should I include the dependency spring-boot-starter into my custom spring boot starter?我应该将依赖项 spring-boot-starter 包含到我的自定义 Spring Boot 启动器中吗?
【发布时间】:2020-03-26 10:27:42
【问题描述】:

如果我创建一个自定义的 Spring Boot 启动模块,是否需要包含依赖项 spring-boot-starter

spring boot's github

是否有理由不将其添加到依赖项中?

【问题讨论】:

  • 不需要包含 spring-boot-starter 的依赖。仅当您需要自己的启动器的传递依赖项时。但是你的启动器不应该以 spring-* 开头。关键字 spring 是保留的。前缀应该是您的启动器的名称。例如见baeldung.com/spring-boot-custom-starter
  • 我已经知道我的启动器不应该以 spring-* 开头,这不是我的问题。在 baldung 示例中,启动项目包含对 spring-boot-starter 的依赖项,因为它不使用它到自定义启动程序中。

标签: spring-boot spring-boot-starter


【解决方案1】:

如果您的 starter 依赖于 spring-boot-starter,那么任何仅依赖于您的 starter 的应用程序都将拥有成为 Spring Boot 应用程序所需的所有依赖项。一般来说,这就是您希望启动器的行为方式。

spring-boot-stater-log4j2spring-boot-starter-undertowspring-boot-starter-tomcat 略有不同,因为它们不能单独使用。 Spring Boot documentation calls them technical starters。它们旨在与现有的启动器一起使用,以更改所使用的底层技术。例如,如果您正在构建一个 Web 应用程序,您将依赖于 spring-boot-starter-web。此启动器默认使用 Tomcat 作为嵌入式容器。如果你想切换到 Undertow,你需要排除 spring-boot-starter-tomcat 并在 spring-boot-starter-web 依赖项旁边添加对 spring-boot-starter-undertow 的依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <!-- Exclude the Tomcat dependency -->
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- Use Undertow instead -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

【讨论】:

  • 如果客户端项目使用与我的启动项目中指定的版本不同的 Spring Boot 版本怎么办?当包含我的自定义启动项目时,他将获得两个版本的 spring-boot、spring-core 等...
  • 他们不会得到两个版本。构建系统将确保每个依赖项只使用一个版本。控制哪个版本最常用的方法是使用 Spring Boot 的依赖管理。
  • 你是对的,如果找到多个,maven 会选择最近的(在依赖树中)spring 依赖
猜你喜欢
  • 2016-06-29
  • 1970-01-01
  • 2018-01-10
  • 2021-04-29
  • 1970-01-01
  • 2015-12-28
  • 1970-01-01
  • 2019-08-06
  • 2017-06-17
相关资源
最近更新 更多