spring profile的使用

本文介绍了Spring框架中如何利用Profile特性实现不同环境下的配置切换。通过application-{profile}

1. application.properties

通过application-{profile}.properties的命名规则,给application.properties指定profile,已达到不同的环境使用不同配置的目的。

指定profile的application.properties配置文件会覆盖不指定profile的配置文件,如果有多个profile配置文件,后者覆盖前者。

2. @Profile

任何有@Component和@Configuration注解的class都能添加@Profile注解,以使其仅在指定的环境中生效。如:

@Configuration
@Profile("production")
public class ProductionConfiguration {

    // ...

}

通常,使用系统属性(spring.profiles.active) 或操作系统环境变量(SPRING_PROFILES_ACTIVE)激活profile配置。

使用系统属性,如

$ java -jar -Dspring.profiles.active=production demo-0.0.1-SNAPSHOT.jar

使用spring.profiles.active变量来激活profile,比如在application.properties中指定属性

spring.profiles.active=dev,hsqldb

或者,通过命令行--spring.profiles.active=dev,hsqldb

转载于:https://my.oschina.net/yqz/blog/994381

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值