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 使創(chuàng)建獨立的进统、產(chǎn)品級的、基于Spring的應用變得更容易浪听,你只需要運行run即可螟碎。我們采用不變的Spring平臺和第三方庫,因此你可以幾乎無差別的使用Spring Boot迹栓。大多數(shù)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創(chuàng)建Java應用,用java-jar或更傳統(tǒng)的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開發(fā)者提供一個從根本上更迅速可用的入門經(jīng)驗华坦。

  • 堅持開箱即用,當默認設置不滿足需求時可以快速避免默認設置不从。

  • 為具有許多類的工程提供一系列常用的非功能特性(例如嵌入式服務器惜姐、安全、度量椿息、健康檢查歹袁、外部配置)。

  • 絕對沒有XML配置代碼產(chǎn)生和XML配置需求寝优。

9. System Requirements

默認情況下条舔,Spring Boot 2.0.0.BUILD-SNAPSHOT需要Java 7和Spring Framework 5.0.0.BUILD-SNAPSHOT及以上。你可以通過一些額外配置在Java 6下使用Spring Boot乏矾。更多細節(jié)請看80.11小節(jié)孟抗,"怎樣使用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可以和"經(jīng)典"的Java開發(fā)工具一起使用,也可作為一個命令行工具來進行安裝亿胸。不管你是需要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開發(fā)的新手侈玄,或你只想嘗試Spring Boot婉刀,你可能首先想試一下Spring Boot CLI,否則序仙,請讀"經(jīng)典"的安裝說明突颊。

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程序一樣來運行和調(diào)試船惨。

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疙挺,你可以根據(jù)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.

?

在許多操作系統(tǒng)上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插件來創(chuàng)建可執(zhí)行的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小節(jié),『沒有父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,你可以根據(jù)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插件用來簡化依賴聲明并創(chuàng)建可執(zhí)行的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。它是一小段腳本和庫朴爬,并隨著你的代碼一起提交來引導構建過程即寒。更多細節(jié)請看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發(fā)行版:

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開發(fā)功能并向很容易的訪問你夠簡單版本抖单,按照這些額外的介紹去做。

$ 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可能過時了囚玫。執(zhí)行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提供了命令行實現(xiàn)抓督。你可以在任何shell中source腳本(也稱為spring),或?qū)⑺旁谟脩艋蛳到y(tǒng)范圍內(nèi)的bash完成初始化束亏。在Debian系統(tǒng)中系統(tǒng)范圍內(nèi)的腳本是在/shell-completion/bash目錄中铃在,當新shell啟動時,目錄下的所有腳本都被執(zhí)行碍遍。如果你裝了SDKMAN!定铜,為了手動運行腳本,執(zhí)行下面的命令:

$ . ~/.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應用,你可以用來測試你的安裝赖捌。創(chuàng)建一個文件叫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中的『發(fā)布說明』卤唉。你將找到升級說明涩惑,說明中有每一次發(fā)布的特性列表。

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.

為了升級現(xiàn)有的CLI桑驱,要使用合適的包管理命令(例如brew upgrade)竭恬,如果你手動安裝的CLI跛蛋,要按照標準的說明去做,為了移除舊的引用要記住更新你的PATH環(huán)境變量痊硕。

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末赊级,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子岔绸,更是在濱河造成了極大的恐慌理逊,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件盒揉,死亡現(xiàn)場離奇詭異晋被,居然都是意外死亡,警方通過查閱死者的電腦和手機刚盈,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進店門羡洛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人扁掸,你說我怎么就攤上這事翘县∽钣颍” “怎么了谴分?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵秩伞,是天一觀的道長袖牙。 經(jīng)常有香客問我,道長撮执,這世上最難降的妖魔是什么薄翅? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任沙兰,我火速辦了婚禮,結果婚禮上翘魄,老公的妹妹穿的比我還像新娘鼎天。我一直安慰自己,他們只是感情好暑竟,可當我...
    茶點故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布斋射。 她就那樣靜靜地躺著,像睡著了一般但荤。 火紅的嫁衣襯著肌膚如雪罗岖。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天腹躁,我揣著相機與錄音桑包,去河邊找鬼。 笑死纺非,一個胖子當著我的面吹牛哑了,可吹牛的內(nèi)容都是我干的赘方。 我是一名探鬼主播,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼弱左,長吁一口氣:“原來是場噩夢啊……” “哼蒜焊!你這毒婦竟也來了?” 一聲冷哼從身側響起科贬,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤泳梆,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后榜掌,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體优妙,經(jīng)...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年憎账,在試婚紗的時候發(fā)現(xiàn)自己被綠了套硼。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,096評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡胞皱,死狀恐怖邪意,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情反砌,我是刑警寧澤雾鬼,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站宴树,受9級特大地震影響策菜,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜酒贬,卻給世界環(huán)境...
    茶點故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一又憨、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧锭吨,春花似錦蠢莺、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至埠况,卻和暖如春耸携,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背辕翰。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工夺衍, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人喜命。 一個月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓沟沙,卻偏偏與公主長得像河劝,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子矛紫,可洞房花燭夜當晚...
    茶點故事閱讀 45,037評論 2 355

推薦閱讀更多精彩內(nèi)容