【spring之条件评估器】

2024-01-04 21:12
文章标签 java 条件 spring 评估器

本文主要是介绍【spring之条件评估器】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Spring条件评估器

  • 1. ConditionEvaluator是干嘛的
  • 2. 先看其属性类ConditionContextImp context
  • 3. 看ConditionEvaluator 的内部方法
  • 4. AnnotationTypeMetadata 是干嘛的
  • 5. Condition 接口

1. ConditionEvaluator是干嘛的

内部的使用类,用来评估注解的

2. 先看其属性类ConditionContextImp context

private static class ConditionContextImpl implements ConditionContext {@Nullableprivate final BeanDefinitionRegistry registry;@Nullableprivate final ConfigurableListableBeanFactory beanFactory;private final Environment environment;private final ResourceLoader resourceLoader;@Nullableprivate final ClassLoader classLoader;// 构造器, 实例化时构造这些参数public ConditionContextImpl(@Nullable BeanDefinitionRegistry registry,@Nullable Environment environment, @Nullable ResourceLoader resourceLoader) {this.registry = registry;this.beanFactory = deduceBeanFactory(registry);this.environment = (environment != null ? environment : deduceEnvironment(registry));this.resourceLoader = (resourceLoader != null ? resourceLoader : deduceResourceLoader(registry));this.classLoader = deduceClassLoader(resourceLoader, this.beanFactory);}@Nullable// 根据子类类型,推到出beanFactory!private ConfigurableListableBeanFactory deduceBeanFactory(@Nullable BeanDefinitionRegistry source) {if (source instanceof ConfigurableListableBeanFactory) {return (ConfigurableListableBeanFactory) source;}if (source instanceof ConfigurableApplicationContext) {return (((ConfigurableApplicationContext) source).getBeanFactory());}return null;}// 获取到环境private Environment deduceEnvironment(@Nullable BeanDefinitionRegistry source) {if (source instanceof EnvironmentCapable) {return ((EnvironmentCapable) source).getEnvironment();}return new StandardEnvironment();}// 推到出资源加载器private ResourceLoader deduceResourceLoader(@Nullable BeanDefinitionRegistry source) {if (source instanceof ResourceLoader) {return (ResourceLoader) source;}// 不是ResouceLoader类型的话,就初始化一个默认资源加载器return new DefaultResourceLoader();}@Nullable// 不解释啦,看不懂就别看了private ClassLoader deduceClassLoader(@Nullable ResourceLoader resourceLoader,@Nullable ConfigurableListableBeanFactory beanFactory) {if (resourceLoader != null) {ClassLoader classLoader = resourceLoader.getClassLoader();if (classLoader != null) {return classLoader;}}if (beanFactory != null) {return beanFactory.getBeanClassLoader();}return ClassUtils.getDefaultClassLoader();}@Override// 获取beanDefinition注册器public BeanDefinitionRegistry getRegistry() {Assert.state(this.registry != null, "No BeanDefinitionRegistry available");return this.registry;}@Override@Nullable// 返回beanFactorypublic ConfigurableListableBeanFactory getBeanFactory() {return this.beanFactory;}@Overridepublic Environment getEnvironment() {return this.environment;}@Overridepublic ResourceLoader getResourceLoader() {return this.resourceLoader;}@Override@Nullablepublic ClassLoader getClassLoader() {return this.classLoader;}
}
// Context information for use by Condition implementations
// 条件实现类使用的上下文信息类
public interface ConditionContext {// 获取BeanDefinition信息BeanDefinitionRegistry getRegistry();// 这不用说获取容器ConfigurableListableBeanFactory getBeanFactory();// 获取环境Environment getEnvironment();// 获取资源加载器ResourceLoader getResourceLoader();// 获取类加载器ClassLoader getClassLoader();
}

3. 看ConditionEvaluator 的内部方法

// 构造器
public ConditionEvaluator(@Nullable BeanDefinitionRegistry registry,@Nullable Environment environment, @Nullable ResourceLoader resourceLoader) {// 根据参数实例化条件上下文this.context = new ConditionContextImpl(registry, environment, resourceLoader);
}// 是否跳过
public boolean shouldSkip(AnnotatedTypeMetadata metadata) {return shouldSkip(metadata, null);
}public boolean shouldSkip(@Nullable AnnotatedTypeMetadata metadata, @Nullable ConfigurationPhase phase) {if (metadata == null || !metadata.isAnnotated(Conditional.class.getName())) {return false;}if (phase == null) {if (metadata instanceof AnnotationMetadata &&ConfigurationClassUtils.isConfigurationCandidate((AnnotationMetadata) metadata)) {return shouldSkip(metadata, ConfigurationPhase.PARSE_CONFIGURATION);}return shouldSkip(metadata, ConfigurationPhase.REGISTER_BEAN);}List<Condition> conditions = new ArrayList<>();for (String[] conditionClasses : getConditionClasses(metadata)) {for (String conditionClass : conditionClasses) {Condition condition = getCondition(conditionClass, this.context.getClassLoader());conditions.add(condition);}}AnnotationAwareOrderComparator.sort(conditions);for (Condition condition : conditions) {ConfigurationPhase requiredPhase = null;if (condition instanceof ConfigurationCondition) {requiredPhase = ((ConfigurationCondition) condition).getConfigurationPhase();}if ((requiredPhase == null || requiredPhase == phase) && !condition.matches(this.context, metadata)) {return true;}}return false;
}

