摘要: 原創(chuàng)出處:www.bysocket.com 泥瓦匠BYSocket 希望轉(zhuǎn)載,保留摘要,謝謝!
“以前是人放狗看家,現(xiàn)在是狗牽著人散步” — 隨筆
Spring Boot 系列文章:《Spring Boot 那些事》
世界上最好的文檔來(lái)源自官方的《Spring Boot Reference Guide》,是這樣介紹的:
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”…Most Spring Boot applications need very little Spring configuration.
Spring Boot(英文中是“引導(dǎo)”的意思),是用來(lái)簡(jiǎn)化Spring應(yīng)用的搭建到開(kāi)發(fā)的過(guò)程。應(yīng)用開(kāi)箱即用,只要通過(guò) “just run”(可能是 java -jar 或 tomcat 或 maven插件run 或 shell腳本),就可以啟動(dòng)項(xiàng)目。二者,Spring Boot 只要很少的Spring配置文件(例如那些xml,property)。
因?yàn)椤傲?xí)慣優(yōu)先于配置”的原則,使得Spring Boot在快速開(kāi)發(fā)應(yīng)用和微服務(wù)架構(gòu)實(shí)踐中得到廣泛應(yīng)用。
Javaer裝好JDK環(huán)境和Maven工具就可以開(kāi)始學(xué)習(xí)Boot了~
首先得有個(gè)maven基礎(chǔ)項(xiàng)目,可以直接使用Maven骨架工程生成Maven骨架Web項(xiàng)目,即man archetype:generate命令:
mvn archetype:generate -DgroupId=springboot -DartifactId=springboot-helloworld -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
代碼如下:
<?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>springboot</groupId>
<artifactId>springboot-helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-helloworld :: HelloWorld Demo</name>
<!-- Spring Boot 啟動(dòng)父依賴 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<!-- Spring Boot web依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
只要加入一個(gè) Spring Boot 啟動(dòng)父依賴即可。
HelloWorldController的代碼如下:
/**
* Spring Boot HelloWorld案例
*
* Created by bysocket on 16/4/26.
*/
@RestController
public class HelloWorldController {
@RequestMapping("/")
public String sayHello() {
return "Hello,World!";
}
}
@RestController和@RequestMapping注解是來(lái)自SpringMVC的注解,它們不是SpringBoot的特定部分。
1. @RestController:提供實(shí)現(xiàn)了REST API,可以服務(wù)JSON,XML或者其他。這里是以String的形式渲染出結(jié)果。
2. @RequestMapping:提供路由信息,”/“路徑的HTTP Request都會(huì)被映射到sayHello方法進(jìn)行處理。
具體參考,世界上最好的文檔來(lái)源自官方的《Spring Framework Document》
和第一段描述一樣,開(kāi)箱即用。如下面Application類:
/**
* Spring Boot應(yīng)用啟動(dòng)類
*
* Created by bysocket on 16/4/26.
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
1. @SpringBootApplication:Spring Boot 應(yīng)用的標(biāo)識(shí)
2. Application很簡(jiǎn)單,一個(gè)main函數(shù)作為主入口。SpringApplication引導(dǎo)應(yīng)用,并將Application本身作為參數(shù)傳遞給run方法。具體run方法會(huì)啟動(dòng)嵌入式的Tomcat并初始化Spring環(huán)境及其各Spring組件。
一個(gè)好的程序,不能缺少好的UT。針對(duì)HelloWorldController的UT如下:
/**
* Spring Boot HelloWorldController 測(cè)試 - {@link HelloWorldController}
*
* Created by bysocket on 16/4/26.
*/
public class HelloWorldControllerTest {
@Test
public void testSayHello() {
assertEquals("Hello,World!",new HelloWorldController().sayHello());
}
}
Just Run的宗旨,運(yùn)行很簡(jiǎn)單,直接右鍵Run運(yùn)行Application類。同樣你也可以Debug Run。可以在控制臺(tái)中看到:
Tomcat started on port(s): 8080 (http)
Started Application in 5.986 seconds (JVM running for 7.398)
然后訪問(wèn) http://localhost:8080/ ,即可在頁(yè)面中看到Spring Boot對(duì)你 say hello:
Hello,World!
1. Spring Boot pom配置
2. Spring Boot 啟動(dòng)及原理
3. 對(duì)應(yīng)代碼分享在 Github 主頁(yè)
如以上文章或鏈接對(duì)你有幫助的話,別忘了在文章結(jié)尾處評(píng)論哈~ 你也可以點(diǎn)擊頁(yè)面右邊“分享”懸浮按鈕哦,讓更多的人閱讀這篇文章。
更多建議: