Skip to content

Commit ad2c3cc

Browse files
committed
extract packages from SpringBootApplication or ComponentScan if EnableFeignClients does not set basePackages or basePackageClasses
1 parent 80cba56 commit ad2c3cc

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientsRegistrar.java

+59
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,65 @@ protected Set<String> getBasePackages(AnnotationMetadata importingClassMetadata)
410410
basePackages.add(ClassUtils.getPackageName(clazz));
411411
}
412412

413+
if (basePackages.isEmpty()) {
414+
// org.springframework.boot.autoconfigure.SpringBootApplication
415+
String bootAppClassName = "org.springframework.boot.autoconfigure.SpringBootApplication";
416+
if (importingClassMetadata.hasAnnotation(bootAppClassName)){
417+
Map<String, Object> bootAttributes = importingClassMetadata
418+
.getAnnotationAttributes(bootAppClassName);
419+
for (String pkg : (String[]) bootAttributes.get("scanBasePackages")) {
420+
if (StringUtils.hasText(pkg)) {
421+
basePackages.add(pkg);
422+
}
423+
}
424+
for (Class<?> clazz : (Class[]) bootAttributes.get("scanBasePackageClasses")) {
425+
basePackages.add(ClassUtils.getPackageName(clazz));
426+
}
427+
}
428+
429+
String componentScanClassName = "org.springframework.context.annotation.ComponentScan";
430+
if (importingClassMetadata.hasAnnotation(componentScanClassName)) {
431+
Map<String, Object> scanAttributes = importingClassMetadata
432+
.getAnnotationAttributes(componentScanClassName);
433+
for (String pkg : (String[]) scanAttributes.get("value")) {
434+
if (StringUtils.hasText(pkg)) {
435+
basePackages.add(pkg);
436+
}
437+
}
438+
for (String pkg : (String[]) scanAttributes.get("basePackages")) {
439+
if (StringUtils.hasText(pkg)) {
440+
basePackages.add(pkg);
441+
}
442+
}
443+
for (Class<?> clazz : (Class[]) scanAttributes.get("basePackageClasses")) {
444+
basePackages.add(ClassUtils.getPackageName(clazz));
445+
}
446+
}
447+
448+
String componentScansClassName = "org.springframework.context.annotation.ComponentScans";
449+
if (importingClassMetadata.hasAnnotation(componentScansClassName)) {
450+
Map<String, Object> componentScansAttrs =
451+
importingClassMetadata.getAnnotationAttributes(componentScansClassName);
452+
AnnotationAttributes[] componentScanAttributes =
453+
(AnnotationAttributes[]) componentScansAttrs.get("value");
454+
for (AnnotationAttributes scanAttributes : componentScanAttributes) {
455+
for (String pkg : (String[]) scanAttributes.get("value")) {
456+
if (StringUtils.hasText(pkg)) {
457+
basePackages.add(pkg);
458+
}
459+
}
460+
for (String pkg : (String[]) scanAttributes.get("basePackages")) {
461+
if (StringUtils.hasText(pkg)) {
462+
basePackages.add(pkg);
463+
}
464+
}
465+
for (Class<?> clazz : (Class[]) scanAttributes.get("basePackageClasses")) {
466+
basePackages.add(ClassUtils.getPackageName(clazz));
467+
}
468+
}
469+
}
470+
}
471+
413472
if (basePackages.isEmpty()) {
414473
basePackages.add(ClassUtils.getPackageName(importingClassMetadata.getClassName()));
415474
}

0 commit comments

Comments
 (0)