1. 程式人生 > >SpringBoot 多模組專案實踐(附打包方法)

SpringBoot 多模組專案實踐(附打包方法)

 

作者:yizhiwazi
連結:https://www.jianshu.com/p/59ceea4f029d
本專案傳送門: https://github.com/yizhiwazi/springboot-socks/tree/master/springboot-integration

序言:

比起傳統複雜的單體工程,使用Maven的多模組配置,可以幫助專案劃分模組,鼓勵重用,防止POM變得過於龐大,方便某個模組的構建,而不用每次都構建整個專案,並且使得針對某個模組的特殊控制更為方便。接下來,本文將重點闡述SpringBoot在Maven環境的多模組構建過程。

本專案傳送門: https://github.com/yizhiwazi/springboot-socks/tree/master/springboot-integration

一、建立聚合父工程

  • 1.首先使用 Spring Initializr 來快速建立好一個Maven工程。然後刪除無關的檔案,只需保留pom.xml 檔案。

SpringBoot 多模組專案實踐(附打包方法)

 

聚合父工程

  1. 然後在 pom.xml 裡面宣告該父工程包含的子模組。(其它資訊就不逐一講述了,諸如繼承SpringBoot官方父工程以及統一依賴管理 請檢視下面的註釋說明)
<?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">
 <!-- 基本資訊 -->
 <description>SpringBoot 多模組構建示例</description>
 <modelVersion>4.0.0</modelVersion>
 <name>springboot-integration</name>
 <packaging>pom</packaging>
 <!-- 專案說明:這裡作為聚合工程的父工程 -->
 <groupId>com.hehe</groupId>
 <artifactId>springboot-integration</artifactId>
 <version>1.0.0.RELEASE</version>
 <!-- 繼承說明:這裡繼承SpringBoot提供的父工程 -->
 <parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>1.5.7.RELEASE</version>
 <relativePath/>
 </parent>
 <!-- 模組說明:這裡宣告多個子模組 -->
 <modules>
 <module>mm-web</module>
 <module>mm-service</module>
 <module>mm-repo</module>
 <module>mm-entity</module>
 </modules>
 <!-- 版本說明:這裡統一管理依賴的版本號 -->
 <dependencyManagement>
 <dependencies>
 <dependency>
 <groupId>com.hehe</groupId>
 <artifactId>mm-web</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 </dependency>
 <dependency>
 <groupId>com.hehe</groupId>
 <artifactId>mm-service</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 </dependency>
 <dependency>
 <groupId>com.hehe</groupId>
 <artifactId>mm-repo</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 </dependency>
 <dependency>
 <groupId>com.hehe</groupId>
 <artifactId>mm-entity</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 </dependency>
 <dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>5.1.42</version>
 </dependency>
 </dependencies>
 </dependencyManagement>
</project>
二、建立子模組(module)

注:這裡是使用IDEA來建立子模組,使用Eclipse的小夥伴可通過 Spring Initializr 構建,然後複製去進去父工程根目錄即可。

  • 1.對著父工程右鍵 - New - Module - > 輸入 mm-web
  • 2.對著父工程右鍵 - New - Module - > 輸入 mm-service
  • 3.對著父工程右鍵 - New - Module - > 輸入 mm-repo
  • 4.對著父工程右鍵 - New - Module - > 輸入 mm-entity
  • 1~4 步驟完成後,分別調整它們的pom.xml 以繼承上面的父工程。
  • 例如mm-web模組的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>com.hehe</groupId>
 <artifactId>mm-web</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>
 <name>mm-web</name>
 <!-- 繼承本專案的父工程 -->
 <parent>
 <groupId>com.hehe</groupId>
 <artifactId>springboot-integration</artifactId>
 <version>1.0.0.RELEASE</version>
 </parent>
 <!-- Web模組相關依賴 -->
 <dependencies>
 <dependency>
 <groupId>com.hehe</groupId>
 <artifactId>mm-service</artifactId>
 </dependency>
 <dependency>
 <groupId>com.hehe</groupId>
 <artifactId>mm-entity</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 <dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-test</artifactId>
 <scope>test</scope>
 </dependency>
 </dependencies>
</project>

三、編寫子模組程式碼

1. 控制層(mm-web)

SpringBoot 多模組專案實踐(附打包方法)

 

結構圖

啟動類 :MmWebApplication.java (mm-web)

@SpringBootApplication
public class MmWebApplication {
 public static void main(String[] args) {
 SpringApplication.run(MmWebApplication.class, args);
 }
}

控制器:UserController.java (mm-web )

@RestController
@RequestMapping("/user/*")
public class UserController {
 @Autowired
 UserService userService;
 @GetMapping("list")
 public R list() {
 try {
 return R.isOk().data(userService.list());
 } catch (Exception e) {
 return R.isFail(e);
 }
 }
}

配置檔案:application.yml (mm-web)

