springboot不同环境不同配置

方式一:最简单的方式是直接使用外部的配置文件

在启动参数中添加spring.config.location变量, 指定配置文件路径

java -jar -Dspring.config.location=C:\Users\sk-qianxiao\Desktop\application-dev.properties demo-0.0.1-SNAPSHOT.jar

 

方式二:设置环境变量使用程序类不同环境配置文件

不同环境的配置文件命名规则如下

application-dev.properties:默认配置文件

application-dev.properties:开发环境

application-test.properties:测试环境

application-prod.properties:生产环境

 

指定变量设置环境

java -jar -Dspring.profiles.active=dev demo-0.0.1-SNAPSHOT.jar

注意:没有设置 或 设置 错误使用的是默认配置文件

 

方式三:使用maven在打包时进行配置填充

maven方式原文链接:https://www.cnblogs.com/yucy/p/7082042.html

maven提供了一组属性以供开发人员灵活搭配,可以根据环境来打包,比如测试环境:mvn package -DskipTests -P test,-P也就是指定profile里面id为test的子项配置来打包。在pom文件里面,可以指定多套配置文件,下面例子中区分了三套配置文件,不指定-P则默认为dev。其中的env相当于属性文件中的env=test,也可以多指定多个属性,看实际需要。至于为什么不在相应的属性文件中添加类似env这样的配置,下文会提到。

<!-- 区分环境打包 -->
   <profiles>
       <!-- 开发环境[默认] -->
       <profile>
           <id>dev</id>
           <properties>
               <env>dev</env>
           </properties>
           <activation>
               <activeByDefault>true</activeByDefault>
           </activation>
       </profile>
       <!-- 测试环境 -->
       <profile>
           <id>test</id>
           <properties>
               <env>test</env>
           </properties>
       </profile>
       <!-- 线上环境 -->
       <profile>
           <id>product</id>
           <properties>
               <env>product</env>
           </properties>
       </profile>
   </profiles>

resources和filters,maven中的这两个属性通常是搭配起来使用的,filter这里可以理解为筛选器或者填充器。filters里面可以配置多个filter,但是会以最后一个为准,所以一般只配置一个filter,看下面例子。例子有${env},打包的时候,根据-P test指定,-P 后面的参数与<profile>的<id>一一对应,再根据此ID,到<profile> 的 <properties>得到env属性值,即<env>test</env>。上文指定了三套配置,所以项目里面也需要有这三个配置文件:dev.properties , test.properties , product.properties

 <filters>
      <filter>src/main/resources/${env}.properties</filter>
</filters>

  然后是resources

  1. directory属性指定资源文件放置的目录。
  2. includes则是指定打包时,需要打到jar/war包里的配置文件。下面例子是说需要把src/main/resources目录下的所有的xml配置文件和init.properties属性文件打到包里,所以dev.properties,test.properties,product.properties这三个属性文件不会出现在[jar/war]包文件里
  3. filtering如果设置为false的话,则表示上文的filters配置失效;如果设置为true,则会根据${env}.properties里面的键值对来填充includes指定文件里的${xxxx}占位符。
   <!-- 指定资源文件目录 -->
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <!-- 设置为true,则init.properties会根据${env}.properties里面的配置来填充 -->
            <filtering>true</filtering>
            <includes>
                <include>*.xml</include>
                <include>init.properties</include>
            </includes>
        </resource>
    </resources>

最后演示一下打包运行的结果,假设init.properties里有这么几行:

###########################mysql#################################################
pool.driverClassName=${pool.driverClassName}
pool.url=${pool.url}
pool.username=${pool.username}
pool.password=${pool.password}
pool.maxWait=${pool.maxWait}
# env的属性在这里也有效
env=${env}

test.properties属性文件里是如下配置,注意没有pool.maxWait配置

###########################mysql#################################################
pool.driverClassName=com.mysql.jdbc.Driver
pool.url=jdbc:mysql://192.168.100.23:3306/etmis
pool.username=root
pool.password=123456

则运行打包命令[mvn package -DskipTests -P test]之后,init.properties会被填充为:

###########################mysql#################################################
pool.driverClassName=com.mysql.jdbc.Driver
pool.url=jdbc:mysql://192.168.100.23:3306/etmis
pool.username=root
pool.password=123456
pool.maxWait=${pool.maxWait}
# env的属性在这里也有效
env=test
pool.maxWait不会被填充,因为在test.properties属性文件里面没有这个键值对。在xml文件里面的筛选填充也是一样,会根据文件中的${xxxx}占位符来筛选处理。
<profile>里面配置的属性和${env}.properties里面的属性如果有冲突,比如test.properties里面也配置了【env=testaaaa】,会优先使用<profile>里面的

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值