springboot多配置文件

springboot动态的获取不同环境下的配置文件

需求:在实际的开发过程中,我们可能会遇到不同的环境选用不同配置的情况,如开发环境连开发的数据库,服务器。生产环境连生产的数据库,服务器等。

方法一、启动时切换环境法

在这里插入图片描述
公共的配置可以放在application.properties中,各个环境的配置放在相应的配置文件中。
当使用某个环境的配置时,只需在公共的配置中(即application.properties)加一行下面的代码

spring.profiles.active=dev

即激活相应的配置文件。
注意:
此方法各个环境的配置文件的命名需是application-{}.properties

打成war包之后,可以在启动时切换环境
java -jar 项目的war包名 -P dev/test/pro 来切换不同的环境配置文件

方法二:打包时切换环境法

2.1配置文件命名
在这里插入图片描述
注:在springboot项目中,bootstrap.properties配置的优先级高于application.properties的配置文件

在上述截图配置文件中,bootstrap.properties的配置
在这里插入图片描述
该配置文件,只写这一项就可以

2.2 pom文件中的配置

 <project >
 <build>
       <resources>
           <resource>
               <directory>src/main/resources</directory>
               <filtering>true</filtering>
               <excludes>
                   <exclude>bootstrap-dev.properties</exclude>
                   <exclude>bootstrap-pro.properties</exclude>
                   <exclude>bootstrap-uat.properties</exclude>
                   <exclude>bootstrap.properties</exclude>
               </excludes>
           </resource>
           <resource>
               <directory>src/main/resources</directory>
               <filtering>true</filtering>
               <includes>
                   <include>bootstrap-${env}.properties</include>
                   <include>bootstrap.properties</include>
               </includes>
           </resource>
       </resources>

       <finalName>SpringbootWeb</finalName>
       <plugins>
           <plugin>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-maven-plugin</artifactId>
           </plugin>
       </plugins>
   </build>
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>
            </properties>
            <activation>
               <!-- 默认启动的配置文件-->
                <activeByDefault>true</activeByDefault>  
            </activation>
        </profile>
        <profile>
            <id>pro</id>
            <properties>
                <env>pro</env>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <env>test</env>
            </properties>
        </profile>
    </profiles>
    </project>

备注:
使用SpringBoot,发现其中的bootstrap.properties文件无法生效,改成yaml格式也无济于事。

最后调查发现原来是因为SpringBoot本身并不支持,需要和Spring Cloud 的组件结合——只有加上Spring Cloud Context依赖才能生效。

  <!--需要引入该jar才能使bootstrap配置文件生效-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-context</artifactId>
        </dependency>

版本号问题注意一下

**该方法进行环境切换时,是在打包的时候执行命令
mvn clean package -Dmaven.test.skip=true -P dev/test/pro

通过 java -jar 包名
启动项目时,默认是打包时的环境配置文件**

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值