1. 程式人生 > >Spring Boot實踐教程(一):Hello,world!

Spring Boot實踐教程(一):Hello,world!

dap con result ret guid gui dea mode 結構

??本篇會通過完成一個常用的Helloworld示例,來開始我們的Spring Boot旅程

準備工作

15分鐘

開發環境

項目創建

??項目創建的時候我們可以選擇通過自定義配置Maven文件來創建,也可以用Spring官方提供的Spring Initializr工具,Spring Initializr是一個可以簡化Spring Boot項目構建的工具,有兩種使用Spring Initializr的方法:

  • 官方提供的web頁面工具,地址

  • 開發工具集成的方式

??兩種方式操作的步驟是一樣的,只不過通過Web頁面工具多了一步將生成結果導入到開發工具的過程,這裏我們采用的第二種:開發工具集成的Spring Initializr。

??在Idea中New Project >選擇Spring Initializ即出現如下對話框:

技術分享圖片

??填寫對應的Group、Artifact,選擇Type為Maven Project,並填寫其他相關信息(如上圖所示)後點擊下一步,在這個步驟中可以選擇對應的功能,選擇後Spring Boot就會為項目添加對應的依賴等,這裏我們只添加一個Web的基礎功能。進一步填寫項目路徑後即可完成一個Web項目的創建。

??看一下項目創建完成之後的結構:

spring-boot-1-helloworld
    │  pom.xml    
    ├─src
    │  ├─main
    │  │  ├─java
    │  │  │  └─com
    │  │  │      └─practice
    │  │  │          └─springboot
    │  │  │              └─hello
    │  │  │                  │  SpringBoot1HelloworldApplication.java
    │  │  │                          
    │  │  └─resources
    │  │      │  application.properties
    │  │      ├─static
    │  │      └─templates
    │  └─test
    │      └─java
    │          └─com
    │              └─practice
    │                  └─springboot
    │                      └─hello
    │                              SpringBoot1HelloworldApplicationTests.java

項目運行

??我們可以通過運行main方法的方式將項目啟動起來,我們先創建一個Controller來看更直觀的看一下啟動之後的效果,在web包(需要手動創建)中創建HelloController:

package com.practice.springboot.hello.web;

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

@RestController
public class HelloController {

    @RequestMapping
("/") public String hi(){ return "Congratulations! It works!"; } }

??運行Springboot1HelloApplication.java中的main方法,我們會在控制臺中看到一個Spring的標誌和Spring Boot版本的說明。啟動完成之後在瀏覽器中訪問http://localhost:8080/ ,會看到下圖。至此我們花了幾分鐘,就創建了一個web項目並將其運行起來了。在這個過程中我們沒有添加任何jar包的依賴,沒有配置Web.xml,沒有配置Spring和SpringMVC的環境,甚至都沒有將項目部署只Tomcat,因為這一切Spring Boot都為我們完成了。

技術分享圖片

項目分析

??首先我們來看一下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>com.practice.springboot</groupId>
    <artifactId>hello</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot-1-helloworld</name>
    <description>spring-boot-1-helloworld</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.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>

??在pom文件中可以看到以下信息:

  1. 整個項目繼承於spring-boot-starter-parent
  2. 項目有兩個依賴包spring-boot-starter-web,spring-boot-starter-test

??有了以上兩個配置,Spring Boot就為我們配置了一個基礎的web項目的環境,包括依賴包和一個嵌入的tomcat環境。後續的文章裏面我們會分析一下這裏的starter。

項目打包

??在項目路徑下運行maven命令mvn package,跑完之後就會在項目的target目錄下得到一個jar文件,這就是項目包。跟以前的war包不太一樣的是,這裏的項目包格式是jar包。

??可以在通過java -jar 文件名.jar 來運行這個jar文件,然後在瀏覽器中訪問http://localhost:8080/ ,可以看到結果與之前在開發環境下是一樣的。

總結

??通過以上的步驟,完成了一個初步的最基礎的Spring Boot的項目的創建和運行,後續我會通過實踐的方式來分析一下Spring Boot的一些常用功能和特性,並按照我自己的理解來分析一下Spring Boot的運行原理。

??參考:
??Spring Boot Reference Guide

歡迎關註我的微信公眾號:

技術分享圖片

Spring Boot實踐教程(一):Hello,world!