Spring @Profile注解使用和源码解析

简介: 在之前的文章中,写了一篇使用Spring @Profile实现开发环境,测试环境,生产环境的切换,之前的文章是使用SpringBoot项目搭建,实现了不同环境数据源的切换,在我们实际开发中,会分为dev,test,prod等环境,他们之间数独立的,今天进来详解介绍Spring @Profile的原理。

介绍


在之前的文章中,写了一篇使用Spring @Profile实现开发环境,测试环境,生产环境的切换,之前的文章是使用SpringBoot项目搭建,实现了不同环境数据源的切换,在我们实际开发中,会分为dev,test,prod等环境,他们之间数独立的,今天进来详解介绍Spring @Profile的原理。


# Spring注解@Profile实现开发环境,测试环境,生产环境的切换


使用


带有@Profile的注解的bean的不会被注册进IOC容器,需要为其设置环境变量激活,才能注册进IOC容器,如下通过setActiveProfiles设置了dev值,那么这三个值所对应的Bean会被注册进IOC容器。当然,我们在实际使用中,不会这样去做,使用SpringBoot的话,我们一般是使用yml,在yml中配置spring.profiles.active,也可以通过配置jvm参数。


通过Environment设置profile


我们可以直接通过Environment来设置环境属性,这是比较原生的方法。


AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.getEnvironment().setActiveProfiles("dev");


通过JVM参数设置


可以通过JVM参数来设置环境变量的值,在开发中,这种方式也是使用得比较普遍。



SpringBoot通过yml进行配置


在SpringBoot项目中,我们得配置项一般都是配置在yml文件中,这样就能和代码分开,并且也能进行动态配置。



从上面我们看出可以通过好几种方式进行配置,但是他们最终其实都是将环境变量设置进Environment中,这样,spring在后续得流程里面,就能从Environment中获取环境变量,然后进行相应的逻辑处理。


源码解析


BeanDefinition注册


首先,需要注册bean的元信息BeanDefinition,不过对于@Profile标注的方法,如果环境变量中有对应的变量值,那么就能注册,没有的话则不会进行注册,我们来看关键的代码,在ConfigurationClassBeanDefinitionReader中,有一个shouldSkip判断,它会筛选出符合的bean,不符合条件的bean则被加入skippedBeanMethods集合中,不会被注册。


private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {
  ConfigurationClass configClass = beanMethod.getConfigurationClass();
  MethodMetadata metadata = beanMethod.getMetadata();
  String methodName = metadata.getMethodName();
    // Do we need to mark the bean as skipped by its condition?
  if (this.conditionEvaluator.shouldSkip(metadata, ConfigurationPhase.REGISTER_BEAN)) {
            configClass.skippedBeanMethods.add(methodName);
            return;
  }
            if (configClass.skippedBeanMethods.contains(methodName)) {
            return;
  }
}


shouldSkip源码


在shouldSkip中,会使用Condition接口,@Profile使用的是ProfileCondition,然后调用matches方法。


    public boolean shouldSkip(@Nullable AnnotatedTypeMetadata metadata, @Nullable ConfigurationCondition.ConfigurationPhase phase) {
        for (Condition condition : conditions) {
            ConfigurationCondition.ConfigurationPhase requiredPhase = null;
            if (condition instanceof ConfigurationCondition configurationCondition) {
                requiredPhase = configurationCondition.getConfigurationPhase();
            }
            if ((requiredPhase == null || requiredPhase == phase) && !condition.matches(this.context, metadata)) {
                return true;
            }
        }
        return false;
    }


ProfileCondition匹配


在ProfileCondition的matches方法中,主要就是去Environment中寻找环境变量,然后解析@Profile注解设置的value值,如果Environment中激活的配置中包含当前的配置,包含则能为true,不包含则为false,如上通过setActiveProfiles设置Environment中激活的配置为dev,当前传过来的配置为dev,那么就能匹配上,就能装配进IOC容器。


    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;
    }


从源码可以看出,其最核心的思想就是是否注册bean的元信息BeanDefinition,因为只有注册了BeanDefinition,后续才能为创建bean提供元数据支持,判断是否注册bean元信息,主要就是从Environment中取出profiles的值,然后和@Profile注解设置的值进行匹配,匹配得上就注册,bean不上就不注册。


总结


上面我们对@Profile的使用做了详细的介绍,并对它的核心源码进行解剖,无非就是判断是否要注册BeanDefinition,如果我们需要做一些环境隔离的工作,使用@Profile还是比较不错的。


今天的分享就到这里,感谢你的观看,下期见!


目录
相关文章
|
17小时前
|
监控 Java 应用服务中间件
Spring Boot 源码面试知识点
【5月更文挑战第12天】Spring Boot 是一个强大且广泛使用的框架,旨在简化 Spring 应用程序的开发过程。深入了解 Spring Boot 的源码,有助于开发者更好地使用和定制这个框架。以下是一些关键的知识点:
19 6
|
17小时前
|
Java 应用服务中间件 测试技术
深入探索Spring Boot Web应用源码及实战应用
【5月更文挑战第11天】本文将详细解析Spring Boot Web应用的源码架构,并通过一个实际案例,展示如何构建一个基于Spring Boot的Web应用。本文旨在帮助读者更好地理解Spring Boot的内部工作机制,以及如何利用这些机制优化自己的Web应用开发。
16 3
|
17小时前
PandasTA 源码解析(二十三)
PandasTA 源码解析(二十三)
39 0
|
17小时前
PandasTA 源码解析(二十二)(3)
PandasTA 源码解析(二十二)
34 0
|
17小时前
PandasTA 源码解析(二十二)(2)
PandasTA 源码解析(二十二)
38 2
|
17小时前
PandasTA 源码解析(二十二)(1)
PandasTA 源码解析(二十二)
30 0
|
17小时前
PandasTA 源码解析(二十一)(4)
PandasTA 源码解析(二十一)
22 1
|
Java Maven 数据库
【Spring】Spring常用配置-Profile
【Spring】Spring常用配置-Profile
286 0
【Spring】Spring常用配置-Profile
|
Java Spring Maven
【Spring】Spring常用配置-Profile
转载请注明出处:http://blog.csdn.net/qq_26525215 本文源自【大学之旅_谙忆的博客】 分析 对于Profile先做一个简单的介绍: 单讲profile就是一组配置,不同profile提供不同组合的配置,程序运行时可以选择使用哪些profile来适应环境。
1147 0

热门文章

最新文章

推荐镜像

更多