Background
ChatGPT broke through in 2023, the Agent concept landed in 2024, and AI applications went mainstream in 2025. The AI toolkit for Java developers matured rapidly during this period. Spring AI 1.0 GA shipped in May 2025, and after nearly a year of iteration, it’s become the de facto standard for enterprise Java AI applications. This article covers Spring AI comprehensively: why it exists, how the architecture evolved, how it compares to LangChain4J, and how to use it in practice.
What Is Spring AI and Why You Need It
The AI Dilemma for Java Developers
When large language models first took off, Java developers faced an uncomfortable choice. Calling provider APIs directly meant dealing with different interfaces per platform, high switching costs, and no abstractions for RAG, conversational memory, or Agents. Switching to Python frameworks like LangChain or LlamaIndex meant cross-language integration and inconsistent architecture. Over 80% of enterprise Java applications run on Spring, and the market needed an AI framework that fits natively into the Spring ecosystem.
Origin and Evolution
Spring AI’s version timeline:
| Date | Version | Milestone |
|---|---|---|
| Nov 2023 | 0.8.0 | Project launched, first Milestone |
| Throughout 2024 | 0.8.x - 0.9.x | Rapid iteration, community feedback |
| May 2025 | 1.0.0 GA | Released at Spring I/O 2025, production-ready |
| H2 2025 | 1.0.x - 1.1.x | Stable iteration, bug fixes, MCP support added |
| Early 2026 | 1.1.x / 1.2 | Agent capabilities enhanced, Chinese model support matured |
The 1.0 GA release delivered several key milestones: core interfaces like ChatClient, Advisor, and VectorStore stabilized (API frozen); observability, error handling, and configuration management reached enterprise standards (production-ready); official support for 20+ model providers and 10+ vector databases (mature ecosystem); comprehensive documentation and examples with an active community.
Core Value
| Value | Description | Enterprise Benefit |
|---|---|---|
| Unified API abstraction | OpenAI, Azure, Anthropic, Ollama, Chinese models, all behind one interface | Zero code changes when switching models, better vendor negotiation leverage |
| Spring-native integration | Auto-configuration, dependency injection, observability, config center | Zero learning curve for Spring developers, reuse existing infrastructure |
| Enterprise features | Token consumption monitoring, call chain tracing, circuit breaking, security audit | Deploy directly to production, unified operations pipeline |
| Advanced AI abstractions | RAG, Agent, MCP, Function Calling, conversational memory | No need to reinvent the wheel, focus on business logic |
| Chinese model support | Alibaba Qwen, Baidu Wenxin, iFlytek Spark, Tencent Hunyuan | Data compliance, cost control, optimized Chinese-language performance |
Spring AI is essentially the LangChain of the Java world, but it understands enterprise applications better than LangChain does.
Architecture Design
Spring AI 1.0’s architecture has stabilized into four layers: the Application Layer (ChatClient / PromptTemplate / Advisor / Agent), the MCP Protocol Layer (MCP Client / MCP Server / Tool Registry), the Core Abstraction Layer (ChatModel / EmbeddingModel / ImageModel / AudioModel), and the Integration Layer (adapters for OpenAI / Azure / Anthropic / Ollama / Alibaba / VectorDB and other providers).
Core Abstraction Layer: Unified Interface Design
Spring AI defines a stable set of model interfaces. Regardless of which provider sits underneath, the API stays the same.
ChatModel is the core interface for conversational models:
1 | // Core interface definition (stabilized after 1.0 GA) |
EmbeddingModel handles vector embeddings. Version 1.0 added batch embedding capability:
1 | public interface EmbeddingModel extends Model<EmbeddingRequest, EmbeddingResponse> { |
ImageModel handles image generation, supporting DALL-E 3, Stability AI, and Midjourney API:
1 | public interface ImageModel extends Model<ImagePrompt, ImageResponse> { |
Application Layer: High-Level Feature Wrapping
ChatClient is Spring AI 1.0’s recommended high-level API, using a fluent Builder pattern. It wraps conversational configuration, streaming responses, and structured output:
1 |
|
PromptTemplate supports Mustache syntax for dynamic prompt generation:
1 | PromptTemplate template = new PromptTemplate(""" |
Advisor is Spring AI 1.0’s core interceptor mechanism, coming in two types: CallAroundAdvisor (synchronous call interception) and StreamAroundAdvisor (streaming call interception). It inserts custom logic before and after AI calls, enabling chained composition of logging, conversational memory, RAG retrieval, and other features:
1 | // Custom Advisor (official 1.0 API) |
The Agent abstraction was introduced starting from version 1.1, supporting autonomous task decomposition and execution:
1 |
|
MCP Protocol Layer
MCP (Model Context Protocol) is an open protocol launched by Anthropic in 2024 that became the de facto standard for AI tool connectivity in 2025. Spring AI 1.0 officially supports MCP. Its architecture: an MCP Client (running inside Spring AI) connects to multiple MCP Servers (GitHub, filesystem, PostgreSQL, etc.), and the ChatClient Agent orchestrates these external tools through the MCP Client.
1 | // Configure MCP Server |
Integration Layer: Provider Support Status (2026)
| Type | Supported Providers | Status |
|---|---|---|
| Chat models | OpenAI, Azure OpenAI, Anthropic Claude, Google Gemini, Amazon Bedrock, Alibaba Qwen, Baidu Wenxin, iFlytek Spark, Tencent Hunyuan, Ollama | Production-ready |
| Embedding | OpenAI, Azure, Alibaba Qwen, local models | Production-ready |
| Image generation | OpenAI DALL-E 3, Stability AI, Azure | Production-ready |
| Vector databases | Pinecone, Chroma, Weaviate, Milvus, Redis, PostgreSQL/pgvector, MongoDB, Elasticsearch | Production-ready |
| MCP Server | GitHub, Filesystem, PostgreSQL, Slack, Google Drive (official), rich third-party ecosystem | 1.0+ support |
Spring Ecosystem Integration
Spring AI’s biggest advantage is its native fit within the Spring ecosystem, something LangChain4J cannot match.
Auto-Configuration (Spring Boot Starter)
Adding a Starter dependency handles basic configuration. Here’s OpenAI and Alibaba Cloud Qwen as examples:
1 | <!-- OpenAI --> |
Observability
Spring AI automatically integrates with Spring Boot Actuator, giving you zero-config observability for AI calls. Enable tracing and AI observations in configuration, and Actuator automatically reports token consumption (Prompt + Completion), call latency (P50, P95, P99), success rates, embedding latency, and vector search latency.
1 | management: |
Auto-traced Spans cover the full call chain: from Prompt construction (ai-chat-request), through LLM invocation (ai-chat-model-call, including token-count and response-parse), to result processing (ai-chat-response).
Spring Cloud Integration
Dynamic model parameter refresh from config center, combined with circuit breaking to protect AI calls:
1 | // Config center: dynamic model parameter refresh |
Spring Security Integration
API Keys fetched from Vault or a secrets management service, user-level permissions controlled through Spring Security:
1 | // Secure AI API Key management |
Spring AI vs LangChain4J: In-Depth Comparison
LangChain4J is another popular Java AI framework, and the two get compared frequently. From the vantage point of 2026, the comparison results are clear.
Version Maturity
| Framework | Latest Version | Release Date | Maturity |
|---|---|---|---|
| Spring AI | 1.1.x / 1.2 | 2025.05 GA | Production-ready, API stable |
| LangChain4J | 1.0.x | 2025.06 GA | Production-ready, active community |
Both released 1.0 GA in H1 2025 and are roughly equal in production readiness.
Design Philosophy
| Dimension | Spring AI | LangChain4J |
|---|---|---|
| Design philosophy | Spring-native, enterprise-first | Lightweight, independent, framework-neutral |
| Dependency requirements | Requires Spring Boot 3.x | No mandatory dependencies, supports Quarkus/Micronaut/Spring |
| API style | Builder pattern + Advisor chain | Fluent API + AI Services annotations |
| Configuration | YAML auto-config + Actuator | Code-based Builder configuration |
| Ecosystem integration | Seamless full Spring ecosystem | Manual integration with each framework |
Core Capability Comparison
| Capability | Spring AI | LangChain4J | Notes |
|---|---|---|---|
| Chat | ChatClient | ChatLanguageModel | Both mature |
| Embedding | EmbeddingModel | EmbeddingModel | Both mature |
| RAG | QuestionAnswerAdvisor | ContentRetriever + Augmentor | Spring AI is simpler |
| Function Calling | @Bean + @Description | @Tool annotation | LangChain4J is more intuitive |
| Conversational memory | PromptChatMemoryAdvisor | ChatMemoryProvider | Both mature |
| Structured output | .entity(Class) | Returns POJO | Both mature |
| MCP protocol | Native support in 1.0+ | Supported in 1.0+ | Spring AI integrates more deeply |
| Agent | Basic support from 1.1+ | Mature Agent framework | LangChain4J is more mature |
| Observability | Actuator auto-integrated | Requires manual setup | Clear Spring AI advantage |
| Chinese models | Official Alibaba/Baidu/iFlytek support | Requires custom adaptation | Spring AI advantage |
Code Style Comparison
The usage patterns for chat models differ noticeably. Spring AI takes the fluent Builder route; LangChain4J goes with direct construction:
1 | // Spring AI: Fluent Builder |
LangChain4J’s AI Services annotations are its most distinctive design. You define an interface with annotations and get a complete AI service built, including system prompts, user templates, conversational memory, and tool binding:
1 | // LangChain4J's declarative AI Services |
For RAG implementation, Spring AI handles retrieval and context injection automatically through Advisors, while LangChain4J requires manual assembly of ContentRetriever and Augmentor:
1 | // Spring AI: Advisor handles everything automatically |
Agent Capabilities (Key Difference)
LangChain4J’s Agent framework is more mature, supporting tool binding, conversational memory, and autonomous task execution:
1 | // LangChain4J: Complete Agent framework |
Spring AI’s Agent was introduced in version 1.1 and remains at a basic stage:
1 | // Spring AI: Basic Agent capability |
For complex autonomous Agent applications, LangChain4J is currently more mature. For enterprise-grade observable Agents, Spring AI is the better fit.
Selection Guide
The core decision criterion is your project’s tech stack and specific requirements:
| Scenario | Recommendation | Reason |
|---|---|---|
| Spring Boot enterprise project | Spring AI | Best ecosystem fit, default choice |
| Quarkus/Micronaut project | LangChain4J | Better official support |
| Complex autonomous Agent | LangChain4J | More mature Agent framework |
| RAG + observability | Spring AI | Advisor + Actuator combo is cleaner |
| Chinese model compliance | Spring AI | Official Alibaba/Baidu/iFlytek support |
| Rapid prototyping | LangChain4J AI Services | Interface definition is implementation |
Hands-On: Spring AI in Practice
Basic Chat Application
Maven dependency and configuration:
1 | <dependency> |
1 | spring: |
The Controller should inject ChatClient.Builder at construction time and build once, avoiding repeated configuration per request. The example below demonstrates regular calls, streaming responses, and structured output:
1 |
|
Function Calling (Tool Invocation)
Spring AI 1.0’s Function Calling supports two approaches: registering Spring Bean functions with @Bean + @Description, or writing complex functions directly. The LLM decides when to call based on the function description:
1 |
|
RAG in Practice
Spring AI 1.0’s RAG implementation relies on two components: DocumentService handles document ingestion (reading, chunking, vectorizing), and QuestionAnswerAdvisor handles auto-retrieval and context injection during queries.
Document ingestion service requires PDF reader and vector database Starters:
1 | <dependency> |
1 |
|
The core of the RAG query service is QuestionAnswerAdvisor, which handles retrieval and context injection automatically. Bind VectorStore and search parameters at construction time, and the Advisor handles document retrieval on every subsequent call:
1 |
|
For comparison, Spring AI’s RAG core code takes just 3 lines (create Advisor, bind to ChatClient, make the call). LangChain4J requires more configuration steps.
Using Alibaba Cloud Qwen
For domestic projects, Alibaba Cloud is the top choice: good price-performance ratio and strong Chinese-language results. After adding the spring-ai-alibaba-starter dependency, configure the api-key and model name:
1 | <dependency> |
1 | spring: |
1 |
|
Qwen model series selection guide: qwen-turbo is fast and cheap, suitable for simple conversations; qwen-plus is balanced, recommended for daily use; qwen-max has the strongest reasoning, suitable for complex tasks; qwen-vl is multimodal, supporting image understanding.
MCP Tool Integration
After configuring MCP Servers, ChatClient can automatically orchestrate external tools. This example configures GitHub and filesystem MCP Servers:
1 | # Configure MCP Servers |
1 |
|
Pitfall Guide
Common Issues
| Problem | Solution |
|---|---|
| API Key management chaos | Use Spring Cloud Vault or config center; never hardcode |
| Token consumption out of control | Enable Actuator, configure budget alerts, use maxTokens limits |
| Poor RAG quality | Adjust Top-K, similarity threshold, chunking strategy; try different embedding models |
| Slow responses | Use streaming responses, use smaller models for simple requests, warm up connections |
| Hallucination | RAG + strict system prompts, set “unable to answer” fallback |
| Chinese model instability | Multi-model backup, configure fallback chains |
Best Practices
- Use ChatClient, not ChatModel directly: ChatClient has better wrapping and Advisor chain processing
- Use QuestionAnswerAdvisor for RAG: 3 lines of code, don’t manually assemble context
- Keep Function definitions clear: write @Description with purpose and parameters to help LLM invoke correctly
- VectorStore selection: Chroma for prototyping, Milvus/Qdrant for production, Redis/pgvector for small datasets
- Always enable Observability: configure before going live, AI calls must have tracing
- Chinese model compliance: data stays domestic with Alibaba/Baidu/iFlytek, costs remain controllable
Production Deployment Checklist
- [ ] API Keys from config center/Vault
- [ ] Observability enabled, token consumption monitoring
- [ ] Circuit breaker configuration (AI call failure fallback)
- [ ] Streaming response (reduces time-to-first-token)
- [ ] Log sanitization (Prompts may contain sensitive info)
- [ ] VectorStore persistence configured
- [ ] Multi-model backup (OpenAI backed by Qwen, and vice versa)