首页 > 其他分享 >SpringBoot的yml多环境配置3种方法

SpringBoot的yml多环境配置3种方法

时间:2022-10-12 12:00:09浏览次数:52  
标签:SpringBoot 配置 profiles application dev true yml

方式一:多个yml文件
  步骤一、创建多个配置文件

application.yml      #主配置文件
application-dev.yml  #开发环境的配置
application-prod.yml #生产环境的配置
application-test.yml #测试环境的配置
  步骤二、applicaiton.yml中指定配置
在application.yml中选择需要使用的配置文件(当选择的文件和application.yml文件存在相同的配置时,application.yml中的配置会被覆盖掉)
spring:
 profiles:
   active: dev #需要使用的配置文件的后缀

方式二: 单个yml文件
#激活dev环境配置
spring:
  profiles.active: dev
 
 
# 开发环境配置
spring:
  profiles: dev
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/dev?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
server:
  port: 8080
 
 
# 测试环境配置
spring:
  profiles: test
  datasource:
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    username: root
    password: test
    driver-class-name: com.mysql.jdbc.Driver
server:
  port: 88
 
 
 
# 生产环境配置
spring:
  profiles: prod
  datasource:
    url: jdbc:mysql://localhost:3306/prod?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    username: root
    password: prod
    driver-class-name: com.mysql.jdbc.Driver
 server:
  port: 99

  配置默认的profile为dev,其他环境可以通过指定启动参数来使用不同的profile,比如:
 测试环境:java -jar 项目.jar --spring.profiles.active=test
 生产环境:java -jar 项目.jar --spring.profiles.active=prod

方式三:在pom.xml中指定环境配置
步骤一、创建多个配置文件
application.yml      #主配置文件
application-dev.yml  #开发环境的配置
application-prod.yml #生产环境的配置
application-test.yml #测试环境的配置
步骤二、在application.yml中添加多环境配置属性
#多环境配置
  profiles:
    active: @profiles.active@
步骤三、在pom.xml中指定使用的配置
 <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <!--  默认激活-->
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <profiles.active>dev</profiles.active>
            </properties>
        </profile>
 
        <profile>
            <id>prod</id>
            <properties>
                <profiles.active>prod</profiles.active>
            </properties>
        </profile>
 
        <profile>
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
        </profile>
 </profiles>
<activeByDefault>true</activeByDefault>配置为true则激活对应profile的配置。

或如图所示,在maven->profiles下勾选动态激活需要使用的配置

 

 



避坑:不能识别符号@
在步骤二中配置的@profiles.active@,启动会报异常,不能识别@符号。解决方法:

在pom.xml中设置filtering为true

     <build>
         <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>
  </build>

 


总结:
三种方式都可以实现多环境的配置。在application.yml主配置文件中做项目通用的配置,在其他配置文件中做不同环境下的配置,以避免重复配置的情况。

标签:SpringBoot,配置,profiles,application,dev,true,yml
From: https://www.cnblogs.com/shanhubei/p/16783990.html

相关文章

  • SpringBoot_RestFul风格CURD
    一、什么是RestFulREST(英文:RepresentationalStateTransfer,简称REST,意思:表述性状态转换,描述了一个架构样式的网络系统,比如web应用),是一种软件架构风格不是标准哦!一种软......
  • SpringBoot笔记
    Idea环境创建项目:   ......
  • nginx配置——根据路由参数来设置对应响应方式
      location/{set$is_matched0;#是否有匹配的参数#正则判断url中携带的参数是否有匹配if($query_string~".*(?:^|\?|&)token=123"){set$is_ma......
  • secureCRT配置vim编辑器(语法高亮等)
    配置vim编辑器(语法高亮等)1、在SecureCRT中secureCRT使用VIM时对语法高亮其实不是secureCRT的功能,而是VIM的设置:Options->SessionOptions->Terminal->Emulatio......
  • 内置认证类、权限类、频率类;过滤类的其他使用;全局异常处理;接口文档;django配置文件
    目录内置认证类、权限类、频率类一、内置认证类二、内置权限类三、内置频率类过滤类的其他使用一、内置过滤类全局异常处理一、简介二、自己写一个函数捕获异常接口文档一......
  • node.js安装及配置
    下载官网下载地址:https://nodejs.org/zh-cn/点击下载长期维护版安装系统:Windows11家庭中文版在D盘创建空白文件夹node双击下载的安装包点击next打√并点击next默认安装路......
  • SpringBoot+MyBatis Plus对Map中Date格式转换的处理
    在SpringBoot项目中,如何统一JSON格式化中的日期格式问题现在的关系型数据库例如PostgreSQL/MySQL,都已经对JSON类型提供相当丰富的功能,项目中对于不需要检索但是......
  • yml问题
    YML的问题:当配置其他的生产环境,后面的配置文件不会生效下面的ok.haha无法加载......
  • 思科设备路由重发布配置命令
    Cisco(config)#routereigrp90Cisco(config-router)#redistributeospf110metric100000100025511500 //将OSPF重发布到EIGRPCisco(config)#routerospf110Ci......
  • mysql安装以及配置
    mysql安装执行命令aptlist|grep"mysql"查找mysql软件包找到软件包,执行aptinstallmysql-server-8.0/focal-security安装mysql执行mysql进入mysqlType'help;'or......