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沟通,多谢!

springboot-多模块构建的更多相关文章

  1. 使用IDEA构建Spring-boot多模块项目配置流程

    使用IDEA构建Spring-boot多模块项目配置流程 1.创建项目 点击Create New Project 在左侧选中Spring Initializer,保持默认配置,点击下一步. 在Grou ...

  2. gradle多模块构建集成swagger

    1.首先说一下软件的版本:springboot:1.5.2:springcloud:D-SR1:swaager2:2.6.0:gradle:4.5.工程模块是分开的单独的entity,api,mapp ...

  3. 记Spring搭建功能完整的个人博客「Oyster」全过程[其二] Idea中Maven+SpringBoot多模块项目开发的设计和各种坑(模块间依赖和打包问题)

    大家好嘞,今天闲着没事干开写写博客,记录一下Maven+SpringBoot的多模块设计和遇到的坑. 多模块设计 简单说明一下截止目前的需求: 需要RESTful API:对文章.标签.分类和评论等的 ...

  4. docker部署 springboot 多模块项目+vue

    之前学习了docker,今天就来试试将这个项目打包成docker镜像并通过运行一个镜像来运行项目.这里使用的项目是el-admin.是一个开源的springboot后端管理框架(前端vue),有兴趣的 ...

  5. 转:使用memc-nginx和srcache-nginx模块构建高效透明的缓存机制

    原文地址:http://blog.codinglabs.org/articles/nginx-memc-and-srcache.html 为了提高性能,几乎所有互联网应用都有缓存机制,其中Memcac ...

  6. [转] 使用memc-nginx和srcache-nginx模块构建高效透明的缓存机制

    为了提高性能,几乎所有互联网应用都有缓存机制,其中Memcache是使用非常广泛的一个分布式缓存系统.众所周知,LAMP是非常经典的Web架构方式,但是随着Nginx的 成熟,越来越多的系统开始转型为 ...

  7. SpringBoot多模块项目打包问题

    项目结构图如下: 在SpringBoot多模块项目打包时遇见如下错误: 1.repackage failed: Unable to find main class -> [Help 1] 解决步 ...

  8. springboot多模块开发以及整合dubbo\zookeeper进行服务管理

    之前研究了springboot单工程的使用,参考git地址:https://github.com/qiao-zhi/springboot-ssm 下面研究springboot多模块开发的过程. 1.模 ...

  9. springboot学习之构建简单项目搭建

    概述 相信对于Java开发者而言,spring和springMvc两个框架一定不陌生,这两个框架需要我们手动配置的地方非常多,各种的xml文件,properties文件,构建一个项目还是挺复杂的,在这 ...

  10. 架构实战项目心得(十四):spring-boot结合Swagger2构建RESTful API测试体系

    一.添加依赖: <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-s ...

随机推荐

  1. 微信小程序把玩(二十四)toast组件

    原文:微信小程序把玩(二十四)toast组件 toast消息提示框,可用在提示一些信息,比如清楚缓存给用户一个友好的提示!或操作一些请求不想让用户有什么操作,toast也可以做到因为toast显示时其 ...

  2. C#有哪几种定时器

    1.定义在System.Windows.Forms里2.定义在System.Threading.Timer类里3.定义在System.Timers.Timer类里 System.Windows.For ...

  3. MongoDB数据库和集合的基本操作

    非关系型数据库 命令区分大小写:命令结束符为回车(与MySQL不同之处) mongodb配置 sudo service mongodb start mongo mongodb基本概念 集合对应于关系型 ...

  4. spring bean 加载过程(spring)

    以classpathXmlApplication为例 入口方法包含3个部分, public ClassPathXmlApplicationContext(String[] configLocation ...

  5. linux下编译qt5.6.0静态库(使用./configure --help来看看都有哪些参数。超详细,有每一个模块的说明。如果改变了安装的目录,需要到安装目录下的bin目录下创建文件qt.conf)(乌合之众)good

    linux下编译qt5.6.0静态库 linux下编译qt5.6.0静态库 configure生成makefile 安装选项 Configure选项 第三方库: 附加选项: QNX/Blackberr ...

  6. Qt中使用QSqlDatabase::removeDatabase()的正确方法 good

    如果你用过Qt的QSqlDatabase的话,多半会对下面的警告信息感兴趣: QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_defau ...

  7. jmeter报告分析工具

    一直以来做性能测试都是用jmeter和LR,当然还有一些自己写测试脚本,LR不用说,分析结果那个组件杠杠的!但是jmeter毕竟是开源的,所以分析查看结果不像LR那样自带图形神马的,虽然可以自己写脚本 ...

  8. Python连载13-shutile模块(续)和zipfile模块

    一.shutil模块(续) 1.函数:upack_archive() (1)用法:解包操作 (2)格式:shutil.unpack_archive("归档文件地址“,”解包之后的地址“) ( ...

  9. 基于 Kong 和 Kubernetes 的 WebApi 多版本解决方案

    前言 大家好,很久没有写博客了,最近半年也是比较的忙,所以给关注我的粉丝们道个歉.去年和朱永光大哥聊的时候提了一下我们的这个方案,他说让我有空写篇博客讲一下,之前是非常的忙,所以这次趁着有些时间就写一 ...

  10. springboot如何读取自定义配置项

    我们springboot项目有自己默认的配置文件,一般地由application.yml和bootstrap.yml组成,前者是模块的配置,后者是微服务的配置,后台比前者先被框架加载. 我们有时需要自 ...