Skip to content

Commit

Permalink
extract packages from SpringBootApplication or ComponentScan if Enabl…
Browse files Browse the repository at this point in the history
…eFeignClients does not set basePackages or basePackageClasses
  • Loading branch information
diguage committed Oct 19, 2024
1 parent 80cba56 commit ad2c3cc
Showing 1 changed file with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,65 @@ protected Set<String> getBasePackages(AnnotationMetadata importingClassMetadata)
basePackages.add(ClassUtils.getPackageName(clazz));
}

if (basePackages.isEmpty()) {
// org.springframework.boot.autoconfigure.SpringBootApplication
String bootAppClassName = "org.springframework.boot.autoconfigure.SpringBootApplication";
if (importingClassMetadata.hasAnnotation(bootAppClassName)){
Map<String, Object> bootAttributes = importingClassMetadata
.getAnnotationAttributes(bootAppClassName);
for (String pkg : (String[]) bootAttributes.get("scanBasePackages")) {
if (StringUtils.hasText(pkg)) {
basePackages.add(pkg);
}
}
for (Class<?> clazz : (Class[]) bootAttributes.get("scanBasePackageClasses")) {
basePackages.add(ClassUtils.getPackageName(clazz));
}
}

String componentScanClassName = "org.springframework.context.annotation.ComponentScan";
if (importingClassMetadata.hasAnnotation(componentScanClassName)) {
Map<String, Object> scanAttributes = importingClassMetadata
.getAnnotationAttributes(componentScanClassName);
for (String pkg : (String[]) scanAttributes.get("value")) {
if (StringUtils.hasText(pkg)) {
basePackages.add(pkg);
}
}
for (String pkg : (String[]) scanAttributes.get("basePackages")) {
if (StringUtils.hasText(pkg)) {
basePackages.add(pkg);
}
}
for (Class<?> clazz : (Class[]) scanAttributes.get("basePackageClasses")) {
basePackages.add(ClassUtils.getPackageName(clazz));
}
}

String componentScansClassName = "org.springframework.context.annotation.ComponentScans";
if (importingClassMetadata.hasAnnotation(componentScansClassName)) {
Map<String, Object> componentScansAttrs =
importingClassMetadata.getAnnotationAttributes(componentScansClassName);
AnnotationAttributes[] componentScanAttributes =
(AnnotationAttributes[]) componentScansAttrs.get("value");
for (AnnotationAttributes scanAttributes : componentScanAttributes) {
for (String pkg : (String[]) scanAttributes.get("value")) {
if (StringUtils.hasText(pkg)) {
basePackages.add(pkg);
}
}
for (String pkg : (String[]) scanAttributes.get("basePackages")) {
if (StringUtils.hasText(pkg)) {
basePackages.add(pkg);
}
}
for (Class<?> clazz : (Class[]) scanAttributes.get("basePackageClasses")) {
basePackages.add(ClassUtils.getPackageName(clazz));
}
}
}
}

if (basePackages.isEmpty()) {
basePackages.add(ClassUtils.getPackageName(importingClassMetadata.getClassName()));
}
Expand Down

0 comments on commit ad2c3cc

Please sign in to comment.