【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

相关文章

JavaWeb-WebSocket浏览器服务器双向通信方式

《JavaWeb-WebSocket浏览器服务器双向通信方式》文章介绍了WebSocket协议的工作原理和应用场景,包括与HTTP的对比,接着,详细介绍了如何在Java中使用WebSocket,包括配... 目录一、概述二、入门2.1 POM依赖2.2 编写配置类2.3 编写WebSocket服务2.4 浏

配置springboot项目动静分离打包分离lib方式

《配置springboot项目动静分离打包分离lib方式》本文介绍了如何将SpringBoot工程中的静态资源和配置文件分离出来,以减少jar包大小,方便修改配置文件,通过在jar包同级目录创建co... 目录前言1、分离配置文件原理2、pom文件配置3、使用package命令打包4、总结前言默认情况下,

Java文件与Base64之间的转化方式

《Java文件与Base64之间的转化方式》这篇文章介绍了如何使用Java将文件(如图片、视频)转换为Base64编码,以及如何将Base64编码转换回文件,通过提供具体的工具类实现,作者希望帮助读者... 目录Java文件与Base64之间的转化1、文件转Base64工具类2、Base64转文件工具类3、

java获取图片的大小、宽度、高度方式

《java获取图片的大小、宽度、高度方式》文章介绍了如何将File对象转换为MultipartFile对象的过程,并分享了个人经验,希望能为读者提供参考... 目China编程录Java获取图片的大小、宽度、高度File对象(该对象里面是图片)MultipartFile对象(该对象里面是图片)总结java获取图片

Java通过反射获取方法参数名的方式小结

《Java通过反射获取方法参数名的方式小结》这篇文章主要为大家详细介绍了Java如何通过反射获取方法参数名的方式,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、前言2、解决方式方式2.1: 添加编译参数配置 -parameters方式2.2: 使用Spring的内部工具类 -

Java如何获取视频文件的视频时长

《Java如何获取视频文件的视频时长》文章介绍了如何使用Java获取视频文件的视频时长,包括导入maven依赖和代码案例,同时,也讨论了在运行过程中遇到的SLF4J加载问题,并给出了解决方案... 目录Java获取视频文件的视频时长1、导入maven依赖2、代码案例3、SLF4J: Failed to lo

如何使用Spring boot的@Transactional进行事务管理

《如何使用Springboot的@Transactional进行事务管理》这篇文章介绍了SpringBoot中使用@Transactional注解进行声明式事务管理的详细信息,包括基本用法、核心配置... 目录一、前置条件二、基本用法1. 在方法上添加注解2. 在类上添加注解三、核心配置参数1. 传播行为(

在Java中使用ModelMapper简化Shapefile属性转JavaBean实战过程

《在Java中使用ModelMapper简化Shapefile属性转JavaBean实战过程》本文介绍了在Java中使用ModelMapper库简化Shapefile属性转JavaBean的过程,对比... 目录前言一、原始的处理办法1、使用Set方法来转换2、使用构造方法转换二、基于ModelMapper

JAVA调用Deepseek的api完成基本对话简单代码示例

《JAVA调用Deepseek的api完成基本对话简单代码示例》:本文主要介绍JAVA调用Deepseek的api完成基本对话的相关资料,文中详细讲解了如何获取DeepSeekAPI密钥、添加H... 获取API密钥首先,从DeepSeek平台获取API密钥,用于身份验证。添加HTTP客户端依赖使用Jav

Java实现状态模式的示例代码

《Java实现状态模式的示例代码》状态模式是一种行为型设计模式,允许对象根据其内部状态改变行为,本文主要介绍了Java实现状态模式的示例代码,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来... 目录一、简介1、定义2、状态模式的结构二、Java实现案例1、电灯开关状态案例2、番茄工作法状态案例