SpringBoot學習筆記:Getting Started構建第一個Spring Boot工程

本文參考 Spring Boot官方文檔 Part II. Getting Started部分
特定版本如1.5.10.RELEASE版本官方文檔地址:
https://docs.spring.io/spring...
注:本文基於Spring Boot 1.5.10.RELEASE構建html

The Spring Boot repository has also a bunch of samples you can run.(話說spring-boot在github上已經有23000多顆星了,足見spring-boot火爆程度)java

Spring Boot簡介

如下介紹引自 https://projects.spring.io/sp...git

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.github

Featuresweb

  • Create stand-alone Spring applications
    建立獨立的Spring應用程序
  • Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
    直接嵌入Tomcat,Jetty或Undertow(無需部署WAR文件)
  • Provide opinionated 'starter' POMs to simplify your Maven
    configuration
    提供本身的'入門'POM來簡化你的Maven配置
  • Automatically configure Spring whenever possible
    儘量自動配置Spring
  • Provide production-ready features such as metrics, health checks and
    externalized configuration
    提供生產就緒功能,如指標,運行情況檢查和外部配置
  • Absolutely no code generation and no requirement for XML
    configuration
    絕對不會生成代碼,而且不須要XML配置

系統要求

Spring Boot 1.5.10.RELEASE版本須要redis

Java 7 and Spring Framework 4.3.14.RELEASE or above 最好java8及以上
構建工具:Maven (3.2+), and Gradle 2 (2.9 or later) and 3.spring

clipboard.png

Servlet容器:apache

clipboard.png

構建工程

這裏筆者採用構建Empty Project做爲Spring Boot學習過程的總工程,以添加Module的方式構建不一樣學習過程如springboot-first-app、springboot-redis等瀏覽器

步驟演示

在Idea歡迎頁點擊Create New Project或File --> New Project,彈出如下界面並選擇Empty Projectspringboot

clipboard.png

點擊Next,填寫Project name如SpringBootLearning

clipboard.png

點擊Finish完成建立,進入工程Idea會彈出Project Structure窗口,能夠點擊綠色的+按鈕來New Module

clipboard.png

也能夠經過File --> New --> Module

使用Maven構建第一個Spring Boot應用

以上構建空工程的步驟只是筆者爲了自身的系統學習,讀者能夠直接建立一個Spring Boot工程
方法一:在 Spring Boot官方Initializer頁面 在線構建工程再導入到Ide中

clipboard.png

方法二:直接在Idea中Create New Project --> Spring Initializr --> 填寫group、artifact -->鉤上web --> 點下一步就好了,具體步驟參照筆者New Module步驟便可

clipboard.png

clipboard.png

clipboard.png

clipboard.png

工程目錄結構

clipboard.png

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>cn.fulgens.springbootlearning</groupId>
    <artifactId>springboot-first-app-maven</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-first-app-maven</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

編寫HelloController

package cn.fulgens.springbootlearning.springbootfirstappmaven.web;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping("/")
    public String hello() {
        return "Hello Spring Boot!";
    }

}

啓動第一個Spring Boot應用

方法一:直接運行SpringbootFirstAppMavenApplication中的main方法

package cn.fulgens.springbootlearning.springbootfirstappmaven;

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

@SpringBootApplication
public class SpringbootFirstAppMavenApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootFirstAppMavenApplication.class, args);
    }
}

啓動後在瀏覽器輸入localhost:8080

clipboard.png

方法二:maven命令啓動
cd到項目主目錄後執行(其中-Dtest.skip=true表示跳過單元測試)

mvn clean package 
mvn spring-boot:run -Dtest.skip=true

若是是Gradle構建可執行
gradle build
gradle bootRun

方法三:以java -jar的方式啓動
maven打包完成後cd到target目錄下執行,jar包名稱視本身狀況而定

java -jar springboot-first-app-maven-0.0.1-SNAPSHOT.jar

單元測試

package cn.fulgens.springbootlearning.springbootfirstappmaven;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpMethod;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class HelloControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void helloControllerTest() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.request(HttpMethod.GET, "/"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().string("Hello Spring Boot!"));
    }

}

spring boot 1.4.0 版本以前使用如下三個註解,參考Spring Boot 系列(二)單元測試&網絡請求@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes = DemoApplication.class) //在spring boot 1.4.0 版本以後取消了 //classes須要指定spring boot 的啓動類如:DemoApplication.class 否則WebApplicationContext不被實例化@WebAppConfiguration

相關文章
相關標籤/搜索