当前位置: 首页 > news >正文

SpringBoot服务多环境配置

一个项目的的环境一般有三个:开发(dev)、测试(test)、生产(proc),一般对应三套环境,三套配置文件。
像下面这样直接写两个配置文件是不行的。

application.ymlserver:port: 8080
application-dev.ymlspring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTCusername: rootpassword: root

SpringBoot多环境配置有两种方式,一种是在配置文件中指定环境,一种是用maven中指定文件

通过配置文件

使用spring.profiles.active配置项

application.ymlserver:port: 8080
spring:profiles:active: dev
application-dev.ymlspring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTCusername: rootpassword: root

通过maven

通过maven控制环境有个好处就是可以直接通过idea右侧的maven管理功能控制项目环境

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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion>
...<build><resources><resource><directory>src/main/resources</directory><excludes><exclude>application*.yml</exclude></excludes></resource><resource><directory>src/main/resources</directory><filtering>true</filtering><includes><include>application.yml</include><include>application-${env}.yml</include></includes></resource></resources></build><profiles><profile><id>dev</id><activation><!-- 默认激活的环境 --><activeByDefault>true</activeByDefault></activation><properties><env>dev</env></properties></profile><profile><id>prod</id><properties><env>prod</env></properties></profile></profiles></project>
application.ymlspring:profiles:active: @env@
server:port: 8080
application-dev.ymlspring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTCusername: rootpassword: root
http://www.lryc.cn/news/488016.html

相关文章:

  • STM32单片机CAN总线汽车线路通断检测-分享
  • 【环境搭建】使用IDEA远程调试Docker中的Java Web
  • 贴代码框架PasteForm特性介绍之select,selects,lselect和reload
  • STM32G4的数模转换器(DAC)的应用
  • SpringMVC跨线程获取requests请求对象(子线程共享servletRequestAttributes)和跨线程获取token信息
  • 提取repo的仓库和工作树(无效)
  • 力扣整理版七:二叉树(待更新)
  • 基于单片机的多功能环保宠物窝设计
  • HBase 基础操作
  • 小米顾此失彼:汽车毛利大增,手机却跌至低谷
  • PCL 三维重建 a-shape曲面重建算法
  • 【Android】线程池的解析
  • 集群聊天服务器(8)用户登录业务
  • Go语言中的错误嵌套
  • 51单片机基础 06 串口通信与串口中断
  • Elasticsearch:更好的二进制量化(BBQ)对比乘积量化(PQ)
  • 【GNU】gcc -g编译选项 -g0 -g1 -g2 -g3 -gdwarf
  • MySQL【六】
  • 杰发科技AC7801——ADC定时器触发的简单使用
  • VTK知识学习(8)-坐标系统
  • IO流部分串讲
  • Excel——宏教程(2)
  • unity 中 RectTransform 的常用几个属性
  • 项目-摄像
  • 摄像机ISP和DSP的区别?
  • Ubuntu24安装配置NDK
  • 【Next】中间件
  • Vulnhub靶场案例渗透[11]- Momentum2
  • STM32设计防丢防摔智能行李箱-分享
  • Vue Mixin混入机制