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. IIS:IIS 8.5下设置404错误页

    IIS版本:IIS 8.5 问题描述 搭建一个测试网站,总共就2个页面(index.php和404.php),默认首页为:index.php 当访问index.php和404.php的时候,IIS服务 ...

  2. 二、LINQ之查询表达式基础

    1.查询是什么? 查询是一组指令,描述要从给定数据源(或源)检索的数据以及返回的数据应具有的形状和组织.查询表达式和它所产生的结果不同.

  3. Azure Redis 缓存使用注意事项与排查问题文档整理

    StackExchange.Redis 使用名为 synctimeout 的配置设置进行同步操作,该设置的默认值为 1000 毫秒. 如果同步调用未在规定时间内完成,StackExchange.Red ...

  4. C#基础篇五值类型和引用类型

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace P01M ...

  5. TCP/IP 笔记 - 概述

    Effective communication depends on the use of a common language. 有效沟通取决于使用共同的语言 . TCP/IP协议族 一系列相关协议的 ...

  6. es-04-mapping和setting的建立

    mapping和setting, 使用java客户端比较难组装, 可以使用python或者scala 这儿直接在kibana中进行DSL创建 1, mapping 创建索引的时候, 可以事先对数据进行 ...

  7. top 动态查看进程

    top 统计信息前五行是系统整体的统计信息 1.第一行是任务队列信息 同uptime质性命令结果一样. 06:47:11 up 6:39, 3 users, load average: 0.00, 0 ...

  8. SPI(Service Provider Interface)机制

    JAVA SPI 约定如下:当服务的提供者提供了服务接口的一种实现之后,在jar包的META-INF/services/ 目录中同时创建一个以服务接口命名的文件,该文件中的内容就是实现该服务接口的具体 ...

  9. Prim Algoritm(最小生成树)

    Prim Algorithm.这个算法可以分为下面几个步骤: 将顶点集V分成两个集合A和B,其中集合A表示目前已经在MST中的顶点,而集合B则表示目前不在MST中的顶点. 在B寻找与集合A连通的最短的 ...

  10. MySQL Replication之主从切换

    在生产环境中,我们的架构很多都是一主多从.比如一个主数据库服务器M,两个从数据库服务器S1,S2同时指向主数据库服务器M.当主服务器M因为意外情况宕机,需要将其中的一个从数据库服务器(假设选择S1)切 ...