Spring Boot 2.0.0参考手册_中英文对照_Part II_8-10

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

Part II. Getting started

If you’re just getting started with Spring Boot, or 'Spring' in general, this is the section for you! Here we answer the basic “what?”, “how?” and “why?” questions. You’ll find a gentle introduction to Spring Boot along with installation instructions. We’ll then build our first Spring Boot application, discussing some core principles as we go.

如果你刚开始学习Spring Boot或通常所说的Spring,这部分就是为你准备的!这部分中我们回答了基本的"what?","how","why?"问题,并在安装说明中介绍Spring Boot。我们将构建第一个Spring Boot应用,讨论一些我们遵循的核心原则。

8. Introducing Spring Boot

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”. We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.

Spring Boot 使创建独立的、产品级的、基于Spring的应用变得更容易,你只需要运行run即可。我们采用不变的Spring平台和第三方库,因此你可以几乎无差别的使用Spring Boot。大多数Spring Boot应用只需要很少的Spring配置。

You can use Spring Boot to create Java applications that can be started using java-jar or more traditional war deployments. We also provide a command line tool that runs “spring scripts”.

你可以使用Spring Boot创建Java应用,用java-jar或更传统的war包来部署应用。我们也提供了运行"Spring脚本"的命令行工具。

Our primary goals are:

  • Provide a radically faster and accessible getting started experience for all Spring development.

  • Be opinionated out of the box, but get out of the way quickly as requirements start to diverge from the defaults.

  • Provide a range of non-functional features that are common to large classes of projects (e.g. embedded servers, security, metrics, health checks, externalized configuration).

  • Absolutely no code generation and no requirement for XML configuration.

我们的主要目标是:

  • 为所有Spring开发者提供一个从根本上更迅速可用的入门经验。

  • 坚持开箱即用,当默认设置不满足需求时可以快速避免默认设置。

  • 为具有许多类的工程提供一系列常用的非功能特性(例如嵌入式服务器、安全、度量、健康检查、外部配置)。

  • 绝对没有XML配置代码产生和XML配置需求。

9. System Requirements

默认情况下,Spring Boot 2.0.0.BUILD-SNAPSHOT需要Java 7和Spring Framework 5.0.0.BUILD-SNAPSHOT及以上。你可以通过一些额外配置在Java 6下使用Spring Boot。更多细节请看80.11小节,"怎样使用Java 6"。明确的构建支持有Maven (3.2+)和Gradle (1.12 or 2.x),不支持Gradle 3。

虽然你可以在Java 6或Java 7下使用Spring Boot,但我们建议尽可能的使用Java 8。

9.1 Servlet containers

The following embedded servlet containers are supported out of the box:

Name Servlet Version Java Version
Tomcat 8 3.1 Java 7+
Tomcat 7 3.0 Java 6+
Jetty 9.3 3.1 Java 8+
Jetty 9.2 3.1 Java 7+
Jetty 8 3.0 Java 6+
Undertow 1.3 3.1 Java 7+

You can also deploy Spring Boot applications to any Servlet 3.0+ compatible container.

下面的嵌入式servlet容器支持开箱即用:

Name Servlet Version Java Version
Tomcat 8 3.1 Java 7+
Tomcat 7 3.0 Java 6+
Jetty 9.3 3.1 Java 8+
Jetty 9.2 3.1 Java 7+
Jetty 8 3.0 Java 6+
Undertow 1.3 3.1 Java 7+

你也可以部署Spring Boot应用到任何兼容Servlet 3.0+的容器。

10. Installing Spring Boot

Spring Boot can be used with “classic” Java development tools or installed as a command line tool. Regardless, you will need Java SDK v1.6 or higher. You should check your current Java installation before you begin:

Spring Boot可以和"经典"的Java开发工具一起使用,也可作为一个命令行工具来进行安装。不管你是需要Java SDK v.16还是更高版本,在开始之前你应该检查你当前安装的Java版本。

$ java -version

If you are new to Java development, or if you just want to experiment with Spring Boot you might want to try the Spring Boot CLI first, otherwise, read on for “classic” installation instructions.

