【spring之条件评估器】

1. ConditionEvaluator是干嘛的

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

2. 先看其属性类ConditionContextImp context

private static class ConditionContextImpl implements ConditionContext {
   

		@Nullable
		private final BeanDefinitionRegistry registry;

		@Nullable
		private final ConfigurableListableBeanFactory beanFactory;

		private final Environment environment;

		private final ResourceLoader resourceLoader;

		@Nullable
		private 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
		// 返回beanFactory
		public ConfigurableListableBeanFactory getBeanFactory() {
   
			return this.beanFactory;
		}

		@Override
		public Environment getEnvironment() {
   
			return this.environment;
		}

		@Override
		public ResourceLoader getResourceLoader() {
   
			return this.resourceLoader;
		}

		@Override
		@Nullable
		public 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);

相关推荐

  1. spring条件评估

    2024-01-07 21:04:04       35 阅读
  2. Spring Boot系列条件注解

    2024-01-07 21:04:04       11 阅读
  3. 2. 条件构造

    2024-01-07 21:04:04       37 阅读
  4. 【MyBatisPlus条件构造

    2024-01-07 21:04:04       8 阅读
  5. 详解 Spring Boot 条件装配

    2024-01-07 21:04:04       27 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-07 21:04:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-07 21:04:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-07 21:04:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-07 21:04:04       18 阅读

热门阅读

  1. AMP 通讯RPMsg

    2024-01-07 21:04:04       44 阅读
  2. PHP运行环境之宝塔Web站点部署

    2024-01-07 21:04:04       34 阅读
  3. Android 车联网——电源管理功能扩展(十)

    2024-01-07 21:04:04       27 阅读
  4. Linux&Shell--多服务器自动登录连接

    2024-01-07 21:04:04       39 阅读
  5. Qt 的流式布局 FlowLayout

    2024-01-07 21:04:04       43 阅读
  6. 结构体数组按总分排序(结构体)

    2024-01-07 21:04:04       30 阅读
  7. 家庭顶梁柱保险如何配置?

    2024-01-07 21:04:04       37 阅读