Spring基礎:快速入門spring boot(7):spring boot 2.0簡單介紹
阿新 • • 發佈:2018-11-01
從這篇文章開始以spring boot2為主要版本進行使用介紹。
Spring boot 2特性
spring boot2在如下的部分有所變化和增強,相關特性在後續逐步展開。
特性增強
- 基礎元件升級:
JDK1.8+
tomcat 8+
Thymeleaf 3
Hibernate 5.2 - spring framework 5
Reactive Spring
Functional API
Kotlin支援 - Metrics
- Security
使用變化
- 配置屬性變化
- Gradle外掛
- Actuator endpoints
依賴條件
以當前GA版本的spring boot 2.0.6為例,其依賴條件如下:
- Maven: 3.2+
- Gradle:4.x
Servlet容器
支援的servlet容器資訊如下:
- Tomcat 8.5: servlet3.1
- Jetty 9.4: servlet 3.1
- Undertow 1.4: servlet 3.1
pom.xml
按照如下方式設定pom檔案
座標項 | 設定值 |
---|---|
groupId | com.liumiaocn |
artifactId | springbootdemo |
version | 0.0.1-SNAPSHOT |
packaging | 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.liumiaocn</groupId> <artifactId>springbootdemo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springbootdemo</name> <description>spring boot demo project</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
SpringbootdemoApplication
liumiaocn:springbootdemo liumiao$ cat src/main/java/com/liumiaocn/springbootdemo/SpringbootdemoApplication.java
package com.liumiaocn.springbootdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
@SpringBootApplication
public class SpringbootdemoApplication {
@RequestMapping("/")
String home() {
return "Hello, Spring Boot 2";
}
public static void main(String[] args) {
SpringApplication.run(SpringbootdemoApplication.class, args);
}
}
liumiaocn:springbootdemo liumiao$
- RestController和RequestMapping註解都是Spring MVC的註解,用於快速設定路由跳轉資訊
- SpringBootApplication註解用於入口類,也是保證junit測試能夠進行的條件之一。
SPRING INITIALIZR
也可以使用SPRING INITIALIZR快速建立spring boot專案,因為以前的文章中已經解釋過,此處不再贅述。
編譯&構建&執行
編譯&構建
編譯命令:mvn install
執行
執行命令:java -jar target/springbootdemo-0.0.1-SNAPSHOT.jar
- 或者使用mvn命令:mvn spring-boot:run
結果確認
liumiaocn:springbootdemo liumiao$ curl http://localhost:8080
Hello, Spring Boot 2liumiaocn:springbootdemo liumiao$