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。

相关推荐
码农飞哥2 小时前
互联网大厂Java求职面试实战:Spring Boot到微服务的技术问答解析
java·spring boot·缓存·面试·消息队列·技术栈·microservices
曼岛_2 小时前
[Java实战]Spring Boot + Netty 实现 TCP 长连接客户端及 RESTful 请求转发(二十六)
java·spring boot·tcp/ip
果冻kk3 小时前
【实战教程】从零实现DeepSeek AI多专家协作系统 - Spring Boot+React打造AI专家团队协作平台
人工智能·spring boot·react.js
CircleMouse3 小时前
springboot如何通过提供的注解方式来操作Redis
java·spring boot·redis·spring·mybatis
bing_1584 小时前
Spring Boot 项目中什么时候会抛出 FeignException?
java·spring boot·后端
Java&Develop4 小时前
springboot + mysql8降低版本到 mysql5.7
java·spring boot·后端
sg_knight4 小时前
从单体架构到微服务:架构演进之路
java·spring boot·spring·spring cloud·微服务·云原生·架构
资深の小白4 小时前
一个基于 Spring Boot 的实现,用于代理百度 AI 的 OCR 接口
人工智能·spring boot·百度
武昌库里写JAVA4 小时前
MacOS Python3安装
java·开发语言·spring boot·学习·课程设计