使用 Spring Initializr 初始化 Spring Boot 專案
Spring Initializr 從本質上說就是一個Web應用程式,它能為你構建Spring Boot專案結構,
雖然不能生成應用程式代碼,但它能為你提供一個基本的專案結構,以及一個用于構件代碼的Maven或者Gradle構建說明檔案,
Spring Initializr 的幾種用法
-
通過Web界面使用
-
通過Spring Tool Suite使用
-
通過IntelliJ IDEA使用
-
通過Spring Boot CLI使用
使用Web方式
要使用Spring Initializr,最直接的辦法就是使用瀏覽器打開 https://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="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 https://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,

啟動成功,在瀏覽器輸入:http://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

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/495574.html
標籤:其他
上一篇:Spring-常用注解及作用
下一篇:Java基礎-JVM篇