spring:
 datasource:
 url: jdbc:mysql://localhost:3306/socks?useSSL=false
 username: root
 password: root
 driver-class-name: com.mysql.jdbc.Driver

2. 業務層(mm-service)

SpringBoot 多模組專案實踐(附打包方法)

 

結構圖

實現類:UserServiceImpl.java (mm-service)

@Service
public class UserServiceImpl implements UserService {
 @Autowired
 UserRepository userRepository;
 @Override
 public List<User> list() {
 return userRepository.findAll();
 }
}

3. 資料層(mm-repo)

SpringBoot 多模組專案實踐(附打包方法)

 

結構圖

資料層程式碼:UserRepository.java (mm-repo)

public interface UserRepository extends JpaRepository<User,String> {
}

4. mm-entity (實體模型層)

SpringBoot 多模組專案實踐(附打包方法)

 

結構圖

R.java 作為統一返回的Bean物件

package com.hehe.integration.common;
import java.io.Serializable;
public class R<T> implements Serializable {
 private static final long serialVersionUID = -4577255781088498763L;
 private static final int OK = 0;
 private static final int FAIL = 1;
 private static final int UNAUTHORIZED = 2;
 private T data; //服務端資料
 private int status = OK; //狀態碼
 private String msg = ""; //描述資訊
 //APIS
 public static R isOk(){
 return new R();
 }
 public static R isFail(){
 return new R().status(FAIL);
 }
 public static R isFail(Throwable e){
 return isFail().msg(e);
 }
 public R msg(Throwable e){
 this.setMsg(e.toString());
 return this;
 }
 public R data(T data){
 this.setData(data);
 return this;
 }
 public R status(int status){
 this.setStatus(status);
 return this;
 }
 //Constructors
 public R() {
 }
 //Getter&Setters
 
}
@Entity
@Table(name = "T_USER")
public class User {
 @Id
 @Column(name = "USERID")
 private String userId;
 @Column(name = "USERNAME")
 private String username;
 @Column(name = "PASSWORD")
 private String password;
 
 //Getter&Setters
}

三、執行專案

為了更好的學習效果,建議先下載本專案,在IDE執行成功之後,然後再由自己手工敲一遍。

具體步驟:

  • 1.首先下載好 springboot-socks,然後開啟springboot-integration 工程。

SpringBoot 多模組專案實踐(附打包方法)

 

image.png

  • 2.安裝Mysql資料庫,然後建立資料庫socks,並新增表t_user,插入資料如圖:

SpringBoot 多模組專案實踐(附打包方法)

 

sock.t_user.PNG

  1. 配置好整個專案之後,這裡只需要執行mm-web模組下的MmWebApplication的啟動類就可以了,如正常啟動後,訪問http://localhost:8080 可查詢到使用者列表資訊。如下圖:

SpringBoot 多模組專案實踐(附打包方法)

 

image.png

四、運維部署(多模組打包)

1. 新增打包外掛

注意:多模組專案僅僅需要在啟動類所在的模組新增打包外掛即可!!不要在父類新增打包外掛,因為那樣會導致全部子模組都使用spring-boot-maven-plugin的方式來打包(例如BOOT-INF/com/hehe/xx),而mm-web模組引入mm-xx 的jar 需要的是裸露的類檔案,即目錄格式為(/com/hehe/xx)。

本案例的啟動模組是 mm-web , 只需在它的pom.xml 新增打包外掛(spring-boot-maven-plugin):

 
 <!--多模組打包:只需在啟動類所在模組的POM檔案:指定打包外掛 -->
 <build>
 <plugins>
 <plugin>
 <!--該外掛主要用途:構建可執行的JAR -->
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-maven-plugin</artifactId>
 </plugin>
 </plugins>
 </build>

2. 打包工程

首先在IDE開啟Maven外掛,然後在聚合父工程spring-boot-integration中點選 clean ,然後點選 package 進行打包。如圖:

SpringBoot 多模組專案實踐(附打包方法)

 

打包效果如下:

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] spring-boot-integration ............................ SUCCESS [ 0.000 s]
[INFO] mm-entity .......................................... SUCCESS [ 1.915 s]
[INFO] mm-repo ............................................ SUCCESS [ 0.235 s]
[INFO] mm-service ......................................... SUCCESS [ 0.218 s]
[INFO] mm-web ............................................. SUCCESS [ 0.891 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.798 s
[INFO] Finished at: 2017-10-18T17:17:02+08:00
[INFO] Final Memory: 35M/300M
[INFO] ------------------------------------------------------------------------

打包地址預設在Target目錄:

SpringBoot 多模組專案實踐(附打包方法)

 

3. 啟動專案

通過命令列啟動專案:

xxmm-web	arget>java -jar mm-web-0.0.1-SNAPSHOT.jar

啟動效果如下:

SpringBoot 多模組專案實踐(附打包方法)