Spring中如何实现多环境配置?

 作者简介:大家好,我是码炫码哥,前中兴通讯、美团架构师,现任某互联网公司CTO,兼职码炫课堂主讲源码系列专题


代表作:《jdk源码&多线程&高并发》,《深入tomcat源码解析》,《深入netty源码解析》,《深入dubbo源码解析》,《深入springboot源码解析》,《深入spring源码解析》,《深入redis源码解析》等


联系qq:184480602,加我进群,大家一起学习,一起进步,一起对抗互联网寒冬。码炫课堂的个人空间-码炫码哥个人主页-面试,源码等

回答

在实际应用场景中,多环境配置是非常常见的一个场景,比如开发环境、测试环境、生产环境,各个环境的配置都不相同,而 Spring 就提供了一种非常优雅的方式来支持这种需求。即通过 @Profile 和外部配置文件(如 application.yml 或 application.properties)来实现。

即,我们可以根据不同的环境定义不同的 Bean 或者配置文件,然后通过激活特定环境来加载对应的配置,例如,我们可以在 application.yml 中定义多个环境配置块(如 devtestprod),然后通过 spring.profiles.active 属性指定当前运行的环境。

详解

实现步骤

Spring 允许我们为不同的环境创建不同的配置文件,文件命名规则是 application-{profile}.properties 或 application-{profile}.yml,其中 {profile} 是环境的名称,例如:

application-dev.properties         -- 开发环境
application-test.properties        -- 测试环境
application-prod.properties        -- 生产环境

当启动应用程序时,我们可以通过配置 spring.profiles.active 来指定当前运行环境,比如我们现在需要在开发环境运行我们的应用,则可以配置 spring.profiles.active = dev 。我们可以通过多种方式来指定运行环境,如:

  • application.properties 配置文件
spring.profiles.active=dev
  • 命令行参数
java -jar skjava.jar --spring.profiles.active=dev

然而,在实际开发过程中,我们通常会有一些通用的配置,这些配置在各个环节都是一样的,当然,我们可以将所有配置全部放在各自环境的配置文件中,这种方式也可以实现。但实现的不够优雅。

其实,Spring 提供了一种非常友好的方式来处理这种情况。我们可以将通用的配置放在 application.properties 文件中,然后将特定的配置放在 application-{profile}.properties 文件中。当 Spring 应用启动时,它首先会加载 application.properties 文件,然后再加载 application-{profile}.properties 文件。如果两个文件中有相同的配置,那么 application-{profile}.properties 中的配置会覆盖 application.properties 的配置。.yml 文件也是一样。例如,我们有文件如下:

  • application.properties
spring.datasource.username=root
spring.datasource.password=123456
  • application-dev.properties
spring.datasource.url=jdbc:mysql://localhost/dev
  • application-test.properties
spring.datasource.url=jdbc:mysql://localhost/test
  • application-prod.properties
spring.datasource.url=jdbc:mysql://localhost/prod
spring.datasource.username=root1
spring.datasource.password=123456789

这样,开发环境、测试环境共用一套用户名和密码,而生产环境则使用自己配置中的。

@Profile 注解介绍

Sprin 提供了注解 @Profile,通过它,我们可以根据环境来控制 Bean 的创建,比如某些 Bean 我们只希望他在开发环境被创建,某些 Bean 又希望它在生产环境被创建。这时,就可以使用 @Profile 来处理。比如:

@Configuration
@Profile("dev")
public class DevConfig {
    @Bean
    public DataSource dataSource() {
        return new DataSource("jdbc:mysql://localhost:3306/devdb");
    }
}

@Configuration
@Profile("prod")
public class ProdConfig {
    @Bean
    public DataSource dataSource() {
        return new DataSource("jdbc:mysql://prod-server:3306/proddb");
    }
}

在 dev 环境就加载 DevConfig ,在 prod 环境就加载 ProdConfig 。

上面这种方式是 @Profile 最经典的用法,它还有很多种是哟给你方法。

  • 多个 Profile(逻辑 OR)
@Profile({"dev", "test"})

此 Bean 会在 dev 和 test 环境在加载。

  • 表达式
@Profile("!prod") // 非 "prod" 环境下加载
@Profile("dev & !test") // "dev" 且非 "test" 环境下加载

@Profile 源码分析

@Profile 定义如下:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(ProfileCondition.class)
public @interface Profile {

  /**
   * The set of profiles for which the annotated component should be registered.
   */
  String[] value();

}

从这里可以看出,@Profile 是基于 @Conditional 来实现的,直接看 ProfileCondition

class ProfileCondition implements Condition {

  @Override
  public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(Profile.class.getName());
    if (attrs != null) {
      for (Object value : attrs.get("value")) {
        if (context.getEnvironment().acceptsProfiles(Profiles.of((String[]) value))) {
          return true;
        }
      }
      return false;
    }
    return true;
  }

}

ProfileCondition 实现 Condition 接口,重写了 matches(),从这个方法里面可以看出,它就是从 ConditionContext 里面拿到激活的 Profile 然后和 @Profile 参数进行对比。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值