114 lines
3.1 KiB
Markdown
114 lines
3.1 KiB
Markdown
Spring Boot初级教程
|
||
=======================
|
||
|
||
### 一. Spring Boot安装
|
||
环境要求:Java 8,Maven 3.2或Gradle 1.12
|
||
|
||
#### 1.Maven方式
|
||
<pre>
|
||
<?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.2.1.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>
|
||
</pre>
|
||
注:Spring Boot依赖的groupId是org.springframework.boot,一般Maven pom需要继承spring-boot-starter-parent,然后声明相应[Starter POMs](http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-starter-poms).如果不想继承spring-boot-starter-parent,可以使用import作用域。
|
||
|
||
#### 2.Gradle方式
|
||
|
||
build.gradle脚本:
|
||
<pre>
|
||
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:1.2.2.BUILD-SNAPSHOT")
|
||
}
|
||
}
|
||
|
||
apply plugin: 'java'
|
||
apply plugin: 'spring-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")
|
||
}
|
||
</pre>
|
||
|
||
#### 3.安装Spring Boot命令行工具
|
||
|
||
<pre>
|
||
$ gvm install springboot
|
||
$ spring --version
|
||
$ gvm ls springboot
|
||
$ . ~/.gvm/springboot/current/shell-completion/bash/spring
|
||
$ spring <HIT TAB HERE>
|
||
grab help jar run test version
|
||
</pre>
|
||
|
||
#### 4.Quick Start
|
||
|
||
创建app.groovy,代码如下:
|
||
<pre>
|
||
@RestController
|
||
class ThisWillActuallyRun {
|
||
@RequestMapping("/")
|
||
String home() {
|
||
"Hello World!"
|
||
}
|
||
}
|
||
</pre>
|
||
运行:
|
||
<pre>
|
||
$ spring run app.groovy
|
||
</pre>
|
||
|
||
|
||
|
||
|