Skip to content

Commit 67f038c

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

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

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

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

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

465+
private void extractPackagesFromComponentScan(Map<String, Object> scanAttributes, Set<String> basePackages) {
466+
for (String pkg : (String[]) scanAttributes.get("value")) {
467+
if (StringUtils.hasText(pkg)) {
468+
basePackages.add(pkg);
469+
}
470+
}
471+
for (String pkg : (String[]) scanAttributes.get("basePackages")) {
472+
if (StringUtils.hasText(pkg)) {
473+
basePackages.add(pkg);
474+
}
475+
}
476+
for (Class<?> clazz : (Class[]) scanAttributes.get("basePackageClasses")) {
477+
basePackages.add(ClassUtils.getPackageName(clazz));
478+
}
479+
}
480+
419481
private String getQualifier(Map<String, Object> client) {
420482
if (client == null) {
421483
return null;

0 commit comments

Comments
 (0)