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。

相关推荐
aq553560032 分钟前
SpringBoot有几种获取Request对象的方法
java·spring boot·后端
steel80881 小时前
Spring Boot 整合 log4j2 日志配置教程
spring boot·单元测试·log4j
lierenvip1 小时前
Spring Boot中Tomcat配置
spring boot·tomcat·firefox
Detachym1 小时前
InsightFlow 服务配置优化与部署实践
java·spring boot·tomcat·maven·状态模式·jar
rainchestnut1 小时前
Spring AI 初步集成(1)-初始化
spring boot
流水武qin2 小时前
SpringAI多模态的基本使用
java·spring boot·spring·ai
小飞Coding2 小时前
Spring 容器生命周期:10大核心扩展接口+实战代码
spring boot
吾诺2 小时前
Spring Boot--@PathVariable、@RequestParam、@RequestBody
java·spring boot·后端
jiankeljx2 小时前
Spring Boot实现多数据源连接和切换
spring boot·后端·oracle
xyyaihxl4 小时前
springboot与springcloud对应版本
java·spring boot·spring cloud