在IDEA中創(chuàng)建SpringBoot項(xiàng)目的詳細(xì)步驟
開發(fā)環(huán)境
以下是我的開發(fā)環(huán)境
- JDK 1.8
- Maven 3.6.3
- IDEA 2019(2019 無所畏懼,即使現(xiàn)在已經(jīng) 2023 年了哈哈哈)
使用 Maven 的方式創(chuàng)建 Spring Boot 項(xiàng)目
下面的內(nèi)容可能會(huì)因 IDEA 版本不同,而有些選項(xiàng)不同,但是大同小異。
1. 打開 IDEA
點(diǎn)擊 Create New Project
2. 點(diǎn)擊 Maven
點(diǎn)擊左邊的 Maven,默認(rèn)選擇你的 JDK,然后點(diǎn)擊 Next。
3. 輸入項(xiàng)目信息
輸入你的項(xiàng)目名稱、存儲(chǔ)位置、組等信息,搞定后點(diǎn)擊 Finish 完成。
4. 開啟自動(dòng)導(dǎo)入依賴功能
完成 Maven 項(xiàng)目的創(chuàng)建,接著點(diǎn)擊 Enable Auto-Import
,開啟自動(dòng)導(dǎo)入依賴功能。
5. 添加 Spring Boot 所需依賴
打開項(xiàng)目根目錄下的 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.god23bin</groupId> <artifactId>spring-boot-made-by-maven-demo</artifactId> <version>1.0-SNAPSHOT</version> <!-- 引入 Spring Boot 統(tǒng)一版本父項(xiàng)目管理依賴 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> </parent> <dependencies> <!-- Spring Web 依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project> 復(fù)制代碼
這里添加的依賴是 Spring Boot 統(tǒng)一 2.1.3 版本的父項(xiàng)目管理依賴,接著添加了 Spring Web 依賴項(xiàng)。
題外話,回想剛接觸的時(shí)候是 2.1.X 的版本,現(xiàn)在都已經(jīng)出到 Spring Boot 3.0.5 了,不得不說更新迭代是真的快啊。
此處的版本,你可以換成較新的,目前我沒用過 3.0,印象中我只用過 2.1.3,2.2.2,2.3.4,2.5.7
6. 創(chuàng)建配置文件
新建 application.yml
配置文件,當(dāng)然,你喜歡的話可以創(chuàng)建 properties
為后綴的配置文件。
7. 新建一個(gè)啟動(dòng)類
Application:
package cn.god23bin.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author god23bin */ @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 復(fù)制代碼
到這里,就能夠啟動(dòng) Spring Boot 項(xiàng)目了,可以跑啦!
8. 新建一個(gè)測試類
在 pom.xml 文件中引入 Spring Boot Test 依賴,接著新建一個(gè)測試類,用于單元測試。
pom.xml:
<!-- Spring Boot Test 依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> 復(fù)制代碼
ApplicationTest:
package cn.god23bin.demo; import org.junit.Test; import org.springframework.boot.test.context.SpringBootTest; /** * @author god23bin */ @SpringBootTest public class ApplicationTest { @Test public void test() { System.out.println("Spring Boot Test!"); } } 復(fù)制代碼
以上,就是以 Maven 的方式創(chuàng)建 Spring Boot 項(xiàng)目的過程。
Spring Boot 的版本有哪些?
截至本篇文章書寫時(shí),Spring Boot 最新版本為 3.0.5,可自行在 Maven 倉庫中查看:https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent
使用 Spring Initializr 創(chuàng)建 Spring Boot 項(xiàng)目
下面的內(nèi)容可能會(huì)因 IDEA 版本不同,而有些選項(xiàng)不同,但是大同小異。
1. 打開 IDEA
點(diǎn)擊 Create New Project
2. 點(diǎn)擊 Spring Initializr
點(diǎn)擊左邊的 Spring Initializr
,默認(rèn)選擇你的 JDK 和構(gòu)建 Spring Boot 項(xiàng)目的 URL,接著點(diǎn)擊右下角的 Next。其中,如果遇到 Spring 官方的 URL 不可用,那么可以嘗試阿里云提供的腳手架 start.aliyun.com,選擇 Custom,將阿里云的 URL 復(fù)制過去。
3. 輸入項(xiàng)目信息(主要是 Maven 項(xiàng)目的相關(guān)信息)
進(jìn)行相關(guān)的項(xiàng)目元數(shù)據(jù)配置,比如項(xiàng)目組、項(xiàng)目名稱、項(xiàng)目版本等等信息,搞定繼續(xù) Next。
4. 確定版本以及依賴
選擇 Spring Boot 版本,勾選項(xiàng)目需要的依賴項(xiàng),這里目前勾選需要的 Spring Web 依賴,搞定點(diǎn)擊 Next 繼續(xù)。
5. 確定項(xiàng)目信息
再次確認(rèn)需要的項(xiàng)目名稱,然后選擇項(xiàng)目存儲(chǔ)的位置,還可以進(jìn)行更多的設(shè)置,包括模塊名稱,內(nèi)容根目錄,模塊所在位置等信息,一般按默認(rèn)的就可以了,點(diǎn)擊 Finish 完成。如果選擇的項(xiàng)目存儲(chǔ)的目錄不存在,則會(huì)提示你 IDEA 將進(jìn)行創(chuàng)建這個(gè)目錄。
6. 等待依賴包的下載以及同步
等待依賴包的下載以及同步,同步完成,一個(gè) Spring Boot 項(xiàng)目就創(chuàng)建成功了。
7. 刪除不需要的目錄
對(duì)于這個(gè)項(xiàng)目的目錄結(jié)構(gòu),我一般會(huì)刪除 .mvn、HELP.md、mvnw、mvnw.cmd
和 resources 目錄下的 static 和 templates
目錄。
現(xiàn)在,不需要我們自己創(chuàng)建啟動(dòng)類、測試類、配置文件,也可以跑 Spring Boot 項(xiàng)目啦!
最后的最后
以上就是在IDEA中創(chuàng)建SpringBoot項(xiàng)目的詳細(xì)步驟的詳細(xì)內(nèi)容,更多關(guān)于IDEA創(chuàng)建SpringBoot項(xiàng)目的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java組件FileUpload上傳文件實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Java組件FileUpload上傳文件實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06淺談hashmap為什么查詢時(shí)間復(fù)雜度為O(1)
這篇文章主要介紹了hashmap為什么查詢時(shí)間復(fù)雜度為O(1),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08spring?cloud?gateway中配置uri三種方式
gateway?組件是SpringCloud?組件中的網(wǎng)關(guān)組件,主要是解決路由轉(zhuǎn)發(fā)的問題,跟nginx有點(diǎn)類似,區(qū)別是nginx多用在前端上,gateway用在后端上,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-08-08解決shiro 定時(shí)監(jiān)聽器不生效的問題 onExpiration不調(diào)用問題
這篇文章主要介紹了解決shiro 定時(shí)監(jiān)聽器不生效的問題 onExpiration不調(diào)用問題。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07java 實(shí)現(xiàn)數(shù)組擴(kuò)容與縮容案例
這篇文章主要介紹了java 實(shí)現(xiàn)數(shù)組擴(kuò)容與縮容案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02