一、Spring Boot多模塊項目簡介
在當今軟件開發中,多模塊項目已經變得越來越流行。在Spring Boot框架中,我們同樣可以使用多模塊來構建我們的項目。Spring Boot多模塊項目可以幫助我們更好地實現項目的分層管理和提高代碼的可維護性。下面將從幾個方面詳細介紹Spring Boot多模塊項目的搭建和使用。
二、創建Spring Boot多模塊項目
在開始之前,我們先來了解一下如何創建Spring Boot多模塊項目。首先,我們需要使用Spring Initializr
來快速創建一個Spring Boot父工程,具體步驟如下:
- 打開
https://start.spring.io/
,輸入項目信息,包括項目名稱、項目版本號等。 - 選擇
Project Metada
選項卡,輸入項目的元數據,包括Group
、Artifact
、Name
、Description
等信息。 - 選擇需要的依賴,例如Web、JPA、Thymeleaf等。
- 點擊
Generate
按鈕,下載生成的項目壓縮包。
解壓壓縮包,在解壓後的目錄下使用mvn clean package
命令編譯項目。
三、Spring Boot多模塊項目的結構
Spring Boot多模塊項目的結構通常會有一個父模塊和多個子模塊。下面我們來看一下標準的Spring Boot多模塊項目的結構:
├── parent │ ├── pom.xml │ ├── common │ │ ├── pom.xml │ ├── service │ │ ├── pom.xml │ ├── web │ │ ├── pom.xml
在這個結構中,parent
是父模塊,common
、service
、web
都是子模塊。在實際項目中,還可以根據需要增加其他子模塊。
四、Spring Boot多模塊項目的搭建
1. 創建父模塊
創建一個Spring Boot多模塊項目的第一步是創建父模塊。在父模塊的pom.xml
文件中,我們需要指定Spring Boot的父依賴和一些常用的依賴版本,如下所示:
<groupId>com.example</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.2</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>11</java.version> </properties> <dependencies> <!-- Add your dependencies here --> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <modules> <module>common</module> <module>service</module> <module>web</module> </modules>
在這段pom.xml
文件中,<parent>
元素指定了Spring Boot父依賴,<properties>
元素指定了依賴版本,<dependencies>
元素可以添加項目所需要的其他依賴,例如數據庫、緩存等。然後最重要的是<build>
元素,這裡定義了構建Spring Boot項目所使用的插件,例如spring-boot-maven-plugin
。
2. 創建子模塊
接下來,我們需要創建三個子模塊:common
、service
、web
。每個子模塊都應該包含一個pom.xml
文件,定義了子模塊所需要的依賴和構建配置。下面是common
子模塊的pom.xml
示例:
<parent> <groupId>com.example</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>common</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> </dependencies>
在這個示例中,<parent>
元素引用了父模塊的pom.xml
文件,<dependencies>
元素定義了common
模塊所需要的依賴,包括Spring Boot的基礎依賴和Jackson的依賴。
五、Spring Boot多模塊項目的實戰
1. 創建通用模塊
在我們的多模塊項目中,我們可以使用一個通用模塊來存放一些共用的代碼和配置,例如常量、工具類、攔截器等。在這個示例中,我們創建一個common
子模塊來存放一些共用的代碼和配置。
首先,我們在common
子模塊中創建一個MyConstants
類,並在其中定義一些常量,如下所示:
public class MyConstants { public static final String DATE_FORMAT = "yyyy-MM-dd"; }
然後,我們創建一個MyInterceptor
類,繼承自HandlerInterceptorAdapter
,並在其中實現攔截器邏輯,如下所示:
public class MyInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle( HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 在這裡實現攔截器邏輯 return true; } }
在common
子模塊的resources
目錄下,我們創建一個application.yml
文件,並在其中添加以下內容:
my: date-format: yyyy-MM-dd
這個文件中定義了一個date-format
配置項。
現在,我們已經在common
子模塊中創建了一些共用的代碼和配置。下一步我們來創建service
子模塊和web
子模塊,來使用這些代碼和配置。
2. 創建服務模塊
service
子模塊是我們的業務邏輯實現模塊,我們可以在這個模塊中實現我們業務的邏輯。在這個示例中,我們創建一個UserService
類,用來提供用戶信息相關的業務操作。
@Service public class UserService { @Autowired private UserRepository userRepository; public User getUserById(Long id) { return userRepository.findById(id).orElse(null); } }
在這個示例中,我們使用@Service
註解標記這個類為Spring Bean,使用@Autowired
註解注入了一個UserRepository
實例,用來實現持久化操作。然後我們定義了一個getUserById
方法,用來根據id
獲取用戶信息。
3. 創建Web模塊
web
子模塊是我們的Web模塊,我們可以在這個模塊中實現我們的Web接口和頁面。在這個示例中,我們創建一個UserController
類,用來提供用戶信息相關的接口操作。
@RestController public class UserController { @Autowired private UserService userService; @GetMapping("/user/{id}") public User getUserById(@PathVariable Long id) { return userService.getUserById(id); } }
在這個示例中,我們使用@RestController
註解標記這個類為Spring MVC的Controller類,使用@Autowired
註解注入了一個UserService
實例,用來調用業務邏輯處理。然後我們定義了一個getUserById
方法,用來根據id
獲取用戶信息,並返回給客戶端。
現在,我們已經看到了如何使用Spring Boot多模塊項目的優勢來組織和管理我們的代碼,包括創建父模塊、子模塊、通用模塊、實現服務模塊和Web模塊等。通過這種方式,我們可以更好地實現代碼分層管理和提高代碼的可維護性。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/231648.html