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 ...
随机推荐
- Delphi - 如何执行Windows、OSX、Linux的外部程序?
毫无疑问,几乎对所有Delphi程序员来说,不用说如何在Windows下如何执行外部程序了!目前Delphi,真的已经很棒了,Delphi有一套和VCL并驾齐驱的图形界面库,叫做"FireM ...
- Python3.5 学习十二 数据库介绍
MYSQL介绍: 主流三种数据库:Oracle.Mysql.Sqlserver Mysql安装和启动: windows 1安装 2启动服务 3进入bin目录,打开命令行 4 mysqladmin -u ...
- jQuery基础笔记(5)
day56 参考:https://www.cnblogs.com/liwenzhou/p/8178806.html#autoid-1-9-5 文档处理 添加到指定元素内部的后面 $(A).append ...
- 2018 Multi-University Training Contest 3
claris出题,orzzzzzz.前一天晚上说是贪心专场,喵喵喵??? 之前clsris说难题扔多校了,据说07,13是女生赛撤下来的题,喵喵喵??? A.Ascending Rating 题目传送 ...
- zoj2893 Evolution(矩阵快速幂)
题意:就是说物种进化,有N种物种,编号是0——N-1,M次进化后,问你编号为N-1的物种有多少数量:其中要注意的就是i物种进化到j物种的概率是p:(那么剩下的不要忘了):所以单位矩阵初始化对角线的值为 ...
- 用 TensorFlow 实现 k-means 聚类代码解析
k-means 是聚类中比较简单的一种.用这个例子说一下感受一下 TensorFlow 的强大功能和语法. 一. TensorFlow 的安装 按照官网上的步骤一步一步来即可,我使用的是 virtua ...
- POJ 1013
#include"string.h"char left[3][7],right[3][7],result[3][5];bool isHeavy(char x ){ int i ...
- Java之集合(二十)LinkedBlockingQueue
转载请注明源出处:http://www.cnblogs.com/lighten/p/7503678.html 1.前言 本章介绍阻塞队列LinkedBlockingQueue,这是一个基于链表的可选长 ...
- Spring 小知识点
一.引入配置文件的方式: 方式一: <context:property-placeholder location="classpath:jdbc.properties,classpat ...
- iptables关键学习总结
iptables技术推荐参考这位仁兄的博客:http://www.zsythink.net/archives/category/%E8%BF%90%E7%BB%B4%E7%9B%B8%E5%85%B3 ...