4. AnnotationTypeMetadata 是干嘛的

// 用于获取元素上的注解的元数据,不用看了!
MergedAnnotations getAnnotations();default boolean isAnnotated(String annotationName) {return getAnnotations().isPresent(annotationName);
}default Map<String, Object> getAnnotationAttributes(String annotationName) {return getAnnotationAttributes(annotationName, false);
}default Map<String, Object> getAnnotationAttributes(String annotationName,boolean classValuesAsString) {MergedAnnotation<Annotation> annotation = getAnnotations().get(annotationName,null, MergedAnnotationSelectors.firstDirectlyDeclared());if (!annotation.isPresent()) {return null;}return annotation.asAnnotationAttributes(Adapt.values(classValuesAsString, true));
}default MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName) {return getAllAnnotationAttributes(annotationName, false);
}default MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName, boolean classValuesAsString) {Adapt[] adaptations = Adapt.values(classValuesAsString, true);return getAnnotations().stream(annotationName).filter(MergedAnnotationPredicates.unique(MergedAnnotation::getMetaTypes)).map(MergedAnnotation::withNonMergedAttributes).collect(MergedAnnotationCollectors.toMultiValueMap(map ->map.isEmpty() ? null : map, adaptations));
}

5. Condition 接口

boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata);

这篇关于【spring之条件评估器】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/570624

相关文章

Java StringBuilder 实现原理全攻略

《JavaStringBuilder实现原理全攻略》StringBuilder是Java提供的可变字符序列类,位于java.lang包中,专门用于高效处理字符串的拼接和修改操作,本文给大家介绍Ja... 目录一、StringBuilder 基本概述核心特性二、StringBuilder 核心实现2.1 内部

SpringBoot AspectJ切面配合自定义注解实现权限校验的示例详解

《SpringBootAspectJ切面配合自定义注解实现权限校验的示例详解》本文章介绍了如何通过创建自定义的权限校验注解,配合AspectJ切面拦截注解实现权限校验,本文结合实例代码给大家介绍的非... 目录1. 创建权限校验注解2. 创建ASPectJ切面拦截注解校验权限3. 用法示例A. 参考文章本文

Java中字符编码问题的解决方法详解

《Java中字符编码问题的解决方法详解》在日常Java开发中,字符编码问题是一个非常常见却又特别容易踩坑的地方,这篇文章就带你一步一步看清楚字符编码的来龙去脉,并结合可运行的代码,看看如何在Java项... 目录前言背景:为什么会出现编码问题常见场景分析控制台输出乱码文件读写乱码数据库存取乱码解决方案统一使

Java Stream流与使用操作指南

《JavaStream流与使用操作指南》Stream不是数据结构,而是一种高级的数据处理工具,允许你以声明式的方式处理数据集合,类似于SQL语句操作数据库,本文给大家介绍JavaStream流与使用... 目录一、什么是stream流二、创建stream流1.单列集合创建stream流2.双列集合创建str

springboot集成easypoi导出word换行处理过程

《springboot集成easypoi导出word换行处理过程》SpringBoot集成Easypoi导出Word时,换行符n失效显示为空格,解决方法包括生成段落或替换模板中n为回车,同时需确... 目录项目场景问题描述解决方案第一种:生成段落的方式第二种:替换模板的情况,换行符替换成回车总结项目场景s

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

SpringBoot中@Value注入静态变量方式

《SpringBoot中@Value注入静态变量方式》SpringBoot中静态变量无法直接用@Value注入,需通过setter方法,@Value(${})从属性文件获取值,@Value(#{})用... 目录项目场景解决方案注解说明1、@Value("${}")使用示例2、@Value("#{}"php

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

线上Java OOM问题定位与解决方案超详细解析

《线上JavaOOM问题定位与解决方案超详细解析》OOM是JVM抛出的错误,表示内存分配失败,:本文主要介绍线上JavaOOM问题定位与解决方案的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一、OOM问题核心认知1.1 OOM定义与技术定位1.2 OOM常见类型及技术特征二、OOM问题定位工具

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建