springboot-多模块构建

1. 场景描述

先介绍下背景,项目为什么需要用多模块?springmvc难道还不够?

(1)设计模式真言:“高内聚、低耦合”,springmvc项目,一般会把项目分成多个包:controller、service、dao、util等,但是随着项目的复杂性提高,想复用其他一个模块的话,因为是包的形式,剥离出来会比较困难,耦合性有点强,常用的方法就是复制代码修改,但是这样会做很多无用功与增加出错几率。

(2)springboot多模块简单来说,就是把按包分模块的模式,借助maven升级到jar的方式,抽象性更加强了,假如jar再升级到到war或者多个集合jar,就成微服务了( springcloud入门系列),在多模块jar模式下可以将某个jar拿出来对外共用,能大大提高代码复用率与开发效率。

2. 解决方案

2.1 整体思路

(1)新建springboot项目;

(2)在新建后的springboot项目中新建多个module;

(3)修改pom文件以及删除多余的文件及文件夹。

2.2 新建springboot项目(springboot项目快速搭建

(1)new->project

(2)next,名字改一下。

2.3 新建module

(1)在springboot项目上点击右键->new->module

其余方式跟上面的springboot方式一样,不再多说了。

(2)新建三个module:controller、service、dao,新建后的效果图如下:

2.4 删除多余的文件及修改配置文件(重点)

2.4.1 删除多余文件及文件夹

(1)springboot项目

​ 整体删除src文件夹。

(2)module模块

将service和dao下面的application启动类和对应配置文件application.yml/prpperty,一起删除了,cotroller模块的不动。

2.4.2 修改pom.xml

根据springmvc架构,几个module之间依赖顺序 controller->service->dao

(1)修改springboot最外层pom.xml

这个是父pom.xml,用于加载一些全局的或者公共的jar包,以及配置打包。

此pom文件中,需要需改两个地方:

一是修改打包模式为pom;

二是新建modules标签,将3个module增加进来。

如下:

 <packaging>pom</packaging>

<modules>
        <module>controller</module>
        <module>service</module>
        <module>dao</module>
</modules>

(2)修改cotroller的pom.xml文件

修改标签为本项目springboot项目的gav信息和依赖service的jar包信息。

    <!--<parent>-->
        <!--<groupId>org.springframework.boot</groupId>-->
        <!--<artifactId>spring-boot-starter-parent</artifactId>-->
        <!--<version>2.1.6.RELEASE</version>-->
        <!--<relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->
    <!--</parent>-->

    <parent>
        <groupId>com.laowang</groupId>
        <artifactId>lwmodul</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

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

(3)修改service的pom.xml文件

与controller类似,只是依赖改为dao。

  <!--<parent>-->
        <!--<groupId>org.springframework.boot</groupId>-->
        <!--<artifactId>spring-boot-starter-parent</artifactId>-->
        <!--<version>2.1.6.RELEASE</version>-->
        <!--<relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->
    <!--</parent>-->
    <parent>
        <groupId>com.laowang</groupId>
        <artifactId>lwmodul</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <dependency>
        <groupId>com.laowang</groupId>
        <artifactId>dao</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency> 

(4)修改dao的pom.xml文件

只需修改parent,不需要再配置依赖了。

  <!--<parent>-->
        <!--<groupId>org.springframework.boot</groupId>-->
        <!--<artifactId>spring-boot-starter-parent</artifactId>-->
        <!--<version>2.1.6.RELEASE</version>-->
        <!--<relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->
    <!--</parent>-->

    <parent>
        <groupId>com.laowang</groupId>
        <artifactId>lwmodul</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

(5)启动

在Cotroller模块中使用启动类ControllerApplication启动,空项目的话,看到这一行就说明成功了。

Started ControllerApplication in 2.485 seconds (JVM running for 3.639)

2.5 增加web访问验证

2.5.1 配置文件

(1)controller下面的application.property改下端口号(不更改的话默认是:8080)。

server.port=9000

(2)增加依赖

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.5.2 启动类

在启动类ControllerApplication增加一个标签(@RestController)和一个请求方法(home())。

package com.laowang.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class ControllerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ControllerApplication.class, args);
    }
    
    @RequestMapping("/")
    public String home() {
        return "i'm 软件老王,欢迎光临!";

    }
}

2.5.3 效果图


I'm 软件老王,如果觉得还可以的话,关注下呗!如有不准确或疑问的地方,可通过讨论区、QQ沟通,多谢!

posted @ 2019-07-19 16:51  软件老王  阅读(21218)  评论(0编辑  收藏  举报