From e4dc24cb8d7d90dd2886c58c1a745e60dad93dc0 Mon Sep 17 00:00:00 2001 From: qxo <49526356@qq.com> Date: Thu, 11 Nov 2021 20:50:21 +0800 Subject: [PATCH] fix: the basePackage do not have META-INF/spring.component then switch scanCandidateComponents(basePackage) If the basePackage not defined in META-INF/spring.component, then using old `scanCandidateComponents(basePackage)`; Otherwise the basePackage not in META-INF/spring.component will not load if the env classpath has any META-INF/spring.component file. --- .../ClassPathScanningCandidateComponentProvider.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.java b/spring-context/src/main/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.java index 05ea7a5159a5..1c0091d728b3 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.java @@ -19,6 +19,7 @@ import java.io.IOException; import java.lang.annotation.Annotation; import java.util.ArrayList; +import java.util.Collections; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; @@ -373,12 +374,20 @@ private Set addCandidateComponentsFromIndex(CandidateComponentsI Set candidates = new LinkedHashSet<>(); try { Set types = new HashSet<>(); + boolean basePackageIndexNotDefined = true; for (TypeFilter filter : this.includeFilters) { String stereotype = extractStereotype(filter); if (stereotype == null) { throw new IllegalArgumentException("Failed to extract stereotype from " + filter); } - types.addAll(index.getCandidateTypes(basePackage, stereotype)); + Set candidateTypes = index.getCandidateTypes(basePackage, stereotype); + if (basePackageIndexNotDefined && !Collections.EMPTY_SET.equals(candidateTypes)) { + basePackageIndexNotDefined = false; + } + types.addAll(candidateTypes); + } + if (basePackageIndexNotDefined) { + return scanCandidateComponents(basePackage); } boolean traceEnabled = logger.isTraceEnabled(); boolean debugEnabled = logger.isDebugEnabled();