本准备写点代码实例放到网站,太多的模板,反而每次新建工程的时候很麻烦。于是准备把这个章节的内容提前先讲讲。正好把这个代码也管理起来。话说这个多模块功能还挺爽。

写过 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】下一步。

  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



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

IDEA 创建 Spring Boot 多模块项目(Multi Modules)的更多相关文章

  1. Spring Boot 多模块项目创建与配置 (一) (转)

    Spring Boot 多模块项目创建与配置 (一) 最近在负责的是一个比较复杂项目,模块很多,代码中的二级模块就有9个,部分二级模块下面还分了多个模块.代码中的多模块是用maven管理的,每个模块都 ...

  2. Spring Boot 多模块项目创建与配置 (一)

    最近在负责的是一个比较复杂项目,模块很多,代码中的二级模块就有9个,部分二级模块下面还分了多个模块.代码中的多模块是用maven管理的,每个模块都使用spring boot框架.之前有零零散散学过一些 ...

  3. Spring Boot 多模块项目创建与配置 (转)

    转载:https://www.cnblogs.com/MaxElephant/p/8205234.html 最近在负责的是一个比较复杂项目,模块很多,代码中的二级模块就有9个,部分二级模块下面还分了多 ...

  4. Spring boot 多模块项目 + Swagger 让你的API可视化

    Spring boot 多模块项目 + Swagger 让你的API可视化 前言 手写 Api 文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文档更新交流不及时. 接口返回结果不 ...

  5. Maven 搭建spring boot多模块项目(附源码),亲测可以,感谢原创

    原创地址:https://segmentfault.com/a/1190000005020589 我的DEMO码云地址,持续添加新功能: https://gitee.com/itbase/Spring ...

  6. Maven 搭建spring boot多模块项目

    Maven 搭建spring boot多模块项目 备注:所有项目都在idea中创建 1.idea创建maven项目 1-1: 删除src,target目录,只保留pom.xml 1-2: 根目录pom ...

  7. spring boot:多模块项目生成jar包(spring boot 2.3.3)

    一,多模块项目的优点: 1,为什么要使用多模块项目? 相比传统的单体工程,使用Maven的多模块配置, 有如下优点: 帮助项目划分模块,鼓励重用, 防止POM变得过于庞大, 方便某个模块的构建,而不用 ...

  8. 创建Spring Boot微服务项目

    创建一个测试用的微服务项目HelloWorld 创建项目 编写服务代码 @RestController public class HelloWorld { @RequestMapping(" ...

  9. spring boot多模块项目找不到类

    项目结构 mapper依赖pojo, service依赖mapper和pojo portal依赖pojo和service. 全都是maven模块 <groupId>com.haitian& ...

随机推荐

  1. 洛谷 P5569 [SDOI2008]石子合并 GarsiaWachs算法

    石子合并终极通用版 #include<bits/stdc++.h> using namespace std ; ]; int n,t,ans; void combine(int k) { ...

  2. 实现排行榜神器——redis zset

    需求:假如现在需要搞个 “运动消耗卡路里排行榜”,例似微信步数排名,显示排名前20人的信息和消耗的卡里路,怎样实现排序? 一般思路:存储信息,然后数据库查询,排序?(假如有几十万人参与排名,这样查my ...

  3. 查看ie版本

    window10系统还好说,一般都是IE11版本,其他系统或服务器看=查看ie版本就很烦 方法一:按组合键  ALT+H 打开“帮助”,再按 A 选择“关于Internet Explorer” 方法二 ...

  4. eclipse快速创建一个Spring Boot应用

    1,创建一个空的maven项目 2,添加parent <parent> <groupId>org.springframework.boot</groupId> &l ...

  5. windows 下安装memcache拓展

    Windows下安装memcached (linux 接下来会继续 学习) 以管理员身份进入CMD 模式,具体方法:C:/windows/system32 管理员身份打开cmd.exe memcach ...

  6. Scrapy爬虫基本使用

    一.Scrapy爬虫的第一个实例 演示HTML地址 演示HTML页面地址:http://python123.io/ws/demo.html 文件名称:demo.html 产生步骤 步骤1:建议一个Sc ...

  7. 每天进步一点点------Sobel算子(2)

    转载  http://blog.csdn.net/tianhai110 索贝尔算子(Sobel operator)主要用作边缘检测,在技术上,它是一离散性差分算子,用来运算图像亮度函数的灰度之近似值. ...

  8. Django objects.all()、objects.get()与objects.filter()之间的区别介绍

    前言 本文主要介绍的是关于Django objects.all().objects.get()与objects.filter()直接区别的相关内容,文中介绍的非常详细,需要的朋友们下面来一起看看详细的 ...

  9. itest(爱测试) 4.3.0 发布,开源BUG 跟踪管理 & 敏捷测试管理软件

    itest 简介:查看简介 test 开源敏捷测试管理,testOps 践行者.可按测试包分配测试用例执行,也可建测试迭代(含任务,测试包,BUG)来组织测试工作,也有测试环境管理,还有很常用的测试度 ...

  10. vscode 双击选中用中划线拼接的名称和几个常用的扩展

    左下角点击图标-选择设置,然后搜索editor.wordSeparators,然后去掉中划线-就可以了 `~!@#$%^&*()=+[{]}\|;:'",.<>/? 这样 ...