Unlock Spring AI: Build Java Generative Apps with Model Switching, Memory, and Prompt Engineering
This article introduces Spring AI, explains its relationship to Spring Boot, outlines support for major AI model providers and capabilities, and provides step‑by‑step code examples for a chatbot, conversational memory, and prompt engineering, while highlighting version pitfalls and future extensions.
What is Spring AI?
Spring AI is a Java library that simplifies the development of applications with generative AI features, much like Spring Boot simplifies Spring applications. It aims to reduce the complexity of integrating AI models, data sources, and APIs, making Java a first‑class language for AI development.
Supported Model Providers and Types
Spring AI supports the major large‑model providers, including Anthropic, OpenAI, Microsoft, Amazon, Google, and Ollama. The library abstracts the following model capabilities:
Chat Completion
Embedding
Text‑to‑Image
Audio Transcription
Text‑to‑Speech
Content Moderation
These providers can be switched at runtime with second‑level latency, greatly improving development efficiency.
Getting Started: Hello‑World Chatbot
The example uses Spring AI 1.0 with the openai and ollama starters to demonstrate dynamic model switching. It is important to avoid the 1.1.0‑M7 snapshot because its Maven dependencies cannot be resolved.
A configuration class imports the required starters and defines the model selection. The controller class contains a method that calls the model and returns the response. Running the application produces a simple “Hello World” response from the chosen AI model.
Conversational Memory
Conversational Memory enables the system to retain context across multiple turns, allowing coherent multi‑turn interactions. The demo adds a conversationId parameter to link requests to the same memory store.
When the same conversation ID (e.g., 1) is used, the model correctly remembers the previous question. Changing the ID to a new value (e.g., 2) resets the memory, demonstrating the dependency on the conversation identifier.
Beyond in‑memory storage, Spring AI also offers MySQL, PostgreSQL, Cassandra, Neo4j, and other persistence options for memory data.
Prompt Engineering
Prompt Engineering bridges human intent and model output by crafting precise instructions. It involves four elements—role, task, context, and output format—and aims to clarify intent, control output format, reduce hallucinations, and adapt to specific domains such as healthcare or finance.
The demo implements a completionPrompt method with three steps:
Create a prompt template that includes role, task, context, and desired output.
Inject variable parameters into the template.
Invoke the model and return the result.
Sample runs show that providing the parameter “洞见” yields a modern‑style Chinese paragraph, while the parameter “李白” produces a classical‑style Chinese text, illustrating how prompt design influences output style.
Conclusion
The article covers the basics of Spring AI, including model support, conversational memory, and prompt engineering. Future posts will explore Retrieval‑Augmented Generation (RAG), multimodal capabilities, Model‑Centric Programming (MCP), and vector‑store integrations.
Senior Tony
Former senior tech manager at Meituan, ex‑tech director at New Oriental, with experience at JD.com and Qunar; specializes in Java interview coaching and regularly shares hardcore technical content. Runs a video channel of the same name.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
