Skip to content

Commit

Permalink
allow multiple base packages, comma-separated
Browse files Browse the repository at this point in the history
  • Loading branch information
ttulka committed Jan 20, 2025
1 parent 74b36cf commit 872158d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.util.StringUtils;

import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.Optional;
import java.util.Set;

Expand All @@ -28,8 +29,8 @@ public class AnnotationClassScanner<T extends Annotation> implements ClassScanne

@Override
public Set<Class<?>> scan() {
String basePackage = asyncApiDocketService.getAsyncApiDocket().getBasePackage();
if (!StringUtils.hasText(basePackage)) {
String basePackages = asyncApiDocketService.getAsyncApiDocket().getBasePackage();
if (!StringUtils.hasText(basePackages)) {
throw new IllegalArgumentException("Base package must not be blank");
}

Expand All @@ -38,12 +39,15 @@ public Set<Class<?>> scan() {

provider.addIncludeFilter(new AnnotationTypeFilter(annotation));

log.debug("Scanning for {} classes in {}", annotation.getSimpleName(), basePackage);
return provider.findCandidateComponents(basePackage).stream()
.map(BeanDefinition::getBeanClassName)
.map(this::getClass)
.filter(Optional::isPresent)
.map(Optional::get)
return Arrays.stream(basePackages.replaceAll("\\s", "").split(","))
.flatMap(basePackage -> {
log.debug("Scanning for {} classes in {}", annotation.getSimpleName(), basePackage);
return provider.findCandidateComponents(basePackage).stream()
.map(BeanDefinition::getBeanClassName)
.map(this::getClass)
.filter(Optional::isPresent)
.map(Optional::get);
})
.collect(toSet());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
public class AsyncApiDocket {

/**
* The base package containing the declarations of consumers and producer beans.
* The base package(s) containing the declarations of consumers and producer beans.
* Comma-separated for multiple base packages.
*/
private final String basePackage;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ public static class ConfigDocket {
public static final String DEFAULT_CONTENT_TYPE = "application/json";

/**
* The base package to scan for listeners which are declared inside a class annotated with @Component or @Service.
* The base package(s) to scan for listeners which are declared inside a class annotated with @Component or @Service.
* Comma-separated for multiple base packages.
*
* @see AsyncApiDocket.AsyncApiDocketBuilder#basePackage(String)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.github.springwolf.core.asyncapi.scanners.classes.TestComponent;
import io.github.springwolf.core.asyncapi.scanners.classes.TestConditionalComponent;
import io.github.springwolf.core.asyncapi.scanners.classes.TestOtherConditionalComponent;
import io.github.springwolf.core.asyncapi.scanners.classes2.TestComponent2;
import io.github.springwolf.core.configuration.docket.AsyncApiDocket;
import io.github.springwolf.core.configuration.docket.AsyncApiDocketService;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -49,7 +50,8 @@ void setUp() {
.title("ComponentClassScannerTest-title")
.version("ComponentClassScannerTest-version")
.build())
.basePackage("io.github.springwolf.core.asyncapi.scanners.classes")
.basePackage("io.github.springwolf.core.asyncapi.scanners.classes, "
+ "io.github.springwolf.core.asyncapi.scanners.classes2")
.build());
}

Expand All @@ -62,7 +64,7 @@ void getComponents() {

assertThat(components)
.doesNotContain(TestBeanConfiguration.TestBean.class)
.contains(TestComponent.class)
.contains(TestComponent.class, TestComponent2.class)
.doesNotContain(TestConditionalComponent.class)
.doesNotContain(TestOtherConditionalComponent.class);
}
Expand All @@ -76,7 +78,7 @@ void getComponentsIncludesConditional() {

assertThat(components)
.doesNotContain(TestBeanConfiguration.TestBean.class)
.contains(TestComponent.class)
.contains(TestComponent.class, TestComponent2.class)
.contains(TestConditionalComponent.class)
.doesNotContain(TestOtherConditionalComponent.class);
}
Expand Down

0 comments on commit 872158d

Please sign in to comment.