Springboot之类路径扫描

SpringBoot框架中默认提供的扫描类为:ClassPathBeanDefinitionScanner。 webFlux框架中借助RepositoryComponentProvider扫描符合条件的Repository。

java 复制代码
public class ClassPathScanningCandidateComponentProvider{

	private final List<TypeFilter> includeFilters = new ArrayList<>();
	private final List<TypeFilter> excludeFilters = new ArrayList<>();

	public Set<BeanDefinition> findCandidateComponents(String basePackage) {
		//根据类路径扫描候选类
		return scanCandidateComponents(basePackage);
	}

	private Set<BeanDefinition> scanCandidateComponents(String basePackage) {
		Set<BeanDefinition> candidates = new LinkedHashSet<>();
		try {
			String packageSearchPath = "classpath*:" + resolveBasePackage(basePackage) + '/' + this.resourcePattern;
			Resource[] resources = getResourcePatternResolver().getResources(packageSearchPath);
			for (Resource resource : resources) {
				String filename = resource.getFilename();
				MetadataReader metadataReader = getMetadataReaderFactory().getMetadataReader(resource);
				if (isCandidateComponent(metadataReader)) {
					ScannedGenericBeanDefinition sbd = new ScannedGenericBeanDefinition(metadataReader);
					sbd.setSource(resource);
					if (isCandidateComponent(sbd)) {//候选类条件判断
						candidates.add(sbd);
					}
				}
			}
		}
		return candidates;
	}

	protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException {
		for (TypeFilter tf : this.excludeFilters) {
			if (tf.match(metadataReader, getMetadataReaderFactory())) {
				return false;
			}
		}
		for (TypeFilter tf : this.includeFilters) {
			if (tf.match(metadataReader, getMetadataReaderFactory())) {
				return isConditionMatch(metadataReader);
			}
		}
		return false;
	}
}

扫描路径提供:includeFilters & excludeFilters。

相关推荐
陌殇殇9 小时前
001 Spring AI Alibaba框架整合百炼大模型平台 — 快速入门
人工智能·spring boot·ai
还在忙碌的吴小二11 小时前
Harness 最佳实践:Java Spring Boot 项目落地 OpenSpec + Claude Code
java·开发语言·spring boot·后端·spring
ai产品老杨12 小时前
异构计算时代的视频底座:基于 ZLMediaKit 与 Spring Boot 的 X86/ARM 跨平台架构解析
arm开发·spring boot·音视频
老神在在00112 小时前
Spring Boot 全局异常处理器(GlobalExceptionHandler)
spring boot·spring·java-ee·状态模式·
han_hanker13 小时前
@GetMapping @PostMapping @DeleteMapping @PutMapping
spring boot
han_hanker14 小时前
@Validated @Valid 用法
java·spring boot
言慢行善14 小时前
SpringBoot中的注解介绍
java·spring boot·后端
许杰小刀15 小时前
MyBatis-Plus实战:Spring Boot数据库操作效率提升10倍
数据库·spring boot·mybatis
霸道流氓气质17 小时前
SpringBoot中使用SpringAIAlibaba框架集成阿里云百炼实现AI快速对话入门示例
人工智能·spring boot·阿里云