Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #1593 SpringBoot scanBasePackages does not take effect #36

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
});
}

}
Loading