forked from langchain4j/langchain4j-spring
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix [BUG] #1606 AiServicesAutoConfig is unable to detect AiService in…
… the package specified by @componentscan
- Loading branch information
Showing
4 changed files
with
79 additions
and
25 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...-boot-starter/src/main/java/dev/langchain4j/service/spring/AiServiceScannerProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package dev.langchain4j.service.spring; | ||
|
||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; | ||
import org.springframework.boot.autoconfigure.AutoConfigurationPackages; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.core.type.filter.AnnotationTypeFilter; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Collections; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
@Component | ||
public class AiServiceScannerProcessor implements BeanDefinitionRegistryPostProcessor { | ||
|
||
@Override | ||
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { | ||
ClassPathAiServiceScanner classPathAiServiceScanner = new ClassPathAiServiceScanner(registry, false); | ||
classPathAiServiceScanner.addIncludeFilter(new AnnotationTypeFilter(AiService.class)); | ||
Set<String> basePackages = getBasePackages((ConfigurableListableBeanFactory) registry); | ||
for (String basePackage : basePackages) { | ||
classPathAiServiceScanner.scan(basePackage); | ||
} | ||
} | ||
|
||
private Set<String> getBasePackages(ConfigurableListableBeanFactory beanFactory) { | ||
Set<String> basePackages = new LinkedHashSet<>(); | ||
|
||
List<String> autoConfigPackages = AutoConfigurationPackages.get(beanFactory); | ||
basePackages.addAll(autoConfigPackages); | ||
|
||
String[] beanNames = beanFactory.getBeanNamesForAnnotation(ComponentScan.class); | ||
for (String beanName : beanNames) { | ||
Class<?> beanClass = beanFactory.getType(beanName); | ||
if (beanClass != null) { | ||
ComponentScan componentScan = beanClass.getAnnotation(ComponentScan.class); | ||
if (componentScan != null) { | ||
Collections.addAll(basePackages, componentScan.value()); | ||
Collections.addAll(basePackages, componentScan.basePackages()); | ||
for (Class<?> basePackageClass : componentScan.basePackageClasses()) { | ||
basePackages.add(basePackageClass.getPackage().getName()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return basePackages; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...-boot-starter/src/main/java/dev/langchain4j/service/spring/ClassPathAiServiceScanner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package dev.langchain4j.service.spring; | ||
|
||
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner; | ||
|
||
public class ClassPathAiServiceScanner extends ClassPathBeanDefinitionScanner { | ||
|
||
public ClassPathAiServiceScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters) { | ||
super(registry, useDefaultFilters); | ||
} | ||
|
||
@Override | ||
protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) { | ||
return beanDefinition.getMetadata().isInterface() && beanDefinition.getMetadata().isIndependent(); | ||
} | ||
} |
4 changes: 3 additions & 1 deletion
4
...ain4j-spring-boot-starter/src/main/java/dev/langchain4j/spring/LangChain4jAutoConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
package dev.langchain4j.spring; | ||
|
||
import dev.langchain4j.rag.spring.RagAutoConfig; | ||
import dev.langchain4j.service.spring.AiServiceScannerProcessor; | ||
import dev.langchain4j.service.spring.AiServicesAutoConfig; | ||
import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
import org.springframework.context.annotation.Import; | ||
|
||
@AutoConfiguration | ||
@Import({ | ||
AiServicesAutoConfig.class, | ||
RagAutoConfig.class | ||
RagAutoConfig.class, | ||
AiServiceScannerProcessor.class | ||
}) | ||
public class LangChain4jAutoConfig { | ||
} |