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(SpringBoot、Mybatis、SpringMvc)+MySQL实现(Web)小二结账系统
java·spring boot·mybatis
全职计算机毕业设计3 小时前
SpringBoot+Mysql实现的停车场收费小程序系统+文档
spring boot·mysql·小程序
努力的小郑3 小时前
BeanFactory与ApplicationContext全面指南与实战
spring boot·spring
crud3 小时前
Spring Boot 整合 MyBatis-Plus:从入门到精通,一文搞定高效持久层开发!
java·spring boot·mybatis
alien爱吃蛋挞6 小时前
【JavaEE】Spring Boot项目创建
spring boot·java-ee
wxid:yiwoxuan7 小时前
购物商城网站 Java+Vue.js+SpringBoot,包括商家管理、商品分类管理、商品管理、在线客服管理、购物订单模块
java·vue.js·spring boot·课程设计
Java知识库7 小时前
「深度拆解」Spring Boot如何用DeepSeek重构MCP通信层?从线程模型到分布式推理的架构进化
java·开发语言·spring boot·程序员·编程
小白杨树树8 小时前
【SSM】SpringBoot学习笔记1:SpringBoot快速入门
spring boot·笔记·学习
Java知识库8 小时前
Spring Boot+Neo4j知识图谱实战:3步搭建智能关系网络!
spring boot·知识图谱·neo4j
风象南8 小时前
SpringBoot实现简易直播
java·spring boot·后端