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(英文中是“引導”的意思),是用來簡化Spring應用的搭建到開發的過程。應用開箱即用,只要通過 “just run”(可能是 java -jar 或 tomcat 或 maven插件run 或 shell腳本),就可以啓動項目。二者,Spring Boot 只要很少的Spring配置文件(例如那些xml,property)。

因爲“習慣優先於配置”的原則,使得Spring Boot在快速開發應用和微服務架構實踐中得到廣泛應用。

 

Javaer裝好JDK環境和Maven工具就可以開始學習Boot了~

二、HelloWorld實戰詳解

首先得有個maven基礎項目,可以直接使用Maven骨架工程生成Maven骨架Web項目,即man archetype:generate命令:

1
mvn archetype:generate -DgroupId=springboot -DartifactId=springboot-helloworld -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

2.1  pom.xml配置

代碼如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?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的代碼如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
 * 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:提供實現了REST API,可以服務JSON,XML或者其他。這裏是以String的形式渲染出結果。

2. @RequestMapping:提供路由信息,”/“路徑的HTTP Request都會被映射到sayHello方法進行處理。

具體參考,世界上最好的文檔來源自官方的《Spring Framework Document

2.3 啓動應用類

和第一段描述一樣,開箱即用。如下面Application類:

1
2
3
4
5
6
7
8
9
10
11
12
/**
 * Spring Boot應用啓動類
 *
 * 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 應用的標識

2. Application很簡單,一個main函數作爲主入口。SpringApplication引導應用,並將Application本身作爲參數傳遞給run方法。具體run方法會啓動嵌入式的Tomcat並初始化Spring環境及其各Spring組件。

 

2.4 Controller層測試類

一個好的程序,不能缺少好的UT。針對HelloWorldController的UT如下:

1
2
3
4
5
6
7
8
9
10
11
12
/**
 * 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。可以在控制檯中看到:

1
2
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:

1
Hello,World!

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章