SpringBoot多模塊搭建的實現(xiàn)示例
一、SpringBoot多模塊介紹
Spring Boot是一個用于快速開發(fā)基于Spring框架的應(yīng)用程序的工具。多模塊開發(fā)是指將一個大型應(yīng)用程序拆分為多個模塊(或子項目),每個模塊負(fù)責(zé)不同的功能或業(yè)務(wù)邏輯。在Spring Boot中,多模塊開發(fā)可以幫助我們更好地組織代碼、提高代碼的可維護(hù)性和可擴(kuò)展性。
在Spring Boot中,可以使用Maven或Gradle等構(gòu)建工具來創(chuàng)建多模塊項目。每個模塊可以包含自己的源代碼、配置文件和依賴項,同時也可以有自己的打包方式和部署方式。在多模塊開發(fā)中,通常會有一個父模塊(或者稱為根模塊),用于管理所有子模塊之間的依賴關(guān)系和版本控制。
通過多模塊開發(fā),我們可以將不同功能的代碼分離到不同的模塊中,使得代碼更加清晰和易于管理。同時,每個模塊可以獨立進(jìn)行測試、構(gòu)建和部署,有利于團(tuán)隊協(xié)作和項目的持續(xù)集成與部署。
總的來說,Spring Boot多模塊開發(fā)可以幫助我們更好地組織和管理代碼,提高開發(fā)效率和代碼質(zhì)量,是一個非常實用的開發(fā)方式。
二、搭建多模塊
我們按照功能搭建多模塊
1. 我們先建立父模塊

這里我們什么都不用選直接點擊create

2、創(chuàng)建子模塊
父項目名稱->右鍵->new->moudle

依次創(chuàng)建provide1、provide2、common、server,一樣不用選如何依賴

將common、provide1、provide2的啟動類和框出來的這些都刪除

在父模塊的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>demo</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.6.13</spring-boot.version>
</properties>
<!---父模塊的打包方式-->
<packaging>pom</packaging>
<!--將子模塊放在一堆-->
<modules>
<module>provide1</module>
<module>provide2</module>
<module>server</module>
<module>common</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>server</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>provide1</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>provide2</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
在每一個子模塊的pom文件進(jìn)行修改
- common模塊里面放入通用的依賴
<?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>
<artifactId>demo</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>common</artifactId>
<description>通用模塊</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
- server模塊
<?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>
<artifactId>demo</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>server</artifactId>
<description>啟動模塊</description>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>provide1</artifactId>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>provide2</artifactId>
</dependency>
</dependencies>
</project>
- provide1模塊
<?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>
<artifactId>demo</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>provide1</artifactId>
<description>provide1模塊</description>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
- provide2模塊
<?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>
<artifactId>demo</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>provide2</artifactId>
<description>provide2模塊</description>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
在provide1里面創(chuàng)建一個resource再創(chuàng)建一個application.yml寫一個端口號
server: port: 8081

這樣模塊就搭建成功了
到此這篇關(guān)于SpringBoot多模塊搭建的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)SpringBoot多模塊搭建內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java的JDBC編程使用之連接Mysql數(shù)據(jù)庫
這篇文章主要給大家介紹了關(guān)于Java的JDBC編程使用之連接Mysql數(shù)據(jù)庫的相關(guān)資料,JDBC是一種用于執(zhí)行SQL語句的Java?API,可以為多種關(guān)系數(shù)據(jù)庫提供統(tǒng)一訪問,需要的朋友可以參考下2023-12-12
詳解Java8函數(shù)式編程之收集器的應(yīng)用
這篇文章主要介紹了詳解Java8函數(shù)式編程之收集器的應(yīng)用,收集器是一種通用的、從流生成復(fù)雜值的結(jié)構(gòu)。可以使用它從流中生成List、Set、Map等集合,需要的朋友可以參考下2023-04-04
詳解在Spring?Boot中使用數(shù)據(jù)庫事務(wù)
本篇文章主要介紹了詳解在Spring?Boot中使用數(shù)據(jù)庫事務(wù),具有一定的參考價值,感興趣的小伙伴們可以參考一下<BR>2017-05-05

