方式一:多个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主配置文件中做项目通用的配置,在其他配置文件中做不同环境下的配置,以避免重复配置的情况。

原文地址:http://www.cnblogs.com/shanhubei/p/16783990.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性