0%

JAVA搞不清楚系列 - Spring Boot Multi Module Porject

第一次碰到Multi Module其實應該是在做gRPC的專案的時候,後來才又重新認真研究了一下。
Spring Boot的Multi Module 可以幫助專案規劃分module,重複使用POM,以免引入變得過於龐大,且不用每次都構建整個專案,並且使得針對某個模組的特殊控制更為方便。

這篇文章是參考這個文章實作的:Spring Boot – Multi-Module Project

Gitlab連結

https://gitlab.com/jam68ty/spring-boot-multi-modules-semgrep

流程總覽

  1. 建立一個母moudule以及兩個子module
  2. 重構專案結構、產生jar檔
  3. 母module引入子moudule
  4. 寫一些測試的Service, Controller
  5. 此時應該要可以透過母module去控管整個專案共同需要的POM,而個別子module需要的dependency則在該module中引入即可

實作詳解

  1. 利用Maven Quickstart Archetype建立一個Parent Module
    1
    mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4
  2. Spring Initializr建立兩個Child Modules,並把這兩個專案都移到Parent Module下
    a. jdjcore: 不引入任何dependency
    b. jdjweb: 引入Spring-web
  3. 完成後先mvn clean install 母Module
  4. 修改Parent Module的POM,引入兩個子Module
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    <?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.test</groupId>
    <artifactId>multi-module-project</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!-- 更改packaging的類型為pom-->
    <packaging>pom</packaging>
    <name>multi-module-project</name>

    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <java-version>1.8</java-version>
    </properties>

    <!-- 引入子modules-->
    <modules>
    <module>jdj-core</module>
    <module>jdj-web</module>
    </modules>

    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>2.3.6.RELEASE</version>
    <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <version>2.3.6.RELEASE</version>
    </dependency>
    </dependencies>
    </project>
  5. 修改Child Module - jdjcore (包含Entity, DAO, Service)
  • POM
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!-- 引入母modules-->
    <parent>
    <groupId>com.test</groupId>
    <artifactId>multi-module-project</artifactId>
    <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>jdj-core</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>jdj-core</name>
    <description>Demo project for Spring Boot</description>
    <properties>
    <java.version>1.8</java.version>
    </properties>
    </project>
  • Entity: com.test.jdjcore.entity.Customer
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    package com.test.jdjcore.entity;

    public class Customer {

    private int id;
    private String name;
    private String email;

    public Customer() {
    }

    public Customer(int id, String name, String email) {
    this.id = id;
    this.name = name;
    this.email = email;

    }

    public int getId() {
    return id;
    }

    public void setId(int id) {
    this.id = id;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getEmail() {
    return email;
    }

    public void setEmail(String email) {
    this.email = email;
    }
    }
  • DAO: com.test.jdjcore.dao.CustomerRepository
    1
    2
    3
    4
    5
    6
    7
    package com.test.jdjcore.dao;

    import com.test.jdjcore.entity.Customer;

    public interface CustomerRepository {
    Customer getCustomerById(int id);
    }
  • Service: com.test.jdjcore.service.CustomerService
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    package com.test.jdjcore.service;

    import com.test.jdjcore.dao.CustomerRepository;
    import com.test.jdjcore.entity.Customer;
    import org.springframework.stereotype.Service;

    @Service("customerService")
    public class CustomerService implements CustomerRepository{

    @Override
    public Customer getCustomerById(int id) {
    return new Customer(id, "name", "email");
    }
    }
  • 把JdjCoreApplication的@SpringBootApplication註解掉,並把main的內容也刪除
  1. 修改Child Module - jdjweb (包含Controller)
  • POM
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!-- 引入母modules-->
    <parent>
    <groupId>com.test</groupId>
    <artifactId>multi-module-project</artifactId>
    <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>jdj-web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>jdj-web</name>
    <description>Demo project for Spring Boot</description>
    <properties>
    <java.version>1.8</java.version>
    </properties>
    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.6.4</version>
    </dependency>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>2.6.4</version>
    <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>com.test</groupId>
    <artifactId>jdj-core</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    </dependency>
    </dependencies>

    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    <finalName>javadev-web</finalName>
    </build>

    </project>
  • Controller: com.test.jdjweb.controller.CustomerController
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    package com.test.jdjweb.controller;

    import com.test.jdjcore.dao.CustomerRepository;
    import com.test.jdjcore.entity.Customer;
    import com.test.jdjcore.service.CustomerService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    @RequestMapping("")
    public class CustomerController {

    @Autowired
    CustomerService customerService;

    @GetMapping("/customer/{id}")
    public Customer getCustomer(@PathVariable int id){
    return customerService.getCustomerById(id);
    }

    }

    由於Multi-modules,@Autowierd可能會出錯,此時需要在JdjWebApplication.java加上@ComponentScan({”com.test.jdjcore”, “com.test.jdjweb”})

結果

  1. Start Application: 對整個專案mvn clean installmvn spring-boot:run jdj-web
  2. Test: http://localhost:8080/customer/1