IDEA 創建 Spring Boot 多模塊項目(Multi Modules)


本准備寫點代碼實例放到網站,太多的模板,反而每次新建工程的時候很麻煩。於是准備把這個章節的內容提前先講講。正好把這個代碼也管理起來。話說這個多模塊功能還挺爽。

寫過 C# 項目用過 Visual Studio 的人已經用慣了 一大把的項目放在一個解決方案中,下面我來實踐一下 Java Spring Boot 的玩法。

目錄

  • TOC
    {:toc}

本項目源碼spring-boot-study-helloworld下載
本項目源碼spring-boot-study-helloworld-service下載

本章演示的多模塊之間的關系如下圖:spring-boot-study-helloworld 依賴人 spring-boot-study-helloworld-service 調用其方法 message 輸出字符串。
項目關系圖

本章節主要實現

  • 一個 maven 工程下多個模塊項目按時
  • 一個 maven 工程下各個項目的依賴關系
  • 一個 maven 工程下可以有多個 web application 項目共存

1 建立根項目 Root Project

使用 IDEA 創建一個 Maven 工程

  1. File>New>Project,如下圖選擇 maven ,注意 Create from archetype 不能打鈎,然后點擊 【Next】下一步。
    選擇Maven項目
  2. 填寫GroupId(包名)、Artifact(項目名) 即可。點擊 【Next】下一步
  • groupId=com.fishpro
  • artifactId=springstudy
  1. 項目名設置為 spring-boot-study
  2. 打開 pom.xml 編輯,增加 <packaging>pom</packaging>
<?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.fishpro</groupId>
    <artifactId>springstudy</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
</project>

  1. 刪除 src 文件
  2. 注意如果您是創建的 spring 項目作為根項目,那么您需要刪除 src、mvnw、mvnw.cmd、HELP.md、.mvn 文件

2 建立子項目

子項目可以是類庫項目、Web應用項目

  1. 應用模塊項目 例如 Application 或 Web Application
  2. 類庫 Jar 模塊項目,即 Library Jar

2.1 建立子Web Application 模塊 spring-boot-study-helloworld

  1. 右鍵項目名稱 spring-boot-study > New > Module 進入項目模塊新增頁面
  2. 因為我們這里是 Spring Boot 項目,那么我選擇 Spring Initializr 選擇【Next】
  3. 填寫GroupId(包名)、Artifact(項目名) 即可。點擊 【Next】下一步
  • groupId=com.fishpro
  • artifactId=helloworld
  1. 設置模塊為 maven 中的子模塊 項目名設置為 spring-boot-study-helloworld,至此子項目已經添加完了,但你會發現,子項目不能允許調試,必須在根目錄下的 pom.xml 配置此項目才行。
  2. 設置父項目 pom.xml 加入模塊 spring-boot-study-helloworld
<?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.fishpro</groupId>
    <artifactId>springstudy</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>spring-boot-study-helloworld</module>
    </modules>
</project>
  1. 重命名application.propertiesapplication.yml 增加端口設置,為了和前天模塊不沖突
server:
  port: 8082

設置父項目的pom
7. 簡單調試,右鍵 com.fishpro.helloworld.HelloworldApplication > Run HelloworldApplication 子項目 Helloworld 就運行起來了。
調試子模塊項目
我們還可以使用相同的方式增加多個模塊

2.2 建立另一個 library jar 子模塊項目 spring-boot-study-hellowrld-servie

  1. 右鍵項目名稱 spring-boot-study > New > Module 進入項目模塊新增頁面
  2. 因為我們這里是 Spring Boot 項目,那么我選擇 Spring Initializr 選擇【Next】
  3. 填寫GroupId(包名)、Artifact(項目名) 即可。點擊 【Next】下一步
  • groupId=com.fishpro.helloworld
  • artifactId=service
    4.新建類 MyService
package com.fishpro.helloworld.service;
import org.springframework.stereotype.Service;
@Service
public class MyService {
    public String message(){
        return "this is module for helloworld.service method message";
    }
}

2.3 建立模塊之間的依賴關系

有的時候一個項目的 dao、service、controller 是分在不同的模塊中,那么他們之間就有一些依賴關系,通常這些依賴關系是單向的。

本項目中項目 spring-boot-study-hellowrld 依賴項目 spring-boot-study-helloworld-service ,那么在 spring-boot-study-hellowrld 模塊下 pom.xml 中設置

<dependency>
	<groupId>com.fishpro.helloworld</groupId>
	<artifactId>service</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</dependency>

2.4 在 helloworld 模塊中調用 helloworld.service 模塊方法

我們在 helloworld 項目下新建 controller.IndexController

package com.fishpro.helloworld.controller;

import com.fishpro.helloworld.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {
    @Autowired
    MyService myService;
    @GetMapping("/say")
    public String say(){
        return  myService.message();
    }
}

在瀏覽器中 輸入 http://localhost:8082/say 顯示 hellowrld 項目調用成功

this is module for helloworld.service method message

2.4 建立另一個獨立的 web application 子模塊項目 spring-boot-study-log

我們再建立一個項目,項目名稱 spring-boot-study-log 方法同第 2 節一樣。最終效果圖如下:
多項目效果圖

至此,我們發現可以多個 web application 是可以共存的。所以我們后面的所有示例都是在這個項目中體現。

2.5 圖形化管理模塊界面

IDEA 中也可以通過圖形化管理模塊
在 File > Project Structrue 中管理項目的模塊
多模塊項目管理

本項目中 application.properties 已經重新命名為 application.yml


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM