Springboot多模块依赖开发

前言

有关Spring-boot多模块依赖开发,针对使用springboot架构的java程序开发,为了解耦合,需要把各个功能模块给封装起来并区分开,进行模块化,减少重复性的工作;
项目地址:https://github.com/voctex/springboot-multiple-module

多模块依赖开发

创建父项目

首先先新建一个项目,选择springboot方式创建;

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
018

创建好项目之后,然后会出现以下项目结构,因为这只是当做一个父项目,不需要有具体的实现,所以只保留红框内文件(.gitignore可忽略),其他文件删除掉;

017

创建子模块

删掉多余的文件之后,接下来我们新建三个子项目,也就是三个模块

016

分别创建base、network、web三个模块,network模块依赖base模块,而web模块又依赖于network模块,而三个模块的父项目就是demo;

base模块;

015

network模块

014

013

web模块

012

011

项目整理

删除多余文件

web作为主模块,即启动程序,所以保留WebApplication和application.properties,其他两个模块,因为是作为子模块,也就是依赖库存在的,所以把对应的application文件都删掉;

010

我在这里把web模块的application.properties文件修改为了application.yml文件格式了,这个你们可忽略;

009

008

移动application类

因为如果是放在com.test.web,其他依赖的子模块如com.test.base下面的类是无法被扫描到的,所以需要把application类给移动到com.test包下,如图:

007

006

005

修改pom.xml文件

修改父模块的pom.xml

现在切换回父模块demo,修改pom.xml文件,

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.test</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <!-- 父模块打包类型必须为pom -->
    <packaging>pom</packaging>
    <!-- 所有子模块的声明 -->
    <modules>
        <module>base</module>
        <module>network</module>
        <module>web</module>
    </modules>
    <!-- 版本说明:这里统一管理依赖的版本号 -->
    <dependencyManagement>
        <dependencies>
            <!--        <dependency>-->
            <!--            <groupId>org.springframework.boot</groupId>-->
            <!--            <artifactId>spring-boot-starter</artifactId>-->
            <!--        </dependency>-->

            <!--        <dependency>-->
            <!--            <groupId>org.springframework.boot</groupId>-->
            <!--            <artifactId>spring-boot-starter-test</artifactId>-->
            <!--            <scope>test</scope>-->
            <!--        </dependency>-->
            <dependency>
                <groupId>com.test</groupId>
                <artifactId>base</artifactId>
                <version>0.0.1</version>
            </dependency>
            <dependency>
                <groupId>com.test</groupId>
                <artifactId>network</artifactId>
                <version>0.0.1</version>
            </dependency>
            <dependency>
                <groupId>com.test</groupId>
                <artifactId>web</artifactId>
                <version>0.0.1</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

</project>

修改子模块的pom.xml

base子模块的pom.xml文件内容如下,其中parent是使用顶层的父模块,并且由于我们的base模块用到了lombok,所以还添加了lombok的依赖;

<?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>
    <!-- 继承本项目的父模块 -->
    <parent>
        <groupId>com.test</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1</version>
<!--        <relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->
    </parent>

    <groupId>com.test</groupId>
    <artifactId>base</artifactId>
    <version>0.0.1</version>
    <name>base</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <!--非启动程序,所以注释掉相关代码-->
<!--    <build>-->
<!--        <plugins>-->
<!--            <plugin>-->
<!--                <groupId>org.springframework.boot</groupId>-->
<!--                <artifactId>spring-boot-maven-plugin</artifactId>-->
<!--            </plugin>-->
<!--        </plugins>-->
<!--    </build>-->

</project>

network模块因为依赖了base模块,同样 parent 要使用顶层的父模块demo,所以需要在pom.xml添加声明,内容如下;

<?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>
    <!--继承本项目的父模块-->
    <parent>
        <groupId>com.test</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1</version>
<!--        <relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->
    </parent>

    <groupId>com.test</groupId>
    <artifactId>network</artifactId>
    <version>0.0.1</version>
    <name>network</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- network 子模块需要使用web的一些注释,所以需要添加 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- network 子模块又依赖 base 子模块 -->
        <dependency>
            <groupId>com.test</groupId>
            <artifactId>base</artifactId>
            <version>0.0.1</version>
        </dependency>
    </dependencies>

<!--    <build>-->
<!--        <plugins>-->
<!--            <plugin>-->
<!--                <groupId>org.springframework.boot</groupId>-->
<!--                <artifactId>spring-boot-maven-plugin</artifactId>-->
<!--            </plugin>-->
<!--        </plugins>-->
<!--    </build>-->

</project>

web模块依赖network模块,同样 parent 要使用顶层的父模块,所以web模块的pom.xml内容如下:

<?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>
    <!--继承顶层的父模块-->
    <parent>
        <groupId>com.test</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1</version>
<!--        <relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->
    </parent>

    <groupId>com.test</groupId>
    <artifactId>web</artifactId>
    <version>0.0.1</version>
    <name>web</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- web 子模块又依赖 network 子模块 -->
        <dependency>
            <groupId>com.test</groupId>
            <artifactId>network</artifactId>
            <version>0.0.1</version>
        </dependency>
    </dependencies>

    <!--启动程序的配置-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

测试

创建测试类

现在我们对着多模块的项目进行一下测试,首先我们先在base模块里边新建一个Base实体类;

004

package com.test.base;

import lombok.Data;

/**
 * @author Voctex.Chen
 * @version V1.0.0
 * @program: demo
 * @Package com.test.base
 * @Description: TODO
 * @date 2020/11/26 9:13
 */
@Data
public class Base {

    private int id;

    private String name;

    private String text;
}

接着在network里边新建一个get请求接口,返回一个Base类型的变量;

003

package com.test.network;

import com.test.base.Base;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @author Voctex.Chen
 * @version V1.0.0
 * @program: demo
 * @Package com.test.network
 * @Description: TODO
 * @date 2020/11/26 10:10
 */
@RequestMapping(value = "/test")
public interface BaseRequest {

    @RequestMapping(value = "/base", method = RequestMethod.GET)
    Base getBase();

}

在启动程序web模块中,新建一个controller进行接口的实现,然后配置一下application.yml文件,换个端口,启动程序;

002

package com.test.web.controller;

import com.test.base.Base;
import com.test.network.BaseRequest;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Voctex.Chen
 * @version V1.0.0
 * @program: demo
 * @Package com.test.web.controller
 * @Description: TODO
 * @date 2020/11/26 10:15
 */
@RestController
public class BaseController implements BaseRequest {

    @Override
    public Base getBase() {
        Base base=new Base();
        base.setId(123);
        base.setName("voctex.chen");
        base.setText("This is a Demo.");
        return base;
    }
}

application.yml

server:
  port: 7373
  servlet:
    context-path: /
    encoding:
      force: true
      charset: UTF-8
      enabled: true
  tomcat:
    uri-encoding: UTF-8

浏览器输入接口地址,获得内容如下;

001

参考文献

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值