如果你是Java开发的新手,或你只想尝试Spring Boot,你可能首先想试一下Spring Boot CLI,否则,请读"经典"的安装说明。

Although Spring Boot is compatible with Java 1.6, if possible, you should consider using the latest version of Java.

 

虽然Spring Boot兼容Java 1.6,但可能的话,你应该考虑使用Java的最新版本。

10.1 Installation instructions for the Java developer

You can use Spring Boot in the same way as any standard Java library. Simply include the appropriate spring-boot-*.jar files on your classpath. Spring Boot does not require any special tools integration, so you can use any IDE or text editor; and there is nothing special about a Spring Boot application, so you can run and debug as you would any other Java program.

你可以使用Spring Boot像使用任何标准Java库一样。简单的在你的classpath中包含恰当spring-boot-*.jar即可。Spring Boot不需要任何特定的工具集成,因此你可以使用任何IDE或文本编辑器;Spring Boot应用没有什么特别的,你可以像其它的Java程序一样来运行和调试。

Although you could just copy Spring Boot jars, we generally recommend that you use a build tool that supports dependency management (such as Maven or Gradle).

尽管你可以只拷贝Spring Boot的jars,但通常我们建议你使用一个支持依赖管理的构建工具(例如Maven或Gradle)。

10.1.1 Maven installation

Spring Boot is compatible with Apache Maven 3.2 or above. If you don’t already have Maven installed you can follow the instructions at maven.apache.org.

Spring Boot兼容Apache Maven 3.2或以上。如果你没有安装Maven,你可以根据maven.apache.org的说明来安装。

On many operating systems Maven can be installed via a package manager. If you’re an OSX Homebrew user try brew install maven. Ubuntu users can run sudo apt-get install maven.

 

在许多操作系统上Maven都能通过包管理器来安装。如果你是一个OSX Homebrew用户可以通过brew install maven安装。Ubuntu用户可以通过sudo apt-get install maven安装。

Spring Boot dependencies use the org.springframework.boot groupId. Typically your Maven POM file will inherit from the spring-boot-starter-parentproject and declare dependencies to one or more “Starters”. Spring Boot also provides an optional Maven plugin to create executable jars.

Spring Boot依赖使用org.springframework.boot groupId。通常你的Maven POM文件将继承spring-boot-starter-parent工程并声明一个或多个“Starters”依赖。Spring Boot也提供一个可选的Maven插件来创建可执行的jar包。

Here is a typical pom.xml file:

下面是一个典型的pom.xml文件:

