springboot 使用 profiles区分多环境配置

54 篇文章 1 订阅
1. 在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如:
  application-dev.properties:开发环境
  application-test.properties:测试环境
  application-prod.properties:生产环境

至于哪个具体的配置文件会被加载,需要在application.properties文件中通过spring.profiles.active属性来设置,其值对应{profile}值。
如:spring.profiles.active=test就会加载application-test.properties配置文件内容

参考: spring.profiles.active来分区配置

2. 可能遇到的问题:

如果自定义了日志文件, 如 logback-spring.xml , 并且在具体的 yml 或 properties 文件中 指定了该文件如:

logging:
  config: classpath:logback-spring.xml

那么在 logback-spring.xml 文件中必须对 相应的Profile增加节点值才会在控制台有相应的输出.

    <springProfile name="dev">
        <root level="info">
            <appender-ref ref="CONSOLE"/>
        </root>
        <logger name="com.example" level="debug"/>
    </springProfile>

参考: Spring Boot 日志配置方法(超详细)

3. 其他

在pom.xml文件中也有一个 profile:

    <profiles>
        <profile>
            <id>local</id>
            <properties>
                <profileActive>local</profileActive>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>onlinepre</id>
            <properties>
                <profileActive>onlinepre</profileActive>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <profileActive>prod</profileActive>
            </properties>
        </profile>
    </profiles>

经过测试, 如果在 1. 中定义的profile如 application-dev.properties, 没有添加到pom.xml 的profile中, 项目实际运行时也不会出现问题. 不过很大概率是在我的项目中没有用到 pom.xml 相关的 profile ,比如 @Profiles 注解? 还需要进一步探究.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值