Skip to content

Commit

Permalink
fix #1593 SpringBoot scanBasePackages does not take effect (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
langchain4j committed Aug 22, 2024
1 parent dc5132f commit 818b4a5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ public class AiServiceScannerProcessor implements BeanDefinitionRegistryPostProc

@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
ClassPathAiServiceScanner classPathInterfaceScanner = new ClassPathAiServiceScanner(registry, false);
classPathInterfaceScanner.registerFilters();
ClassPathAiServiceScanner classPathAiServiceScanner = new ClassPathAiServiceScanner(registry, false);
Set<String> basePackages = getBasePackages((ConfigurableListableBeanFactory) registry);
for (String basePackage : basePackages) {
classPathInterfaceScanner.scan(basePackage);
classPathAiServiceScanner.scan(basePackage);
}
}

Expand All @@ -50,12 +49,15 @@ private Set<String> getBasePackages(ConfigurableListableBeanFactory beanFactory)
}
}

String[] applicationBean = beanFactory.getBeanNamesForAnnotation(SpringBootApplication.class);
SpringBootApplication springbootApplication = AnnotationUtils.findAnnotation(beanFactory.getType(applicationBean[0]), SpringBootApplication.class);
if (springbootApplication != null) {
Collections.addAll(basePackages, springbootApplication.scanBasePackages());
for (Class<?> aClass : springbootApplication.scanBasePackageClasses()) {
basePackages.add(ClassUtils.getPackageName(aClass));
String[] applicationBeans = beanFactory.getBeanNamesForAnnotation(SpringBootApplication.class);
if (applicationBeans.length > 0) {
Class<?> applicationBeanClass = beanFactory.getType(applicationBeans[0]);
SpringBootApplication springBootApplication = AnnotationUtils.findAnnotation(applicationBeanClass, SpringBootApplication.class);
if (springBootApplication != null) {
Collections.addAll(basePackages, springBootApplication.scanBasePackages());
for (Class<?> aClass : springBootApplication.scanBasePackageClasses()) {
basePackages.add(ClassUtils.getPackageName(aClass));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.filter.AnnotationTypeFilter;

public class ClassPathAiServiceScanner extends ClassPathBeanDefinitionScanner {
class ClassPathAiServiceScanner extends ClassPathBeanDefinitionScanner {

public ClassPathAiServiceScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters) {
ClassPathAiServiceScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters) {
super(registry, useDefaultFilters);
addIncludeFilter(new AnnotationTypeFilter(AiService.class));
}

@Override
protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
return beanDefinition.getMetadata().isInterface() && beanDefinition.getMetadata().isIndependent();
}

public void registerFilters() {
addIncludeFilter(new AnnotationTypeFilter(AiService.class));
AnnotationMetadata annotationMetadata = beanDefinition.getMetadata();
return annotationMetadata.isInterface() && annotationMetadata.isIndependent();
}
}

0 comments on commit 818b4a5

Please sign in to comment.