<?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.example</groupId>
    <artifactId>myproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.BUILD-SNAPSHOT</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <!-- Add Spring repositories -->
    <!-- (you don't need this if you are using a .RELEASE version) -->
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>
</project>

The spring-boot-starter-parent is a great way to use Spring Boot, but it might not be suitable all of the time. Sometimes you may need to inherit from a different parent POM, or you might just not like our default settings. See Section 13.2.2, “Using Spring Boot without the parent POM” for an alternative solution that uses an import scope.

 

spring-boot-starter-parent是使用Spring Boot的一种极好的方式,但它可能不是一直都合适的。有时你可能需要继承一个不同的父POM,或者你可能不喜欢我们的默认设置。请看13.2.2小节,『没有父POM的情况下使用Spring Boot』是一种可替代的解决方案,它使用了import作用域。

10.1.2 Gradle installation

Spring Boot is compatible with Gradle 1.12 or 2.x. 2.14.1 is recommended. Gradle 3 is not supported. If you don’t already have Gradle installed you can follow the instructions at www.gradle.org/.

Spring Boot兼容Gradle 1.12或2.x。推荐使用2.14.1。不支持Gradle 3。如果你没有安装Gradle,你可以根据www.gradle.org/的介绍来安装。

Spring Boot dependencies can be declared using the org.springframework.boot group. Typically your project will declare dependencies to one or more “Starters”. Spring Boot provides a useful Gradle plugin that can be used to simplify dependency declarations and to create executable jars.

Spring Boot依赖使用org.springframework.boot groupId。通常你的工程会声明一个或多个“Starters”依赖。Spring Boot提供了一个有用的Gradle插件用来简化依赖声明并创建可执行的jar包。

Gradle Wrapper

The Gradle Wrapper provides a nice way of “obtaining” Gradle when you need to build a project. It’s a small script and library that you commit alongside your code to bootstrap the build process. See https://docs.gradle.org/2.14.1/userguide/gradle_wrapper.html for details.

Gradle Wrapper

当你需要构建一个工程时,Gradle Wrapper提供了一种很好的方式来『获得』Gradle。它是一小段脚本和库,并随着你的代码一起提交来引导构建过程。更多细节请看https://docs.gradle.org/2.14.1/userguide/gradle_wrapper.html

Here is a typical build.gradle file:

下面是一个典型的build.gradle文件:

buildscript {
    repositories {
        jcenter()
        maven { url "http://repo.spring.io/snapshot" }
        maven { url "http://repo.spring.io/milestone" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.0.BUILD-SNAPSHOT")
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'

jar {
    baseName = 'myproject'
    version =  '0.0.1-SNAPSHOT'
}

repositories {
    jcenter()
    maven { url "http://repo.spring.io/snapshot" }
    maven { url "http://repo.spring.io/milestone" }
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

10.2 Installing the Spring Boot CLI

The Spring Boot CLI is a command line tool that can be used if you want to quickly prototype with Spring. It allows you to run Groovy scripts, which means that you have a familiar Java-like syntax, without so much boilerplate code.

Spring Boot CLI是一个命令行工具,可以用来快速搭建基于Spring的原型。它允许你运行Groovy脚本,这意味着你有一个熟悉的类Java语法,没有很多的样本代码。

You don’t need to use the CLI to work with Spring Boot but it’s definitely the quickest way to get a Spring application off the ground.

你不需要使用CLI来使用Spring Boot但它是开始得到一个基于Spring应用的最快方式。

10.2.1 Manual installation

You can download the Spring CLI distribution from the Spring software repository:

你可以从Spring软件仓库下载Spring CLI发行版:

Cutting edge snapshot distributions are also available.

最新的snapshot distributions也是可获得的。

Once downloaded, follow the INSTALL.txt instructions from the unpacked archive. In summary: there is a spring script (spring.bat for Windows) in a bin/ directory in the .zip file, or alternatively you can use java -jar with the .jar file (the script helps you to be sure that the classpath is set correctly).

一旦下载完成,按照存档INSTALL.txt中的 介绍来安装。总结:bin/目录的.zip文件中有一个spring脚本(Windows中是spring.bat),或者你可以使用java -jar运行.jar文件(这个脚本帮你确保正确设置classpath)。

10.2.2 Installation with SDKMAN!

SDKMAN! (The Software Development Kit Manager) can be used for managing multiple versions of various binary SDKs, including Groovy and the Spring Boot CLI. Get SDKMAN! from sdkman.io and install Spring Boot with:

SDKMAN! (The Software Development Kit Manager)能用来管理各种二进制SDKs的多个版本,包括Groovy和Spring Boot CLI。从sdkman.io中得到SDKMAN!并安装Spring Boot:

$ sdk install springboot
$ spring --version
Spring Boot v2.0.0.BUILD-SNAPSHOT

If you are developing features for the CLI and want easy access to the version you just built, follow these extra instructions.

如果你在为CLI开发功能并向很容易的访问你够简单版本,按照这些额外的介绍去做。

$ sdk install springboot dev /path/to/spring-boot/spring-boot-cli/target/spring-boot-cli-2.0.0.BUILD-SNAPSHOT-bin/spring-2.0.0.BUILD-SNAPSHOT/
$ sdk default springboot dev
$ spring --version
Spring CLI v2.0.0.BUILD-SNAPSHOT

This will install a local instance of spring called the dev instance. It points at your target build location, so every time you rebuild Spring Boot, spring will be up-to-date.

这将会安装一个名为dev的本地spring实例。它指向你的目标构建位置,因此你每次重新构建Spring Boot,spring将是最新的。

You can see it by doing this:

按下面去做你会看到:

$ sdk ls springboot

================================================================================
Available Springboot Versions
================================================================================
> + dev
* 2.0.0.BUILD-SNAPSHOT

================================================================================
+ - local version
* - installed
> - currently in use
================================================================================

10.2.3 OSX Homebrew installation

If you are on a Mac and using Homebrew, all you need to do to install the Spring Boot CLI is:

如果你在Mac上使用Homebrew,安装Spring Boot CLI所有你需要做的是:

$ brew tap pivotal/tap
$ brew install springboot

Homebrew will install spring to /usr/local/bin.

Homebrew会在/usr/local/bin目录安装spring

If you don’t see the formula, your installation of brew might be out-of-date. Just execute brew update and try again.

 

如果你没有看到公式,你安装的brew可能过时了。执行brew update并重新再试一次。

10.2.4 MacPorts installation

If you are on a Mac and using MacPorts, all you need to do to install the Spring Boot CLI is:

如果你在Mac上使用MacPorts,安装Spring Boot CLI所有你需要做的是:

$ sudo port install spring-boot-cli

10.2.5 Command-line completion

Spring Boot CLI ships with scripts that provide command completion for BASH and zsh shells. You can source the script (also named spring) in any shell, or put it in your personal or system-wide bash completion initialization. On a Debian system the system-wide scripts are in /shell-completion/bash and all scripts in that directory are executed when a new shell starts. To run the script manually, e.g. if you have installed using SDKMAN!

Spring Boot CLI附带的脚本为BASHzsh shells提供了命令行实现。你可以在任何shell中source脚本(也称为spring),或将它放在用户或系统范围内的bash完成初始化。在Debian系统中系统范围内的脚本是在/shell-completion/bash目录中,当新shell启动时,目录下的所有脚本都被执行。如果你装了SDKMAN!,为了手动运行脚本,执行下面的命令:

$ . ~/.sdkman/candidates/springboot/current/shell-completion/bash/spring
$ spring <HIT TAB HERE>
  grab  help  jar  run  test  version

If you install Spring Boot CLI using Homebrew or MacPorts, the command-line completion scripts are automatically registered with your shell.

 

如果你想使用Homebrew或MacPorts安装Spring Boot CLI,命令行完成脚本会自动在shell中注册。

10.2.6 Quick start Spring CLI example

Here’s a really simple web application that you can use to test your installation. Create a file called app.groovy:

这儿有一个真实的简单的web应用,你可以用来测试你的安装。创建一个文件叫app.groovy

@RestController
class ThisWillActuallyRun {

    @RequestMapping("/")
    String home() {
        "Hello World!"
    }

}

Then simply run it from a shell:

然后简单的从shell中运行它:

$ spring run app.groovy

It will take some time when you first run the application as dependencies are downloaded. Subsequent runs will be much quicker.

 

当你第一次运行应用时它需要一段时间,因为要下载依赖。接下来运行会更快。

Open localhost:8080 in your favorite web browser and you should see the following output:

在你最喜欢的浏览器中输入localhost:8080,你应该会看到下面的结果:

Hello World!

10.3 Upgrading from an earlier version of Spring Boot

If you are upgrading from an earlier release of Spring Boot check the “release notes” hosted on the project wiki. You’ll find upgrade instructions along with a list of “new and noteworthy” features for each release.

如果正在升级你的Spring Boot,检查project wiki中的『发布说明』。你将找到升级说明,说明中有每一次发布的特性列表。

To upgrade an existing CLI installation use the appropriate package manager command (for example brew upgrade) or, if you manually installed the CLI, follow the standard instructions remembering to update your PATH environment variable to remove any older references.

为了升级现有的CLI,要使用合适的包管理命令(例如brew upgrade),如果你手动安装的CLI,要按照标准的说明去做,为了移除旧的引用要记住更新你的PATH环境变量。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,012评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,589评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,819评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,652评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,954评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,381评论 1 210
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,687评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,404评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,082评论 1 238
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,355评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,880评论 1 255
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,249评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,864评论 3 232
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,007评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,760评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,394评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,281评论 2 259

推荐阅读更多精彩内容