javascript
source tree 递归子模块_多模块 Spring Boot 项目
在開發一個稍具規模的項目時,我們常常希望能夠將程序的各個部分分成不同的模塊,加以管理。本完簡單介紹了如何建立一個多模塊的 Spring Boot 項目。
假設我們需要開發一個包含 2 個子模塊的項目:app 子模塊是上層應用,service 子模塊提供服務。
代碼:https://github.com/chenf42/spring-boot-examples/tree/master/multi-module-project
1. 模塊配置
使用 Spring Initializr 新建一個 Spring Boot 項目。記得選中 Spring Web 依賴。
修改 pom.xml 文件,將打包模式改為 pom 方式,即添加或修改 <packaging> tag 為:
<packaging>pom</packaging>并添加 <modules> tag 來指示我們的子模塊:
<modules><module>app</module><module>service</module> </modules>這個項目主模塊是不直接運行的,我們可以刪掉 <build> tag 和 src 文件夾。
在項目管理視圖上右擊 multi-module-project 項目名稱,添加 app 模塊:
使用同樣的方式添加另一個名為 service 的子模塊。
我們得要把項目模塊設置為各子模塊的父模塊,將 app/pom.xml 和 service/pom.xml 中的 <parent> tag 修改為如下內容:
<parent><groupId>com.meta-object</groupId><artifactId>multi-module-project</artifactId><version>0.0.1-SNAPSHOT</version> </parent>因為 service 子模塊只用來給 app 提供服務,而不會被直接運行,我們可以把 service/pom.xml 中的 <build> tag 刪掉,并刪掉不需要的 service/src/main/java/com/metaobject/multimoduleproject/service/ServiceApplication.java 文件。
同時,我們需要修改 app/pom.xml,添加對 service 模塊的依賴:
<dependency><groupId>com.meta-object.multi-module-project</groupId><artifactId>service</artifactId><version>0.0.1-SNAPSHOT</version> </dependency>2. 功能實現
添加 service/src/main/java/com/metaobject/multimoduleproject/service/User.java 文件:
package com.metaobject.multimoduleproject.service;public class User {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;} }添加 service/src/main/java/com/metaobject/multimoduleproject/service/UserService.java 文件:
package com.metaobject.multimoduleproject.service;import org.springframework.stereotype.Service;@Service public class UserService {public User getDefaultAdmin() {User user = new User();user.setName("admin");return user;} }這里我們提供了一個最簡單的“服務”:查詢默認管理員信息。
添加 app/src/main/java/com/metaobject/multimoduleproject/app/UserController.java 文件:
package com.metaobject.multimoduleproject.app;import com.metaobject.multimoduleproject.service.User; import com.metaobject.multimoduleproject.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;@RestController public class UserController {private UserService userService;@Autowiredpublic UserController(UserService userService) {this.userService = userService;}@GetMapping("/admin")public User showDefaultAdmin() {return userService.getDefaultAdmin();} }注意這里 Intellij IDEA 會給出警告:Could not autowire. No beans of 'UserService' type found.。
若我們運行 app 模塊,會得到如下輸出:
*************************** APPLICATION FAILED TO START ***************************Description:Parameter 0 of constructor in com.metaobject.multimoduleproject.app.UserController required a bean of type 'com.metaobject.multimoduleproject.service.UserService' that could not be found.Action:Consider defining a bean of type 'com.metaobject.multimoduleproject.service.UserService' in your configuration.這是因為 app 模塊沒掃描到 UserService 這個服務。解決方法是在 app/src/main/java/com/metaobject/multimoduleproject/app/AppApplication.java 中導入此服務:
package com.metaobject.multimoduleproject.app;import com.metaobject.multimoduleproject.service.UserService; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Import;@SpringBootApplication @Import(UserService.class) // 導入 UserService public class AppApplication {public static void main(String[] args) {SpringApplication.run(AppApplication.class, args);}}3. 試一試!
此時重新運行 app 模塊并訪問 http://localhost:8080/admin 可以看到如下結果:
(完)
總結
以上是生活随笔為你收集整理的source tree 递归子模块_多模块 Spring Boot 项目的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 专属经济区是用200海里减去领海的12海
- 下一篇: nodejs redis 发布订阅_太赞