SpringBoot多模塊搭建的實現示例
一、SpringBoot多模塊介紹
Spring Boot是一個用于快速開發(fā)基于Spring框架的應用程序的工具。多模塊開發(fā)是指將一個大型應用程序拆分為多個模塊(或子項目),每個模塊負責不同的功能或業(yè)務邏輯。在Spring Boot中,多模塊開發(fā)可以幫助我們更好地組織代碼、提高代碼的可維護性和可擴展性。
在Spring Boot中,可以使用Maven或Gradle等構建工具來創(chuàng)建多模塊項目。每個模塊可以包含自己的源代碼、配置文件和依賴項,同時也可以有自己的打包方式和部署方式。在多模塊開發(fā)中,通常會有一個父模塊(或者稱為根模塊),用于管理所有子模塊之間的依賴關系和版本控制。
通過多模塊開發(fā),我們可以將不同功能的代碼分離到不同的模塊中,使得代碼更加清晰和易于管理。同時,每個模塊可以獨立進行測試、構建和部署,有利于團隊協(xié)作和項目的持續(xù)集成與部署。
總的來說,Spring Boot多模塊開發(fā)可以幫助我們更好地組織和管理代碼,提高開發(fā)效率和代碼質量,是一個非常實用的開發(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文件進行修改
- 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
這樣模塊就搭建成功了
到此這篇關于SpringBoot多模塊搭建的實現示例的文章就介紹到這了,更多相關SpringBoot多模塊搭建內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!