Spring Boot 之 HelloWorld詳解

摘要: 原創出處:www.bysocket.com 泥瓦匠BYSocket 但願轉載,保留摘要,謝謝!html

「之前是人放狗看家,如今是狗牽着人散步」 — 隨筆java

1、Spring Boot 自述

世界上最好的文檔來源自官方的《Spring Boot Reference Guide》,是這樣介紹的:git

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了~github

2、HelloWorld實戰詳解

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

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

2.1  pom.xml配置

代碼以下:spring

<?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 啓動父依賴便可。  shell

2.2 Controller層

HelloWorldController的代碼以下:apache

/**
 * 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 Documenttomcat

2.3 啓動應用類

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

/**
 * 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以下:

/**
 * 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());
    }
}

 

3、運行

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:

Hello,World!

 

4、小結

  1. Spring Boot pom配置 

  2. Spring Boot 啓動及原理

  3. 對應代碼分享在 Github 主頁


如以上文章或連接對你有幫助的話,別忘了在文章結尾處評論哈~ 你也能夠點擊頁面右邊「分享」懸浮按鈕哦,讓更多的人閱讀這篇文章。

相關文章
相關標籤/搜索