1. 程式人生 > >SpringBoot 入門教程例項詳解(一) 開發第一個SpringBoot應用程式例項

SpringBoot 入門教程例項詳解(一) 開發第一個SpringBoot應用程式例項

構建你的第一個Spring Boot應用程式

更多精彩請閱讀 東陸之滇的csdn部落格:http://blog.csdn.net/zixiao217
此教程提供一個入門應用程式例子,來展示Spring Boot是如何幫助快速、敏捷開發新一代應用的。你還可以通過 Spring Initializr快速生成一個基於Spring Boot的Maven工程(zip形式,你可以儲存到自己計算機):

這裡寫圖片描述

第一個目標

我們將會構建一個基於Spring Boot的應用程式,併為它新增一些有用的服務。

準備工作

  • 你僅僅需要15分鐘的時間

  • 一個你喜愛的IDE或者文字編輯器

  • 你也可以安裝Spring Tool Suite (STS)

    套件,匯入本示例程式碼,在IDE中直接開啟網頁檢視檢視效果。

pom.xml

引入Spring Boot相關依賴

<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>org.byron4j</groupId> <artifactId>springBoot</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>jar</packaging> <name>springBoot</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding
>
UTF-8</project.build.sourceEncoding> </properties> <!--引入Spring Boot的parent模組--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <dependencies> <!--引入Spring Boot 的web依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--引入Spring Boot 的單元測試依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <!--指定JDK版本1.8--> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>

第一個應用:

這裡寫圖片描述

BaseApplication.java程式碼示例,Spring Boot預設掃描使用@SpringBootApplication註解的子路徑下的被@Controller@Service@Repository@Conponent 等註解類標註的類。
package org.byron4j.springBoot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * <pre>Spring Boot服務啟動入口,使用了@SpringBootApplication註解,
 *  會自動掃描該類子路徑下所有
 *   @Controller、@Service、@Repository、@Conponent 等註解類</pre>
 *
 */
@SpringBootApplication
public class BaseApplication 
{
    public static void main( String[] args ){
        SpringApplication.run(BaseApplication.class, args);
    }

}

第一個Hello Spring Boot!程式:

/**
 * 
 */
package org.byron4j.springBoot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author Byron.Y.Y
 * @date 2016年10月12日
 */
@Controller
public class HelloSpringBootController {

    @RequestMapping("/hello")
    public String hello(){
        return "Hello, SpringBoot!";
    }
}

Spring Boot極大簡化了配置,基於註解,執行BaseApplication#main方法,出現啟動介面:

這裡寫圖片描述

Spring Boot內建了一個Tomcat容器,啟動成功後,在瀏覽器端輸入localhost:8080/hello,看看效果吧。