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

這里我們什么都不用選直接點(diǎn)擊create

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

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

將common、provide1、provide2的啟動(dòng)類(lèi)和框出來(lái)的這些都刪除

在父模塊的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>
在每一個(gè)子模塊的pom文件進(jìn)行修改
- common模塊里面放入通用的依賴(lài)
<?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>啟動(dòng)模塊</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)建一個(gè)resource再創(chuàng)建一個(gè)application.yml寫(xiě)一個(gè)端口號(hào)
server: port: 8081

這樣模塊就搭建成功了
到此這篇關(guān)于SpringBoot多模塊搭建的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)SpringBoot多模塊搭建內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring boot AOP通過(guò)XML配置文件聲明的方法
這篇文章主要介紹了Spring boot AOP通過(guò)XML配置文件聲明,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Java的JDBC編程使用之連接Mysql數(shù)據(jù)庫(kù)
這篇文章主要給大家介紹了關(guān)于Java的JDBC編程使用之連接Mysql數(shù)據(jù)庫(kù)的相關(guān)資料,JDBC是一種用于執(zhí)行SQL語(yǔ)句的Java?API,可以為多種關(guān)系數(shù)據(jù)庫(kù)提供統(tǒng)一訪問(wèn),需要的朋友可以參考下2023-12-12
詳解Java8函數(shù)式編程之收集器的應(yīng)用
這篇文章主要介紹了詳解Java8函數(shù)式編程之收集器的應(yīng)用,收集器是一種通用的、從流生成復(fù)雜值的結(jié)構(gòu)??梢允褂盟鼜牧髦猩蒐ist、Set、Map等集合,需要的朋友可以參考下2023-04-04
詳解在Spring?Boot中使用數(shù)據(jù)庫(kù)事務(wù)
本篇文章主要介紹了詳解在Spring?Boot中使用數(shù)據(jù)庫(kù)事務(wù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下<BR>2017-05-05
java實(shí)現(xiàn)簡(jiǎn)單的webservice方式
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單的webservice方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
Java基本概念監(jiān)視器實(shí)習(xí)原理解析
這篇文章主要介紹了Java基本概念監(jiān)視器實(shí)習(xí)原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08

