我们在日常开发过程中,在新建一个项目中,我们经常会将一个项目按照功能性、模块等将一个项目分成多个子项目,这些子项目都包含在主项目中。
对于Spring、SpringBoot中实现多模块化,网上有很多教程,这里就不在讲述了。
本次主要讲的是如何在一个项目中能够有两个Application,即有两个启动类,启动不同的启动类,能够访问不同的功能,另外我们开发项目时,需要将配置分为开发环境、测试环境、生产环境,对于不同环境的参数配置,我们虽然可以将所有配置配置在一个文件中,在打包时根据环境屏蔽放开对应的参数配置,但是这样不仅繁琐,而且经常会导致不必要的错误。
另外SpringBoot/Spring 均有在打包时选择不同的配置文件的实现,但是SpringBoot项目实现不同环境的配置只能够在主项目中进行区分,那么如果子项目想要根据环境配置参数,也必须在主项目中配置,这样的话耦合度比较高,不适合扩展,另外如果是两个Application使用同一个子模块,那么这两个项目均要配置相同的参数配置,非常麻烦。所以该篇将的就是如何处理该问题。
这个项目主要结构如下图
从上图可以看出,这一个项目中包含了controller、scheduled两个启动类项目,这两个启动类项目均引用service、service又引用了dao项目。
我们想要实现的结果是service的不同环境的配置信息,放在其自己的配置文件中,而不是放在controller或者scheduled的配置文件中,因为那样的话我们得配置两边,非常不灵活。
如上图所示的配置文件就可以实现非常灵活的配置,自己模块的参数配置,仅仅配置在自己的文件中,到时候拆分项目、引用项目都非常的容易了。
废话不多说,直接贴代码:
parent
因为这里的项目均为一个项目的多个子模块,我们知道其有个父模块的,所以我们可以把很多公用的配置放在父模块中,那样的话子模块的pom.xml配置的东西就非常的少了,方便阅读。
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>wechat-qingtaixiaozhu</groupId>
<artifactId>qingtaixiaozhu</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<!-- 子模块-->
<modules>
<module>qingtaixiaozhu-service</module>
<module>qingtaixiaozhu-dao</module>
<module>qingtaixiaozhu-coutroller</module>
<module>qingtaixiaozhu-scheduled</module>
</modules>
<!-- 引用springBoot父项目-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/>
</parent>
<!--统一管理依赖的版本号-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<type>pom</type>
<scope>import</scope>
<version>1.5.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.1.RELEASE</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<!-- 环境信息 -->
<profiles>
<!-- 标机开发环境 -->
<profile>
<id>dev</id>
<properties>
<activeProfile>dev</activeProfile>
<serviceactive>servicedev</serviceactive>
<businessactive>businessdev</businessactive>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!-- 测试环境 -->
<profile>
<id>test</id>
<properties>
<activeProfile>test</activeProfile>
<serviceactive>servicetest</serviceactive>
<businessactive>businesstest</businessactive>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
<!-- 生产环境 -->
<profile>
<id>product</id>
<properties>
<activeProfile>product</activeProfile>
<serviceactive>serviceproduct</serviceactive>
<businessactive>businessproduct</businessactive>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
</profiles>
</project>
父项目的pom.xml主要是配置了都需要的SpringBoot的相关引用,这样的话我们子项目完全不需要再重复引用了。另外其配置了profiles
属性,这个属性是让我们区分多环境是打包哪些配置文件,其id参数就是我们的要打包环境的一个标识,比如我们打包测试环境我们只需要执行clean install -Ptest
即可,-P后面的值即为这个id值,其中properties
属