2 springboot多模块项目
一般来说创建一个springboot工程基本就可以了,但是有的时候可能需要将业务模块逻辑划分,每块业务模块都是一个工程,下边演示下多模块进行开发
目录结构
...somefun
......somefun-web
......somefun-service-system
.........somefun-system-api
.........somefun-system-service
somefun项目父工程
somefun-web 项目中的web模块(springboot项目)
somefun-service-system system服务模块父工程
somefun-system-api system服务模块api部分 存放server接口和dto 非常简单的一层
somefun-system-service system服务具体逻辑实现模块
pom文件
这种开发的方式本质上还是将多个模块以jar包的方式引用进来,所以引用关系非常简单
somefun-web 引用 somefun-system-api和 somefun-system-service即可。
创建web项目时相当于独立模块创建,所以讲web模块中的pom文件部分内容移动到父项目中,具体修改后的内容如下
somefun 的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">
<modelVersion>4.0.0</modelVersion> <groupId>com.zhj</groupId>
<artifactId>somefun</artifactId> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/>
</parent> <packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>somefun-service-system</module>
<module>somefun-web</module>
</modules> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build> </project>
somefun-web 的pom.xml文件
我们需要在pom中指定下启动类文件
<?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>somefun</artifactId>
<groupId>com.zhj</groupId>
<version>1.0-SNAPSHOT</version>
</parent> <modelVersion>4.0.0</modelVersion> <groupId>com.zhj</groupId>
<artifactId>somefun-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>somefun-web</name> <dependencies> <dependency>
<groupId>com.zhj</groupId>
<artifactId>somefun-system-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency> <dependency>
<groupId>com.zhj</groupId>
<artifactId>somefun-system-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.zhj.somefun.web.SomefunWebApplication</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build> </project>
somefun-system-api 的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>somefun-service-system</artifactId>
<groupId>com.zhj</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<version>1.0-SNAPSHOT</version>
<artifactId>somefun-system-api</artifactId>
</project>
somefun-system-service 的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>somefun-service-system</artifactId>
<groupId>com.zhj</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>somefun-system-service</artifactId>
<dependencies> <dependency>
<groupId>com.zhj</groupId>
<artifactId>somefun-system-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies> </project>
我们添加下测试代码
somefun-system-api 模块
package com.zhj.somefun.system.api.dto;
public class TestDto{
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
private Integer age;
package com.zhj.somefun.system.api.service;
import com.zhj.somefun.system.api.dto.TestDto;
import java.util.List;
public interface TestService {
public List<TestDto> getTestList();
}
somefun-system-service 模块
package com.zhj.somefun.system.service.impl; import com.zhj.somefun.system.api.dto.TestDto;
import com.zhj.somefun.system.api.service.TestService;
import org.apache.el.stream.Stream;
import org.springframework.stereotype.Service; import java.util.ArrayList;
import java.util.List; @Service
public class TestServiceImpl implements TestService { @Override
public List<TestDto> getTestList() {
List<TestDto> list = new ArrayList<>();
TestDto dto = new TestDto();
dto.setAge(18);
dto.setId(1);
dto.setName("姓名");
list.add(dto);
return list;
}
}
somefun-web模块
package com.zhj.somefun.web.controller; import com.zhj.somefun.system.api.dto.TestDto;
import com.zhj.somefun.system.api.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController
public class Testcontroller { @Autowired
TestService testService; @GetMapping("/sayhello")
public String sayHello() {
return "Hello Word";
} @GetMapping("/getlist")
public List<TestDto> getlist(){
return testService.getTestList();
}
}
package com.zhj.somefun.web; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan; @SpringBootApplication (scanBasePackages={"com.zhj.somefun"})
public class SomefunWebApplication { public static void main(String[] args) {
SpringApplication.run(SomefunWebApplication.class, args);
}
}
SpringBoot在启动过程中会自动扫描启动类所在包名下的注解,如果不在启动类的包名下边的话就不会扫描到
咱们的启动类SomefunWebApplication所在的报名是com.zhj.somefun.web,system-service中的服务所在的报名为com.zhj.somefun.system.service 类并不在com.zhj.somefun.web下,所以启动类在扫描注解的时候不会扫描到service类,所以需要修改启动类代码。
1.将SomefunWebApplication类移动到com.zhj.somefun包下,这样扫描的时候可以将服务一起扫描到。
2.在启动类加上注解指定扫描包 scanBasePackages={"com.zhj.somefun"}
我们采用注解的方式
启动程序 访问域名 http://localhost:8080/getlist
会看到返回值:
[{"id":1,"name":"姓名","age":18}]
多模块程序打包
我们使用idea工具进行打包
选择父项目somefun
双击package

打包成功:

找到target目录中会发现已经打包好的jar文件

cmd切换到此目录,执行命令
java -jar somefun-web-0.0.1-SNAPSHOT.jar

我们再次访问域名 http://localhost:8080/getlist
会看到返回值:
[{"id":1,"name":"姓名","age":18}]
2 springboot多模块项目的更多相关文章
- SpringBoot多模块项目打包问题
项目结构图如下: 在SpringBoot多模块项目打包时遇见如下错误: 1.repackage failed: Unable to find main class -> [Help 1] 解决步 ...
- 2017-09-26 发布 SpringBoot多模块项目实践(Multi-Module)
https://segmentfault.com/a/1190000011367492?utm_source=tag-newest 2017-09-26 发布 SpringBoot多模块项目实践(Mu ...
- 使用IDEA构建Spring-boot多模块项目配置流程
使用IDEA构建Spring-boot多模块项目配置流程 1.创建项目 点击Create New Project 在左侧选中Spring Initializer,保持默认配置,点击下一步. 在Grou ...
- 记Spring搭建功能完整的个人博客「Oyster」全过程[其二] Idea中Maven+SpringBoot多模块项目开发的设计和各种坑(模块间依赖和打包问题)
大家好嘞,今天闲着没事干开写写博客,记录一下Maven+SpringBoot的多模块设计和遇到的坑. 多模块设计 简单说明一下截止目前的需求: 需要RESTful API:对文章.标签.分类和评论等的 ...
- Java秒杀系统实战系列~构建SpringBoot多模块项目
摘要:本篇博文是“Java秒杀系统实战系列文章”的第二篇,主要分享介绍如何采用IDEA,基于SpringBoot+SpringMVC+Mybatis+分布式中间件构建一个多模块的项目,即“秒杀系统”! ...
- springboot 多模块项目创建
1.File>new>project 直接点击next 2.输入groupId .artifactId 3.选择项目保存路劲 finish 4.成功创建多模块项目的根模块 5.创建子 ...
- 使用Gradle构建springboot多模块项目,并混合groovy开发
idea设置本地gradle 打包: build.gradle //声明gradle脚本自身需要使用的资源,优先执行 buildscript { ext { springBootVersion = ' ...
- docker部署 springboot 多模块项目+vue
之前学习了docker,今天就来试试将这个项目打包成docker镜像并通过运行一个镜像来运行项目.这里使用的项目是el-admin.是一个开源的springboot后端管理框架(前端vue),有兴趣的 ...
- IDEA SpringBoot多模块项目搭建详细过程(转)
文章转自https://blog.csdn.net/zcf980/article/details/83040029 项目源码: 链接: https://pan.baidu.com/s/1Gp9cY1Q ...
- Idea创建一个springboot多模块项目
一.创建空Maven项目 二.左边选择maven,右边可以什么不选,直接next: 三.填写artifactId,点击next直到finish 四.finish后,idea会生成如下结果模块,删除sr ...
随机推荐
- Java的入门知识和环境配置
JVM(Java Virtual Machine)Java虚拟机 JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的. JAVA语言非常重要 ...
- 【2019】OCP 12c 062题库更新大量新题-7
7.daily_ords_lst is created in locally managed tablespace ORDERS_TBS which uses automatic segment sp ...
- BZOJ 1305--[CQOI2009]dance跳舞(最大流)
1305: [CQOI2009]dance跳舞 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 4150 Solved: 1792[Submit][St ...
- 关于Boolean()
Boolean(value); 如果省略 value 参数,或者设置为 0.-0.null."".false.undefined 或 NaN,则该对象设置为 false. 否则设置 ...
- git小白使用教程(一)
本文所涉及命令基本可以涵盖日常开发场景, 对于开发者平时很少使用的命令不再列举,这样不至于让刚刚使用git的小伙伴们看的脑袋大...如有特殊使用可以联系我单独回复. 首先通过一张图了解git的工作流程 ...
- Oracle性能问题sql调优脚本集
---------------------------------------------------------------------------------------------------- ...
- Django服务器启动时指定端口和IP方法
python manager.py runserver 127.0.0.1:8001
- execvp php-fpm reload使用的函数
php重启 本质上是调用 execvp("/usr/local/php/sbin/php-fpm"); execvp就是用一个新的进程把自己替换掉,一个进程一旦调用exec类函数, ...
- Linux 基础命令 持续更新中...
1.ls 显示当前文件/文件夹 显示文件大小: ls -lh 显示隐藏文件: ls -a 显示文件详细信息: ls -l (ll)2.pwd 显示当前所在路径 cat 显示当前文件下所有内容3.cd ...
- RabbitMQ : 几种Exchange 模式
AMQP协议中的核心思想就是生产者和消费者隔离,生产者从不直接将消息发送给队列.生产者通常不知道是否一个消息会被发送到队列中,只是将消息发送到一个交换机.先由Exchange来接收,然后Exchang ...