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
Changes Made:
- add scanBasePackages(class) as basePackages
- unit test

Relevant Links:
- langchain4j/langchain4j#1593
  • Loading branch information
qing-wq authored Aug 22, 2024
1 parent 34d0962 commit dc5132f
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils;

import java.util.Collections;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -47,6 +50,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));
}
}

return basePackages;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.langchain4j.service.spring.mode.automatic.scanPackages.aiService;

import dev.langchain4j.service.spring.AiService;

@AiService
public interface ScanPackageAiService {

String chat(String userMessage);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.langchain4j.service.spring.mode.automatic.scanPackages.package1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = {"dev.langchain4j.service.spring.mode.automatic.scanPackages.aiService"})
public class ScanPackageAiServiceApplication {

public static void main(String[] args) {
SpringApplication.run(ScanPackageAiServiceApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package dev.langchain4j.service.spring.mode.automatic.scanPackages.package1;

import dev.langchain4j.service.spring.AiServicesAutoConfig;
import dev.langchain4j.service.spring.mode.automatic.scanPackages.aiService.ScanPackageAiService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

import static dev.langchain4j.service.spring.mode.ApiKeys.OPENAI_API_KEY;
import static org.assertj.core.api.Assertions.assertThat;

class ScanPackageAiServiceIT {

ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(AiServicesAutoConfig.class));

@Test
void should_create_AI_service_that_use_scanPackages_value() {

contextRunner
.withPropertyValues(
"langchain4j.open-ai.chat-model.api-key=" + OPENAI_API_KEY,
"langchain4j.open-ai.chat-model.max-tokens=20",
"langchain4j.open-ai.chat-model.temperature=0.0"
)
.withUserConfiguration(ScanPackageAiServiceApplication.class)
.run(context -> {

// given
ScanPackageAiService aiService = context.getBean(ScanPackageAiService.class);

// when
String answer = aiService.chat("What is the capital of Germany?");

// then
assertThat(answer).containsIgnoringCase("Berlin");
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.langchain4j.service.spring.mode.automatic.scanPackages.package2;

import dev.langchain4j.service.spring.mode.automatic.scanPackages.aiService.ScanPackageAiService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackageClasses = ScanPackageAiService.class)
public class ScanPackageAiServiceApplication {

public static void main(String[] args) {
SpringApplication.run(ScanPackageAiServiceApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package dev.langchain4j.service.spring.mode.automatic.scanPackages.package2;

import dev.langchain4j.service.spring.AiServicesAutoConfig;
import dev.langchain4j.service.spring.mode.automatic.scanPackages.aiService.ScanPackageAiService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

import static dev.langchain4j.service.spring.mode.ApiKeys.OPENAI_API_KEY;
import static org.assertj.core.api.Assertions.assertThat;

class ScanPackageAiServiceIT {

ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(AiServicesAutoConfig.class));

@Test
void should_create_AI_service_that_use_scanPackageClass_value() {

contextRunner
.withPropertyValues(
"langchain4j.open-ai.chat-model.api-key=" + OPENAI_API_KEY,
"langchain4j.open-ai.chat-model.max-tokens=20",
"langchain4j.open-ai.chat-model.temperature=0.0"
)
.withUserConfiguration(ScanPackageAiServiceApplication.class)
.run(context -> {

// given
ScanPackageAiService aiService = context.getBean(ScanPackageAiService.class);

// when
String answer = aiService.chat("What is the capital of Germany?");

// then
assertThat(answer).containsIgnoringCase("Berlin");
});
}

}

0 comments on commit dc5132f

Please sign in to comment.