二:動手實操SpringBoot-使用Spring Initializr創建項目

使用 Spring Initializr 初始化 Spring Boot 項目

Spring Initializr 從本質上說就是一個Web應用程序,它能為你構建Spring Boot項目結構。

雖然不能生成應用程序代碼,但它能為你提供一個基本的項目結構,以及一個用於構件代碼的Maven或者Gradle構建說明文件。

Spring Initializr 的幾種用法

  1. 通過Web界面使用

  2. 通過Spring Tool Suite使用

  3. 通過IntelliJ IDEA使用

  4. 通過Spring Boot CLI使用

使用Web方式

要使用Spring Initializr,最直接的辦法就是使用瀏覽器打開 //start.spring.io,應該能看到一個類似於下圖的一個表單。

配置

其中的內容,是需要我們選擇的配置項,根據個人需求選擇。

Project: 選擇是使用Maven還是Gradle來創建項目。默認Maven,本次選擇Maven。

Language: 選擇使用的開發語言。默認Java,本次選擇Java。

Spring Boot: 選擇Spring Boot版本,默認最新版本(非里程碑和快照版本),也可以自由選擇其他版本,本次選擇最新的2.7.1.

Project Metadata: 指定項目的一些基本信息。最起碼得提供項目的Group和Artfact 。點擊Options展開,也可以配置一些額外的信息:

項目名稱(name):本次為springboot_demo2.

項目描述(Description):本次默認

包名(Package Name):本次為com.tumbler.demo1

打包方式(Packaging):本次為jar方式

Java JDK版本號(Java):本次為1.8

當然這些額外信息都是後期可以修改的,Spring Boot的一大優勢就是內嵌了Servlet容器,打成jar包後直接可以運行,所以建議打成jar包,當然開發者可以根據自己的需求打成war包。

Dependencies: 選擇需要的依賴,輸入關鍵字就有相應提示,我們選擇需要的依賴即可,它會在創建項目時自動在生成的pom.xml(Maven)或者build.gradle(Gradle)引入依賴,當然也可以後期配置。本次選擇web依賴即可。

導入IDEA

填好表單,選好依賴後,可以點擊下方Generate the project 按鈕或者使用快捷鍵Ctrl+Enter,Spring Initializr就會生成一個項目,瀏覽器將會以zip文件的形式(文件名取決於Artifact字段的內容)把這個項目下載下來。

解壓後通過IDEA或者Eclipse將項目導入,導入後我們可以看到項目的基本目錄結構如下

 項目里基本沒有代碼,除了幾個空目錄外,還包含了如下幾樣東西:

  • Maven構建說明:pom.xml

裏面定義了Spring Boot版本,groupId、artifactId、項目描述、jdk版本以及web起步依賴

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="//maven.apache.org/POM/4.0.0" xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="//maven.apache.org/POM/4.0.0 //maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.he</groupId>
    <artifactId>springboot_demo2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot_demo2</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <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>
  • SpringbootDemo2Application.java 一個帶有main方法的類,用於引導啟動應用程序。
package cn.he.springboot_demo2;

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

@SpringBootApplication
public class SpringbootDemo2Application {

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

}
  • SpringbootDemo2ApplicationTests.java 一個空的JUnit測試類,它加載了一個使用Spring Boot自動配置功能的Spring應用程序上下文。
package cn.he.springboot_demo2;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringbootDemo2ApplicationTests {

    @Test
    void contextLoads() {
    }

}
  • application.properties 一個空的properties配置文件,可以根據需求添加配置屬性。

還有幾個空目錄:static目錄存放的是web應用程序的靜態內容(JavaScript、樣式表、圖片等等),templates目錄用於存放呈現模型數據的模板。

這樣通過Spring Initializr 的Web方式已經成功創建一個Spring Boot的項目,我們寫一個Controller進行測試,controller包一定要在啟動類所在包的子包。

package cn.he.springboot_demo2.Controller;

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

/**
 * @description: Spring Boot 測試 Controller
 * @author: 小小赫下士
 * @createDate: 2022/6/24
 */
@RestController
public class HelloController {

    @GetMapping("hello.action")
    public String hello(){
        return "Hello Spring Boot";
    }
}

啟動項目

控制台可以看到使用內置容器Tomcat,端口號為8080。

啟動成功,在瀏覽器輸入://localhost:8080/hello.action

可以看到成功返回了Hello Spring Boot:

IDEA 上使用Spring Initializr

IDEA上是自帶 Spring Initializr 集成的,只需要在新建項目是選擇 Spring Initializr。

其他配置方式也是一樣的

也可以手動選擇依賴

新建好的效果和在Spring Initializr 網站生成的是一樣的,這樣操作更省事。

遇到的一些問題

1、啟動時報錯:無效的源發行版

需要在File – Project Structure – Module 里修改 Language level 為 8 :

2、啟動時報錯:無效目標發行版

需要在 File – Settings – Build,Exec….. – Compiler – Java Compiler 中把Target bytecode version 改為8