SpringBoot实现一个项目多模块,多主启动模块,每个模块多环境配置文件

我们在日常开发过程中,在新建一个项目中,我们经常会将一个项目按照功能性、模块等将一个项目分成多个子项目,这些子项目都包含在主项目中。
对于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

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值