您的位置:首页 > 编程语言 > Java开发

Spring Boot参考文档(3)

2017-05-10 12:49 260 查看
原文链接:http://www.itbus.tech/detail.html?id=8705

安装Spring Boot

Spring Boot可以配合经典的JDK环境使用,也可以作为一个命令行工具来安装。不管如何,你需要先安装好Java SDK v1.6 或者更高。你可以检查当前JDK版本:
java -version


如果你对Java开发还不熟悉,或者,你只是想尝试Spring Boot,那你可以选择先用Spring Boot CLI,之后再使用JDK。

虽然Spring Boot兼容JDK6,但是如果可以,还是最好使用最新的JDK。

安装指南

你可以使用标准的Java类库的方法来使用Spring Boot。只需要把spring-boot-*.jar加到你的classpath中。Spring Boot不需要任何特殊的集成工具,所以你可以使用任何IDE甚至是文本编辑器;而且,Spring Boot和一般的Java项目没有什么区别,所以你可以像往常一样来运行和调试。

虽然你可以直接拷贝Spring Boot的jars,但是我们还是推荐使用一个构建工具来管理以来(比如:Maven,Gradle)。

安装Maven

Spring Boot兼容Apache Maven3.2及以上。如果你还没有安装Maven,那么你可以参考maven.apache.org来安装。

在很多操作系统上,都可以使用包管理器直接安装Maven。如果你使用的是OS X,你可以使用
brew install maven
,如果是在Ubuntu上,可以使用
sudo apt-get install maven


Spring Boot的依赖使用的
groupId
org.springframework.boot
。通常,你的Maven的POM文件会继承
spring-boot-starter-parent
,然后申明一个或多个Starters。Spring Boot还提供了一个Maven插件来生成可执行jar。

这列给出一个典型的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>1.5.3.RELEASE</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>

</project>


继承
spring-boot-starter-parent
是使用Spring Boot的最佳方式,但是有的时候,可能不是很适合。有时候,你的项目需要继承其他项目的POM,或者你就是比喜欢我们的默认设定。你可以参考Using Spring Boot without the parent POM,使用
import
作为一个代替方案。

安装Gradle

Spring Boot兼容Gradle 2 (2.9+)和Gradle 3。如果你还没有安装Gradle,可以参考gradle.org

Spring Boot的依赖使用的
groupId
org.springframework.boot
。通常,你的项目会申明一个或多个Starters。Spring Boot也提供了一个有用的Gradle插件来申明依赖,和构建可运行的jar包。

这里给出一个典型的build.gradle文件:

plugins {
id 'org.springframework.boot' version '1.5.3.RELEASE'
id 'java'
}

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

repositories {
jcenter()
}

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


安装Spring Boot CLI

Spring Boot CLI是一个可以让你快速体验Spirng的命令行工具。它还可以运行Groovy脚本,这样可以使用类Java语法,但又可以减少很多不必要的代码。

你可以在工作中不使用Spring Boot CLI,但是Spring Boot CLI绝对是最快上手的方式。

安装手册

你可以从下面的链接下载Spring CLI发布版:

spring-boot-cli-1.5.3.RELEASE-bin.zip

spring-boot-cli-1.5.3.RELEASE-bin.tar.gz

快照版本也是可以获得的。

下载下来后,跟着INSTALL.txt安装。

一个INSTALL.txt一般是这样:

SPRING BOOT CLI - INSTALLATION
==============================

Thank you for downloading the Spring Boot CLI tool. Please follow these instructions
in order to complete your installation.

Prerequisites
-------------
Spring Boot CLI requires Java JDK v1.6 or above in order to run. Groovy v${groovy.version}
is packaged as part of this distribution, and therefore does not need to be installed (any
existing Groovy installation is ignored).

The CLI will use whatever JDK it finds on your path, to check that you have an appropriate
version you should run:

java -version

Alternatively, you can set the JAVA_HOME environment variable to point an suitable JDK.

Environment Variables
---------------------
No specific environment variables are required to run the CLI, however, you may want to
set SPRING_HOME to point to a specific installation.  You should also add SPRING_HOME/bin
to your PATH environment variable.

Shell Completion
----------------
Shell auto-completion scripts are provided for BASH and ZSH. Add symlinks to the appropriate
location for your environment. For example, something like:

ln -s ./shell-completion/bash/spring /etc/bash_completion.d/spring
ln -s ./shell-completion/zsh/_spring /usr/local/share/zsh/site-functions/_spring

Checking Your Installation
--------------------------
To test if you have successfully install the CLI you can run the following command:

spring --version


总结一下就是:在bin目录下,有一个
spring
脚本(在windows下是
spring.bat
);或者,你可以直接使用
java -jar


安装SDKMAN!

SDKMAN!(The Software Development Kit Manager)(软件开发工具管理器)可以被用来管理同一个SDK的不同版本,包括Groovy,Spring Boot CLI。从sdkman.io获得吧。安装Spring Boot:

$ sdk install springboot
$ spring --version
Spring Boot v1.5.3.RELEASE


如果你正在自己开发Spring Boot CLI,又想快速的使用你刚刚构建的版本,你可以这么做:

$ sdk install springboot dev /path/to/spring-boot/spring-boot-cli/target/spring-boot-cli-1.5.3.RELEASE-bin/spring-1.5.3.RELEASE/
$ sdk default springboot dev
$ spring --version
Spring CLI v1.5.3.RELEASE


这样会安装一个
spring
被叫做
dev
。它指定了你的target目录,所以每一次你重新编译Spring Boot,,
spring
都会更新。

你可以这样获得详细信息:

$ sdk ls springboot

================================================================================
Available Springboot Versions
================================================================================
> + dev
* 1.5.3.RELEASE

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


OS X Homebrew安装

如果你是用的是OS X,并且你也正在使用Homebrew,那你唯一要做的就是执行:

$ brew tap pivotal/tap
$ brew install springboot


Homebrew会把
spring
安装到
/usr/local/bin
目录。

如果你看到的和我的不一样,尝试着执行
brew update
,然后再试一次。

MacPorts安装

如果你正在使用Mac,并且你使用MacPorts,你可以这样安装:

$ sudo port install spring-boot-cli


命令行安装

Spring Boot CLI也自带脚本,可以在BASH,zsh下直接执行安装。你可以
source
这个脚本(名字也是
spring
)。

快速开始Spring CLI

创建一个文件app.groovy:

@RestController
class ThisWillActuallyRun {

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

}


只需要执行:

$ spring run app.groovy


如果你是第一次执行,可能会花很多时间来下载依赖。下一次就会快很多了。

打开localhost:8080就可以看到输出:

Hello World!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring-boot