Springboot与Maven多环境配置的解决方案
作者:海向 发布时间:2023-11-29 08:53:58
目录
Profile用法
resources
filters
多环境配置解决方案
Profile用法
我们在application.yml中为jdbc.username赋予一个值,这个值为一个变量
jdbc:
username: ${jdbc.username}
Maven中的profiles可以设置多个环境,当我们选择a环境后,<jdbc.username>内的值将替换上述配置文件中的变量
</profiles>
<profile>
<id>a</id>
<properties>
<jdbc.username>root</jdbc.username>
</properties>
<!-- 默认使用此环境 -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
我们查看编译后的application.yml文件,果然变量已经被赋值。我们猜想是否可以利用Profile的这一特性设置开发、测试、生产环境,选择不同环境时使用不同变量,配合Resources和Filter来指定打包内容以及替换变量。
jdbc:
username: root
resources
用来操作编译文件
filters
过滤器,设置过滤器的资源将会对同名变量进行赋值(被赋值的资源文件需要设置filtering为true)
多环境配置解决方案
网上大多数都是分为application-dev.xml、application-test.xml、application-prod.xml三个文件,可是我们在真实项目开发中,将会用到很多各式各样的文件(例如log4j的配置文件),它们在不同环境中应该也是不同的配置,不能在测试和生产环境使用同一个配置文件。所以我们将分为三个文件夹分别代表开发环境、测试环境、生产环境,他们里面的配置文件种类一致但是内容不一样。选择完当前环境后,打的jar包只包含当前环境文件夹下的配置文件。
├─main
│ ├─java
│ │ └─......
│ └─resources
│ ├─dev
│ │ └─config
│ │ │ └─mq.yml
│ │ │ └─redis.yml
│ │ └─application-dev.yml
│ ├─prod
│ │ └─config
│ │ │ └─mq.yml
│ │ │ └─redis.yml
│ │ └─application-prod.yml
│ └─test
│ │ └─config
│ │ │ └─mq.yml
│ │ │ └─redis.yml
│ │ └─application-test.yml
│ └─application.yml
│ └─a.xml
└─test
└─java
└─......
dev下的config下的mq.yml
mq: mq-dev
dev下的config下的redis.yml
redis: redis-dev
dev下的application-dev.yml
profiles.active:
dev
port: dev-port
application.yml
spring:
profiles:
active: ${profiles.active}
port: ${port}
查看编译后的结果
其中application.yml中变量已经被替换为
spring:
profiles:
active: dev
port: dev-port
完整的pom.xml
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<!--使用默认的变量分割符即${}-->
<configuration>
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
<!-- 测试文件的编译路径设置 -->
<testResources>
<testResource>
<!--这里是关键! 根据不同的环境,把对应文件夹里的配置文件打包-->
<directory>src/main/resources</directory>
<includes>
<include>application.yml</include>
</includes>
<filtering>true</filtering>
</testResource>
<testResource>
<!--这里是关键! 根据不同的环境,把对应文件夹里的配置文件打包-->
<directory>src/main/resources/${profiles.active}</directory>
<includes>
<include>**/*.yml</include>
</includes>
<filtering>false</filtering>
</testResource>
</testResources>
<resources>
<resource>
<!--打包该目录下的 application.yml -->
<directory>src/main/resources</directory>
<includes>
<include>application.yml</include>
</includes>
<!-- 启用过滤 即该资源中的变量将会被过滤器中的值替换 -->
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<!-- ${profiles.active}由profile提供 -->
<directory>src/main/resources/${profiles.active}</directory>
<includes>
<include>**/*.yml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<!-- 定义 filter,即该资源中的值将会用来替换同名属性(设置 filtering 为 true 的资源中的属性)-->
<filters>
<filter>
src/main/resources/${profiles.active}/application-${profiles.active}.yml
</filter>
</filters>
</build>
<profiles>
<profile>
<!-- 本地开发环境 -->
<id>dev</id>
<properties>
<profiles.active>dev</profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 测试环境 -->
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
</properties>
</profile>
<profile>
<!-- 生产环境 -->
<id>prod</id>
<properties>
<profiles.active>prod</profiles.active>
</properties>
</profile>
</profiles>
来源:https://www.cnblogs.com/haixiang/p/12451703.html
猜你喜欢
- Spring Cloud Gateway(以下简称 SCG)做为网关服务,是其他各服务对外中转站,通过 SCG 进行请求转发。在请求到达真正
- 现在的项目基本上都是java web项目,所以导入jar包会出现问题,主要介绍一下java项目与javaweb项目的区别:java项目:在c
- ActiveMQ是Apache的一个开源项目,它是一个功能强劲的开源消息总线,也是一个中间件产品,它是JMS的一个实现。在介绍ActiveM
- 配置不生效的解决办法注意:如果配置不生效,则说明spring优先加载了其他配置:解决办法:添加启动参数 -Dlogging.config=c
- 本篇紧接上一篇内容继续,还是从继承里的细节开始1.代码块初始化关于代码块的定义和使用因为之前已经进行过介绍,所以这里就不再赘述,我们所关注的
- 1. matlab的lp2lp函数的作用去归一化 H(s) 的分母2. matlab的lp2lp函数的使用方法[z, p, k]=butta
- 本文实例为大家分享了C语言实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下IDE用的是 VS2019先看效果 代码全览game.
- 可以静态绑定数据源,这样就自动为DataGridView控件添加 相应的行。假如需要动态为DataGridView控件添加新行,方法有很多种
- 自定义 webflux 容器配置配置代码@Componentpublic class ContainerConfig extends Rea
- 我们知道HashMap集合是允许存放null值的hashMap是根据key的hashCode来寻找存放位置的,那当key为null时, 怎么
- 前言前阵子有同学反馈Flutter中的http请求无法通过fiddler抓包,作者喜欢使用Charles抓包工具,于是抽时间写了个小demo
- 1,添加依赖在project的build.gradle文件中添加dependencies { classpath 'co
- 一、获取接口请求的数据可以在Interceptor的afterCompletion中实现但是要重写RequestWrapper代码记录如下:
- Java代码package com.zzx.controller;import com.zzx.model.User;import org.
- 自定义TypeHandler映射JSON类型为List1. 实体类这里只展示需要映射的字段,分别在所需映射的字段和实体类上添加注解。&nbs
- 前言:本文主要讲解以c语言编写猜数字游戏,目的是介绍C语言中的循环和分支的具体用法。一:猜数字游戏基本介绍&对程序预期.猜数字游戏,
- 最近在研究android自定义控件属性,学到了TypedArray以及attrs。大家也可以结合《理解Android中的自定义属性》这篇文章
- JdbcTypeInterceptor运行时自动添加 jdbcType 属性 * 签名@Intercepts({
- spring容器是负责实例化、配置、组装组件的容器。容器的配置有很多,常用的是xml、Java注解和Java代码。在spring中Ioc容器
- 前言本文主要给大家介绍的是java虚拟机的故障处理工具,文中提到这些工具包括:名称主要作用jpsJVM process Status Too