Spring Boot 之 HelloWorld詳解
一、Spring Boot 自述
世界上***的文檔來源自官方的《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)”的意思),是用來簡化Spring應(yīng)用的搭建到開發(fā)的過程。應(yīng)用開箱即用,只要通過 “just run”(可能是 java -jar 或 tomcat 或 maven插件run 或 shell腳本),就可以啟動項目。二者,Spring Boot 只要很少的Spring配置文件(例如那些xml,property)。
因為“習(xí)慣優(yōu)先于配置”的原則,使得Spring Boot在快速開發(fā)應(yīng)用和微服務(wù)架構(gòu)實踐中得到廣泛應(yīng)用。
Javaer裝好JDK環(huán)境和Maven工具就可以開始學(xué)習(xí)Boot了~
二、HelloWorld實戰(zhàn)詳解
首先得有個maven基礎(chǔ)項目,可以直接使用Maven骨架工程生成Maven骨架Web項目,即man archetype:generate命令:
- mvn archetype:generate -DgroupId=springboot -DartifactId=springboot-helloworld -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
2.1 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>springboot</groupId>
- <artifactId>springboot-helloworld</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>springboot-helloworld :: HelloWorld Demo</name>
- <!-- Spring Boot 啟動父依賴 -->
- <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>
只要加入一個 Spring Boot 啟動父依賴即可。
2.2 Controller層
HelloWorldController的代碼如下:
- /**
- * Spring Boot HelloWorld案例
- *
- * Created by bysocket on 16/4/26.
- */
- @RestController
- public class HelloWorldController {
- @RequestMapping("/")
- public String sayHello() {
- return "Hello,World!";
- }
- }
@RestController和@RequestMapping注解是來自SpringMVC的注解,它們不是SpringBoot的特定部分。
1. @RestController:提供實現(xiàn)了REST API,可以服務(wù)JSON,XML或者其他。這里是以String的形式渲染出結(jié)果。
2. @RequestMapping:提供路由信息,”/“路徑的HTTP Request都會被映射到sayHello方法進行處理。
具體參考,世界上***的文檔來源自官方的《Spring Framework Document》
2.3 啟動應(yīng)用類
和***段描述一樣,開箱即用。如下面Application類:
- /**
- * Spring Boot應(yī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)識
2. Application很簡單,一個main函數(shù)作為主入口。SpringApplication引導(dǎo)應(yīng)用,并將Application本身作為參數(shù)傳遞給run方法。具體run方法會啟動嵌入式的Tomcat并初始化Spring環(huán)境及其各Spring組件。
2.4 Controller層測試類
一個好的程序,不能缺少好的UT。針對HelloWorldController的UT如下:
- /**
- * Spring Boot HelloWorldController 測試 - {@link HelloWorldController}
- *
- * Created by bysocket on 16/4/26.
- */
- public class HelloWorldControllerTest {
- @Test
- public void testSayHello() {
- assertEquals("Hello,World!",new HelloWorldController().sayHello());
- }
- }
三、運行
Just Run的宗旨,運行很簡單,直接右鍵Run運行Application類。同樣你也可以Debug Run??梢栽诳刂婆_中看到:
- Tomcat started on port(s): 8080 (http)
- Started Application in 5.986 seconds (JVM running for 7.398)
然后訪問 http://localhost:8080/ ,即可在頁面中看到Spring Boot對你 say hello:
1Hello,World!
四、小結(jié)
1. Spring Boot pom配置
2. Spring Boot 啟動及原理
【本文為51CTO專欄作者“李強強”的原創(chuàng)稿件,轉(zhuǎn)載請通過51CTO聯(lián)系作者獲取授權(quán)】