场景
SpringBoot系统中,经常会有在多个服务中需要用到同样的一些实体类,比如用户、文件上传或工具类,当需求变动而需要修改时,就不得不每个子服务都修改一遍,其缺点一是效率低下,其二是难以保证同步。
解决方案
1.新建一个项目,将其他服务的公共类及共有依赖抽取到该项目;
<?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>
<artifactId>fire_module</artifactId>
<version>1.0.0</version>
<groupId>com.xakj</groupId>
<packaging>jar</packaging>
<name>fire_module</name>
<description>通用模块</description>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.2.Final</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>com.xiaoleilu</groupId>
<artifactId>hutool-all</artifactId>
<version>3.0.9</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<!--<mainClass>com.xakj.app.Application</mainClass> -->
<encoding>UTF-8</encoding>
<compilerArguments>
<verbose />
<bootclasspath>${java.home}\lib\rt.jar;${java.home}\lib\jce.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
2.打包安装到本地仓库;
3.将其他项目的重复代码和依赖删除;
4.引入新项目依赖。
<!-- 共用模块 -->
<dependency>
<groupId>com.xakj</groupId>
<artifactId>fire_module</artifactId>
<version>1.0.0</version>
</dependency>