Spring Boot -05- 多模块结构项目构建与测试(详细图文教程)IDEA 版

百度很多博客都不详细,弄了半天才把 Spring Boot 多模块项目构建开发整的差不多,特地重新创建配置,记录一下,也分享给有需要的人

本篇也会非常详细的介绍涉及的基础知识点,更多都写在注释上了

先放成功截图:

(1)项目结构:



(2)启动:



(3)测试主子模块:



(4)测试子模块依赖:

第一步:创建父模块,子模块

(1)打开创建项目窗口,点击 Create New Project

(2)填写

(3)填写

(4)Maven 自动导入

(5)没有 iml 文件,请重新创建,请看截图:

(6)在父项目上,右键新建模块

(7)这里用 xpwi-main



(8)填写

(提示:这个名字建议个上面的一致,不然项目名和文件夹名不一致哈)

(9)依次类推,创建自己需要的子模块

(10)然后去创建包,示例:

第二步:父 pom.xml 配置

请查看详细注释,根据自己的项目修改

注意:

  • 子模块那个是不需要配置的,由创建时自动生成
  • dependencyManagement 也在注释上讲解了
<?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> <!--基本信息-->
<description>Spring Boot 多模块构建示例</description>
<groupId>com.xpwi</groupId>
<artifactId>first-springboot</artifactId>
<!--父 pom 的 packing 必须为 pom,请核查-->
<packaging>pom</packaging>
<version>1.0.0</version> <!--指定整个项目的父项目-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent> <!--模块:这里声明多个子模块 -->
<!--注意,这个不用手动自己去写,因为创建的时候是会自动生成的-->
<modules>
<module>xpwi-test</module>
<module>xpwi-main</module>
<module>xpwi-login</module>
</modules> <!--属性变量配置-->
<properties>
<java.version>1.8</java.version>
</properties> <!--加载依赖管理-->
<!--注意:如果使用dependencyManagement,只是对版本进行管理,不会直接引入jar -->
<!--是为了在这里配置版本,为了让子模块pom或者本pom的直接dependencies不单独配置版本 -->
<!--如果没有版本,会先到这里找版本号,以免版本冲突 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement> <!--公共模块加载,非公共模块请一般放在子pom进行加载-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<!--插件-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.1.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build> </project>

第三步:子 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>first-springboot</artifactId>
<groupId>com.xpwi</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>xpwi-main</artifactId>
<dependencies> <!--这个已经移动到父 pom 了-->
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-web</artifactId>-->
<!--</dependency>--> <!--现在是去加载自己创建的模块-->
<!--就是加载子模块对子模块的依赖-->
<dependency>
<groupId>com.xpwi</groupId>
<artifactId>xpwi-test</artifactId>
<version>1.0.0</version>
</dependency> <dependency>
<groupId>com.xpwi</groupId>
<artifactId>xpwi-login</artifactId>
<version>1.0.0</version>
</dependency> </dependencies> </project>

第四步:创建启动类,本模块测试类

(1)创建两个文件

(2)启动类 App.java 源代码:

package com.xpwi.main;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RestController; import java.util.Date; /**
* 描述:Spring Boot 多模块测试项目
* @author Xiao Pengwei
* @since 2019-03-25
*/ @SpringBootApplication
@RestController
//扫描 main,test 模块中的下的所有包
//在 pom 加载子模块依赖才可以骚包
@ComponentScan({"com.xpwi.main","com.xpwi.test"})
public class App { public static void main(String[] args) {
//启动 Web 容器
SpringApplication.run(App.class, args);
System.out.println("[启动成功]"+new Date());
}
}

(3)MainController.java 文件源代码:

package com.xpwi.main.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* 描述:本模块测试类
* @author Xiao Pengwei
* @since 2019-03-25
*/ @RestController
@RequestMapping("/main")
public class MainController { //请求映射,当请求 /main/test 时执行该方法
@RequestMapping("/test")
public String home() {
return "Hello Main!";
}
}

第五步:子模块依赖测试类

(1)结构:

(2)TestController 文件源代码:

package com.xpwi.test.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* 描述:子模块依赖测试
* @author Xiao Pengwei
* @since 2019-03-25
*/ @RestController
@RequestMapping("test")
public class TestController { //请求映射,当请求 /home 时执行该方法
@RequestMapping("/test")
public String home() {
return "Hello Test!";
}
}

第六步:启动与测试

(1)启动:



(2)测试主子模块:



(3)测试子模块依赖:

自定义 banner

一键加技术朋友群

