官方文档:https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/
spring-boot 2.0
一、第一部分
1.1 Spring-boot介绍
Spring-boot能够轻松创建独立的、生产级的基于Spring的可运行的应用。它集成了Spring依赖和一些第三方类库,能够使得做最小的工作开始一个应用。大部分的应用只需要少量的配置就可以开始。
使用Spring-boot可以创建直接通过命令java -jar
或者更传统的war部署应用。
主要目标:
1. 为所有Spring开发提供一个更快、更广泛的入门体验。
2. 当需求开始偏离默认值时,要跳出固有的框框,但要迅速退出。
3. 提供一系列非功能特性,这些特性对于大型项目(如嵌入式服务器、安全性、度量标准、健康检查和外部化配置)是常见的。
4. 绝对没有代码生成,也不需要XML配置。
1.2 系统环境要求
Spring Boot 2.1.0.BUILD-SNAPSHOT需要:
- Java8及以上
- Spring 5.0.5.RELEASE及以上
- Maven 3.2+
- Gradle4
1.2.1 Servlet 容器
Spring Boot 支持以下内嵌的容器:
名称 | Servlet版本 |
---|---|
Tomcat8.5 | 3.1 |
Jetty 9.4 | 3.1 |
Undertow 1.4 | 3.1 |
我们可以部署Spring Boot应用到任意3.1+的容器。
1.3 安装Spring Boot
1.3.1 安装
只需要将Spring Boot jar包放到classpath即可,推荐一些管理工具比如Maven、Gradle等。
1.3.1.1 Maven环境下安装
Spring Boot通常需要Pom继承自spring-boot-starter-parent项目,并且依赖一个或者多个Starters。同时提供一个可选的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>2.1.0.BUILD-SNAPSHOT</version>
</parent>
<!-- Add typical dependencies for a web application 典型的web依赖starter -->
<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>https://repo.spring.io/snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>https://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>
注意:继承Pom的方式,可能在有的时候不方便使用,可以参考后面章节,使用import的方式引入。
1.3.1.2 Gradle环境下安装
略
1.3.2 安装Spring Boot CLI
CLI是一个命令行工具,可快速使用的spring原型。
1.3.2.1 手工安装
下载以下
- spring-boot-cli-2.1.0.BUILD-SNAPSHOT-bin.zip
- spring-boot-cli-2.1.0.BUILD-SNAPSHOT-bin.tar.gz
下载后安装INSTALL.txt指导进行安装即可,在bin/目录下有个spring.bat脚本
1.3.2.2 通过SDKMAN进行安装
通过一些命令。(略)
1.3.2.3 Mac Homebrew安装
$ brew tap pivotal/tap
$ brew install springboot
1.3.2.4 Mac MacPorts安装
$ sudo port install spring-boot-cli
1.3.2.5 命令行自动命令完成
快速使用Spring CLI例子
如何测试CLI是否安装成功,首先穿创建文件命名为app.groovy
,如下:
@RestController
class ThisWillActuallyRun {
@RequestMapping("/")
String home() {
"Hello World!"
}
}
然后通过以下命令运行:
$ spring run app.groovy
访问localhost:8080能看到页面输出:HelloWorld
1.4 开发第一个 Spring-Boot应用
首先确认是否安装java和maven并配置环境变量。
1.4.1 创建Pom文件
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
</parent>
<!-- Additional lines to be added here... -->
<!-- (you don't need this if you are using a .RELEASE version) -->
<repositories>
<repository>
<id>spring-snapshots</id>
<url>https://repo.spring.io/snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>https://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>
1.4.2 添加Starter依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
1.4.3 编写代码
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration
public class Example {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Example.class, args);
}
}
1.4.3.1 注解
SpringMvc的注解就不多说了。
@EnableAutoConfiguration注解:
这个注释告诉SpringBoot“猜”你将如何想配置Spring,基于你已经添加jar依赖项。如果spring-boot-starter-web已经添加Tomcat和Spring MVC,这个注释自动将假设您正在开发一个web应用程序并添加相应的spring设置。
1.4.3.2 main方法
应用启动入口
1.4.3.3 运行
$ mvn spring-boot:run
1.4.4 创建可执行的jar
添加插件,打包即可
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>