Spring @Profile注解如何使用 您所在的位置:网站首页 spring容器有哪些 Spring @Profile注解如何使用

Spring @Profile注解如何使用

#Spring @Profile注解如何使用| 来源: 网络整理| 查看: 265

Spring @Profile注解如何使用 发布时间:2023-04-14 11:48:38 来源:亿速云 阅读:65 作者:iii 栏目:开发技术

这篇文章主要介绍“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参数来设置环境变量的值,在开发中,这种方式也是使用得比较普遍。

Spring @Profile注解如何使用

SpringBoot通过yml进行配置

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

Spring @Profile注解如何使用

从上面我们看出可以通过好几种方式进行配置,但是他们最终其实都是将环境变量设置进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 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不上就不注册。

关于“Spring @Profile注解如何使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

推荐阅读: Spring @Profile注解如何实现多环境配置 Spring @Profile注解详解

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:[email protected]进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring @profile 上一篇新闻:xmind免费安装使用的方法是什么 下一篇新闻:IDEA中scala生成变量后自动显示变量类型问题怎么解决 猜你喜欢 怎么通过网站API接口查询ICP域名备案信息 怎么真正掌握Web前端技术 Web开发工具有哪些 Context-React如何跨组件访问数据 好程序员web前端教程分享常见基础面试题之性能优化 好程序员web前端分享H5高级工程师学习思路 bootstrap-表单控件——按钮 Jitamin在CentOS下面的安装部署过程 (一) js对象怎么根据时间进行排序 【MAC OS Sierra】PGP邮件加密教程 Id class 变量 的赋值规范 大驼峰和小驼峰 代码的格式和注释的类型


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有