日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Idea 创建简单的SpringBoot 父子项目

發(fā)布時間:2025/3/12 javascript 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Idea 创建简单的SpringBoot 父子项目 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

背景

使用Idea 創(chuàng)建一個模塊化的SpringBoot項目,但是發(fā)現(xiàn)Idea 創(chuàng)建父子項目的方式較Eclipse 較為不同,且Idea 創(chuàng)建的過程較Eclipse創(chuàng)建父子項目的過程復(fù)雜。
Eclipse 創(chuàng)建SpringBoot父子項目傳送門

網(wǎng)上雖然有Idea創(chuàng)建SpringBoot父子項目,但是攜帶各種其他的功能,導(dǎo)致無法簡單的搞懂如何創(chuàng)建。下面就一個例子進行說明一下。

構(gòu)建簡單SpringBoot父子項目

環(huán)境說明:jdk1.8
Idea 版本:2019.3
(1)打開Idea,如果是新安裝的軟件,不會有歷史項目

(2)點擊 “Create New Project”,進入一個新的項目的創(chuàng)建界面。父項目直接創(chuàng)建一個Maven項目即可

PS:不要勾選 “Create from archetype”
點擊下一步。
(3)進入父項目的創(chuàng)建頁面,填寫項目名稱和GoupId與坐標(biāo)

PS:可以在上圖紅圈標(biāo)注的地方修改項目的地址
(4)點擊Finish 按鈕,完成一個父項目的創(chuàng)建。
下圖是點擊Finish 創(chuàng)建好的項目在Idea 軟件中打開的情況:

(5)在現(xiàn)有父項目的基礎(chǔ)上新建SpringBoot子項目
在父項目的名稱的位置右鍵鼠標(biāo):

(6)新建一個Module,進入新建頁面。
子項目直接選擇Spring initializr,創(chuàng)建一個SpringBoot項目

點擊下一步,進入子項目的具體構(gòu)建內(nèi)容。

填寫坐標(biāo)后,點擊下一步。
(7)進入新建項目選擇插件,由于我們測試需要,所以直接簡單的選擇了web

點擊下一步,可以更改項目名稱和地址

可以看出,子項目的地址默認(rèn)在父項目文件夾下。點擊Finish完成子項目的創(chuàng)建。
(8)可以刪除一些子項目不需要的文件,不刪也行

P: 還有父項目的src文件夾,這個已經(jīng)不需要了,所以可以直接刪除

(9)經(jīng)過 1-8 步的操作,我們創(chuàng)建了父子項目結(jié)構(gòu),但是這兩個項目尚未實現(xiàn)父子關(guān)系。下面我們需要更改POM文件,來實現(xiàn)父子關(guān)系。

這里我們先粘貼出還沒有改變關(guān)系之前的父子結(jié)構(gòu)項目原有的POM 文件

  • 父項目:
<?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.codingdemo</groupId><artifactId>springboot-demo</artifactId><version>1.0-SNAPSHOT</version> </project>
  • 子項目
<?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.2.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.codingdemo</groupId><artifactId>springboot-demo-redis</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot-demo-redis</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><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

(10)首先將你認(rèn)為子項目中是公共的部分拿到父項目中,包括spring-boot-starter-parent 和一些 properties 以及build 等公共配置

<!--1 --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent> <!--2 --> <properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties> <!--3--> <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><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>

上面子項目中的配置文件都可以拿到父項目的POM文件中 。
(11)將父項目的坐標(biāo),復(fù)制到 子項目的父坐標(biāo)中

<parent><groupId>com.codingdemo</groupId><artifactId>springboot-demo</artifactId><version>1.0-SNAPSHOT</version></parent>

(12) 父項目 新增 pom
P:這個必須

(13)將子項目的moudle 關(guān)聯(lián)到 父項目中

<modules><module>springboot-demo-redis</module></modules>


(14)我們再來看下新的父子項目的 POM文件

  • 父項目
<?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><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.codingdemo</groupId><artifactId>springboot-demo</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><modules><module>springboot-demo-redis</module></modules><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><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
  • 子項目
<?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>com.codingdemo</groupId><artifactId>springboot-demo</artifactId><version>1.0-SNAPSHOT</version></parent><groupId>com.codingdemo</groupId><artifactId>springboot-demo-redis</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot-demo-redis</name><description>Demo project for Spring Boot</description></project>

讀者可以對比一下,沒有改造之前的POM文件,有什么差距
(15) 我們來驗證一下,父子項目是否成功創(chuàng)建
我們在子項目中新增一個Controller ,進行一下簡單Web訪問,查看是否成功

package com.codingdemo.springbootdemoredis.controller;import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;@RestController public class HelloController {@GetMapping("/hello")public String hello() {return "hello world!";} }

啟動一下SpringBoot 啟動類,輸入網(wǎng)址

可以看到能夠成功訪問,我們已經(jīng)成功搭建好簡單的父子SpringBoot項目
(16) 如果我修改 Controller的方法,需要重啟項目,這樣比較麻煩。下面是如何配置熱部署SpringBoot項目

SpringBoot項目的熱部署

(1)在上面現(xiàn)有的父子SpringBoot項目中,在父項目POM文件增加熱部署的devtools依賴。

<!-- 熱加載 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional><scope>true</scope></dependency><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><!--熱加載必須配上以下內(nèi)容--><configuration><fork>true</fork></configuration></plugin></plugins></build>

(2) 我們可以在配置文件中設(shè)置devtools

### 熱部署配置 spring.devtools.restart.enabled=true spring.devtools.restart.additional-paths=src/main/java spring.devtools.restart.poll-interval=1s spring.freemarker.cache=false

(3)如果修改了Controller的函數(shù)但是還是沒有熱加載,可能是由于你是新的項目需要設(shè)置開啟自動編譯

P:勾上
重啟項目后,然后再次修改文件后,便可以看到開始熱部署了

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的Idea 创建简单的SpringBoot 父子项目的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。