From 1004dca93831000476fb786b1058ac10a867fd1d Mon Sep 17 00:00:00 2001 From: qing-wq <2702461713@qq.com> Date: Wed, 20 Nov 2024 19:06:07 +0800 Subject: [PATCH] fix #2133 Spring boot starter breaks configuration classes --- .../spring/AiServiceScannerProcessor.java | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/langchain4j-spring-boot-starter/src/main/java/dev/langchain4j/service/spring/AiServiceScannerProcessor.java b/langchain4j-spring-boot-starter/src/main/java/dev/langchain4j/service/spring/AiServiceScannerProcessor.java index 02e16ae0..2649b862 100644 --- a/langchain4j-spring-boot-starter/src/main/java/dev/langchain4j/service/spring/AiServiceScannerProcessor.java +++ b/langchain4j-spring-boot-starter/src/main/java/dev/langchain4j/service/spring/AiServiceScannerProcessor.java @@ -46,25 +46,27 @@ private Set getBasePackages(ConfigurableListableBeanFactory beanFactory) } private void addComponentScanPackages(ConfigurableListableBeanFactory beanFactory, Set collectedBasePackages) { - beanFactory.getBeansWithAnnotation(ComponentScan.class).forEach((beanName, instance) -> { - Set componentScans = AnnotatedElementUtils.getMergedRepeatableAnnotations(instance.getClass(), ComponentScan.class); - for (ComponentScan componentScan : componentScans) { - Set basePackages = new LinkedHashSet<>(); - String[] basePackagesArray = componentScan.basePackages(); - for (String pkg : basePackagesArray) { - String[] tokenized = StringUtils.tokenizeToStringArray(this.environment.resolvePlaceholders(pkg), - ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS); - Collections.addAll(basePackages, tokenized); - } - for (Class clazz : componentScan.basePackageClasses()) { - basePackages.add(ClassUtils.getPackageName(clazz)); - } - if (basePackages.isEmpty()) { - basePackages.add(ClassUtils.getPackageName(instance.getClass())); + for (String beanName : beanFactory.getBeanNamesForAnnotation(ComponentScan.class)) { + Class beanClass = beanFactory.getType(beanName); + if (beanClass != null) { + Set componentScans = AnnotatedElementUtils.getMergedRepeatableAnnotations(beanClass, ComponentScan.class); + for (ComponentScan componentScan : componentScans) { + Set basePackages = new LinkedHashSet<>(); + for (String pkg : componentScan.basePackages()) { + String[] tokenized = StringUtils.tokenizeToStringArray(this.environment.resolvePlaceholders(pkg), + ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS); + Collections.addAll(basePackages, tokenized); + } + for (Class clazz : componentScan.basePackageClasses()) { + basePackages.add(ClassUtils.getPackageName(clazz)); + } + if (basePackages.isEmpty()) { + basePackages.add(ClassUtils.getPackageName(beanClass)); + } + collectedBasePackages.addAll(basePackages); } - collectedBasePackages.addAll(basePackages); } - }); + } } private void removeAiServicesWithInactiveProfiles(BeanDefinitionRegistry registry) {