Spring Boot 简明教程

Spring Boot - Build Systems

在 Spring Boot 中,选择构建系统是一项重要任务。我们推荐 Maven 或 Gradle,因为它们为依赖管理提供了良好的支持。Spring 很好地不支持其他构建系统。

In Spring Boot, choosing a build system is an important task. We recommend Maven or Gradle as they provide a good support for dependency management. Spring does not support well other build systems.

Dependency Management

Spring Boot 团队提供了依赖项列表来为其每个版本的 Spring Boot 版本提供支持。你无需在构建配置文件中为依赖项提供版本。Spring Boot 会根据版本自动配置依赖项版本。请记住,当你升级 Spring Boot 版本时,依赖项也会自动升级。

Spring Boot team provides a list of dependencies to support the Spring Boot version for its every release. You do not need to provide a version for dependencies in the build configuration file. Spring Boot automatically configures the dependencies version based on the release. Remember that when you upgrade the Spring Boot version, dependencies also will upgrade automatically.

Note - 如果你想为依赖项指定版本,你可以在配置文件中指定它。但是,Spring Boot 团队强烈建议不需要为依赖项指定版本。

Note − If you want to specify the version for dependency, you can specify it in your configuration file. However, the Spring Boot team highly recommends that it is not needed to specify the version for dependency.

Maven Dependency

对于 Maven 配置,我们应继承 Spring Boot Starter 父项目以管理 Spring Boot Starter 依赖项。为此,我们可以像下面所示一样简单地继承 our pom.xml 文件中的 starter parent。

For Maven configuration, we should inherit the Spring Boot Starter parent project to manage the Spring Boot Starters dependencies. For this, simply we can inherit the starter parent in our pom.xml file as shown below.

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>1.5.8.RELEASE</version>
</parent>

我们应指定 Spring Boot Parent Starter 依赖项的版本号。然后,对于其他 starter 依赖项,我们不需要指定 Spring Boot 版本号。观察下面给出的代码 −

We should specify the version number for Spring Boot Parent Starter dependency. Then for other starter dependencies, we do not need to specify the Spring Boot version number. Observe the code given below −

<dependencies>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
</dependencies>

Gradle Dependency

我们可以直接将 Spring Boot Starter 依赖项导入 build.gradle 文件中。我们不需要像针对 Gradle 的 Maven 那样的 Spring Boot Start 父依赖项。观察下面给出的代码 −

We can import the Spring Boot Starters dependencies directly into build.gradle file. We do not need Spring Boot start Parent dependency like Maven for Gradle. Observe the code given below −

buildscript {
   ext {
      springBootVersion = '1.5.8.RELEASE'
   }
   repositories {
      mavenCentral()
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
   }
}

类似地,在 Gradle 中,我们不需要为依赖项指定 Spring Boot 版本号。Spring Boot 根据版本自动配置依赖项。

Similarly, in Gradle, we need not specify the Spring Boot version number for dependencies. Spring Boot automatically configures the dependency based on the version.

dependencies {
   compile('org.springframework.boot:spring-boot-starter-web')
}