FreeSpring AI · 45 min · Intermediate
🌱

Spring AI Deep Dive

ChatClient, Advisors and the full Spring AI architecture

💡

What Is Spring AI Deep Dive?

Simple overview · Real-world examples included

You have made one AI call. Now let us understand the full Spring AI framework — the toolkit that makes all of this possible without writing boilerplate code. We will learn the ChatClient (main interface), the Advisor pattern (middleware for AI calls), and how to switch between OpenAI and Claude by changing a single config value — zero code changes. This is the foundation that every other topic in this curriculum builds on.

🎯 What You'll Learn

ChatClient builder
Advisor pattern
Multi-provider switching
Auto-configuration

🌍 Real-World Use Case

Enterprise chatbot that falls back from GPT-4o to Claude during outages

📊 How It Works — Interactive Flowchart

Hover nodes for quick tip · Click to open detail · Press "Run Data Flow" to animate

Click any box to learn what it does
🧑‍💻
Your Code
ChatClient.prompt()
Step 1
⚙️
Advisor Chain
Middleware
Step 2
🔢
ChatClient
Core interface
Step 3
🌐
Provider
OpenAI/Claude
Step 4
🤖
AI Model
Any provider
Step 5
Response
Same code
Step 6

🛠️ Step-by-Step Implementation

Click any step to expand · Check the circle to mark complete · Progress saves in your browser

Implementation ProgressClick ○ to mark a step complete
0 / 6
01
Understand ChatClient architecture
💡 WHY AM I DOING THIS?

ChatClient is the central abstraction. Understanding it unlocks all Spring AI features.

▶ WHAT HAPPENS IN THIS STEP

Learn the Builder → Client → Advisor → Model chain.

📄 FILES TO CREATE / MODIFY
+AiConfig.java
📁 FOLDER STRUCTURE
config/AiConfig.java
💻 CODE EXAMPLE
// ChatClient.Builder is auto-injected by Spring AI
@Bean
public ChatClient chatClient(ChatClient.Builder builder) {
    return builder
        .defaultSystem("You are a helpful Java assistant.")
        .defaultAdvisors(
            new MessageChatMemoryAdvisor(chatMemory),
            new SimpleLoggerAdvisor()
        )
        .build();
}
⚠ COMMON MISTAKES
Creating ChatClient directly — always use the Builder bean
▶ HOW TO TEST
Check logs for "ChatClient initialized" with your advisors listed
✅ EXPECTED RESULT
Startup logs show advisor chain: Logger → Memory → Model
02
Add Advisor chain
Advisors are Spring AI's equivalent of interceptors — they run before/after every LLM call
03
Configure multiple providers
Different LLMs excel at different tasks
04
Build provider router
Route simple questions to cheap models, complex to expensive — saves 60-80% on API costs
05
Implement provider fallback
LLM APIs have outages and rate limits
06
Test multi-provider setup
Integration tests verify the full provider chain works end-to-end

✍️ How to Write the Prompt for This Topic

Use PromptTemplate with {placeholders} for all dynamic content. Set defaultSystem() on ChatClient builder.

🎯

Job Interview Prep

8 curated questions · 0 / 8 mastered
Click answers, then rate yourself to track progress
LEVEL:

💡 Don't memorise answers word-for-word. Understand the concept, then explain it in your own words using a real example from this topic's project — that's what impresses interviewers.

⚡ Live AI Demo

LIVE AI DEMO — CLAUDE HAIKUSign up for 3 free demos/day
0/500

📦 Clone & Run the Sample Project

📦
github.com / ai-dev-academy / ai-dev-academy-projects

05-spring-ai-deep-dive

Multi-provider chatbot — switch OpenAI/Claude with zero code changes

Spring AI 1.xSpring Boot 3.xMultiple LLM ProvidersJava 17
QUICK START — RUN IN 5 STEPS
1
Clone the full repo
git clone https://github.com/ai-dev-academy/ai-dev-academy-projects.git
2
Enter the topic folder
cd ai-dev-academy-projects/05-spring-ai-deep-dive
3
Set up your API key
cp .env.example .env
# Then open .env and add your API key
4
Run the project
mvn spring-boot:run
5
Test the endpoint
curl -X POST http://localhost:8080/ai/chat \
  -H "Content-Type: application/json" \
  -d '"Hello, what is Spring AI Deep Dive?"'
OPEN IN YOUR IDE
← DashboardAll Topics →