隨著業務增長,單體項目拆分為多模塊是必經之路。本文演示如何將 Spring Boot 項目重構為「父工程聚合 + 子模塊業務」結構,並創建 web-rest Web 模塊,奠定微服務擴展基礎。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.taynio.demo</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging> <!-- 關鍵:聚合工程打包類型為 pom -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>web-rest</module> <!-- 聲明子模塊 -->
</modules>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
關鍵點:
1. <packaging>pom</packaging> 表示本模塊不產出 JAR,僅聚合子模塊
2. 統一管理 <properties>、<dependencyManagement>、<pluginManagement>,子模塊無版本號引用
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://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.taynio.demo</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>web-rest</artifactId>
<packaging>jar</packaging> <!-- 產出可執行 JAR -->
<dependencies>
<!-- Web 開發起步依賴:內嵌 Tomcat + Spring MVC + Jackson -->
<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>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.taynio.demo.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication // = @Configuration + @EnableAutoConfiguration + @ComponentScan
public class SpringbootWebRestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootWebRestApplication.class, args);
}
}
掃描範圍:默認掃描啟動類所在包及子包(com.taynio.demo.springboot),跨包組件需顯式配置 @ComponentScan。
springboot (父工程)
├── pom.xml
└── web-rest (子模塊)
├── pom.xml
└── src/main/java/com/taynio/demo/springboot
└── SpringbootWebRestApplication.java
Maven → Reload Project,或手動 File → Project Structure → Modules 添加application.yml 配置 server.port: 8081spring-boot-maven-plugin 並執行 mvn packageservice、dao、common 等模塊,父工程 <modules> 追加聲明spring-cloud-dependencies 統一管理微服務組件版本maven-enforcer-plugin 強制依賴收斂,避免版本衝突