Spring Boot -05- 多模块结构项目构建与测试(详细图文教程)IDEA 版的更多相关文章

  1. [转] 使用Spring Boot和Gradle创建项目

    Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的 ...

  2. spring boot / cloud (三) 集成springfox-swagger2构建在线API文档

    spring boot / cloud (三) 集成springfox-swagger2构建在线API文档 前言 不能同步更新API文档会有什么问题? 理想情况下,为所开发的服务编写接口文档,能提高与 ...

  3. 八个开源的 Spring Boot 前后端分离项目,一定要收藏!

    八个开源的 Spring Boot 前后端分离项目 最近前后端分离已经在慢慢走进各公司的技术栈,不少公司都已经切换到这个技术栈上面了.即使贵司目前没有切换到这个技术栈上面,我们也非常建议大家学习一下前 ...

  4. spring cloud和spring boot两个完整项目

    spring cloud和spring boot两个完整项目 spring cloud 是基于Spring Cloud的云分布式后台管理系统架构,核心技术采用Eureka.Fegin.Ribbon.Z ...

  5. Spring Boot 创建hello world项目

    Spring Boot 创建hello world项目 1.创建项目 最近在学习Spring Boot,这里记录使用IDEA创建Spring Boot的的过程 在1出勾选,选择2,点击Next 这里填 ...

  6. Spring Boot 2+gRPC 学习系列1:搭建Spring Boot 2+gRPC本地项目

    Spring Boot 2+gRPC 学习系列1:搭建Spring Boot 2+gRPC本地项目 https://blog.csdn.net/alinyua/article/details/8303 ...

  7. 10个Spring Boot快速开发的项目,接私活利器(快速、高效)

    本文为大家精选了 码云 上优秀的 Spring Boot 语言开源项目,涵盖了企业级系统框架.文件文档系统.秒杀系统.微服务化系统.后台管理系统等,希望能够给大家带来一点帮助:) 1.项目名称:分布式 ...

  8. Spring Boot + JPA 多模块项目无法注入 JpaRepository 接口

    问题描述 Spring Boot + JPA 多模块项目,启动报异常: nested exception is org.springframework.beans.factory.NoSuchBean ...

  9. SpringBoot项目构建、测试、热部署、配置原理、执行流程

    SpringBoot项目构建.测试.热部署.配置原理.执行流程 一.项目构建 二.测试和热部署 三.配置原理 四.执行流程

随机推荐

  1. vmworkstation安装unbuntu server 网络配置:NAT模式

    之前安装虚拟机测试环境的时候,习惯了使用桥接模式或者仅主机模式:今天偶然发现,其实NAT 模式的网络配置还是挺方便的. 在新建虚拟机的时候,选择网络模式为NAT,虚拟机创建完成之后,在vmworkst ...

  2. 十分钟内在Ubuntu系统上搭建Mono开发环境(Mono软件Ubuntu系统国内镜像源、Mono国内镜像源)

    Mono软件Ubuntu系统国内镜像源.Mono国内镜像源 http://download.githall.cn/repo 替换为国内源(非官方)有利于加快mono的安装速度,一般情况下,完成mono ...

  3. fastjson的JSONArray转化为泛型列表

    背景:一个复杂结构体内部可能有array的数据,例如:{name:"test",cities:[{name:"shanghai",area:1,code:200 ...

  4. Selenium自动化测试Python五:WebDriver设计模式

    WebDriver 设计模式 欢迎阅读WebDriver进阶讲义.本篇讲义将会重点介绍Selenium WebDriver 自动化框架的设计,着重使用Page Object设计模式,以及使用HTML测 ...

  5. 关于 ASP.NET Web 应用中 async/await 注意问题

    System.NullReferenceException: Object reference not set to an instance of an object. at System.Web.T ...

  6. 记一次解决CSS定位bug思路

    事因 网站中的遮罩层大都有一个问题,就是在这个遮罩层中滑动,里面的内容也会跟着滑动,我是这样想的,既然都有这个问题,干脆写一个通用的插件出来,省的每个还得单独处理.如果是单独处理这个问题是比较好解决的 ...

  7. Android so 文件进阶<二> 从dlsym()源码看android 动态链接过程

    0x00  前言 这篇文章其实是我之前学习elf文件关于符号表的学习笔记,网上也有很多关于符号表的文章,怎么说呢,感觉像是在翻译elf文件格式的文档一样,千篇一律,因此把自己的学习笔记分享出来.dls ...

  8. 使用CSS定位元素实现水平垂直居中效果

    总结一下平时使用CSS技巧使元素达到水平居中效果 相对定位(或绝对定位)实现水平垂直居中: element{ position:relative; /*这个属性也可以是absolute*/ width ...

  9. CUBA Studio 8.0 发布,企业级应用开发平台

    CUBA Platform 是一款开源且免费的企业级应用开发框架,已有将近10年的发展历史,由俄罗斯的 Haulmont  公司开发,CUBA Platform 近期将正式登陆中国,将提供中文网站.中 ...

  10. Java源码阅读(不断补充)

    java.util.LinkedList LinkedList是实现了List接口的双链表实现,拥有list的所有方法并且允许所有元素(包括null). 双向链表也叫双链表,是链表的一种,它的每个数据 ...