文章作者: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 runsudo 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-parent
project 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 animport
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附帶的腳本為BASH和zsh 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)境變量痊硕。