AI / LangChain4j interview questions
How do you integrate LangChain4j with Spring Boot?
LangChain4j provides a dedicated Spring Boot starter (langchain4j-spring-boot-starter) that wires everything up through standard Spring Boot auto-configuration. You add the starter plus the provider-specific starter for your chosen LLM, drop configuration into application.properties, and Spring automatically creates the ChatLanguageModel, EmbeddingModel, and related beans that you can inject anywhere in the application.
<!-- pom.xml --> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-spring-boot-starter</artifactId> <version>0.32.0</version> </dependency> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-open-ai-spring-boot-starter</artifactId> <version>0.32.0</version> </dependency>
# application.properties langchain4j.open-ai.chat-model.api-key=${OPENAI_API_KEY} langchain4j.open-ai.chat-model.model-name=gpt-4o langchain4j.open-ai.chat-model.temperature=0.7 langchain4j.open-ai.embedding-model.api-key=${OPENAI_API_KEY}
For AI Services specifically, Spring Boot integration uses the @AiService annotation (or you declare a @Bean manually). LangChain4j detects annotated interfaces during component scan and creates Spring-managed proxy beans — meaning the AI service is injectable like any other Spring component:
@AiService interface CustomerSupportAgent { @SystemMessage("You are a helpful customer support agent.") String chat(String userMessage); } @RestController class SupportController { private final CustomerSupportAgent agent; SupportController(CustomerSupportAgent agent) { this.agent = agent; } @PostMapping("/support") String support(@RequestBody String message) { return agent.chat(message); } }
More Related questions...