Let’s explore the Spring AI framework and its advantages, and look at how it is helping Java developers adopt AI.
Generative AI (GenAI) has gained significant prominence in the last couple of years. This technology breakthrough has created the possibility of integrating various innovative use cases into applications across domains. Organisations and developer communities are exploring tools and frameworks that allow them to integrate GenAI use cases into their existing applications. The Spring AI framework is one such great open source tool that helps Java developers to rapidly adopt GenAI.
Rise of AI and generative AI (GenAI)
Creating AI models and solutions around AI has traditionally been very time consuming as well as resource- and cost-intensive. This has led to innovations around AI being limited to a few organisations with very deep pockets. But with tech advancements like the adoption of GPUs; and availability of foundational models from tech giants like Google, Meta, OpenAI, etc, building AI models has become cheaper and easier. Many of the popular foundation models are now available as services. Also, many open source models are being developed. Apart from the generic ones, various domain-specific models are also being built to address the needs of targeted domains. Prompt engineering and retrieval augmented generation (RAG) are helping make GenAI more usable for various innovative use cases. Developers can worry less about the model intricacies and can focus more on how to make best use of the models.
All these options require organisations to experiment with the various models/AI platforms available and choose the right ones that provide them with the best solution. Oganisations and developer communities must try and benefit from tools and frameworks that allow them to quickly explore, evaluate and integrate GenAI into their existing applications.
Prevalence of Java applications
In the last three decades, Java has become the default programming language for enterprise development. It is most prevalent in all general applications (small to large), across all organisations (small to large) and in almost all domains and industries. Libraries, tools, frameworks and platforms have been developed by open source communities to support the Java application ecosystem. Frameworks like Spring and Spring Boot are great examples. These innovative frameworks have tremendously helped and transformed Java application development, making it easier and quicker.
Spring AI: Meeting a need
Rapid developments and technical advancements are happening in the GenAI space. At the same time, many organisations have a large ecosystem of Java applications. They want to experiment, evaluate and integrate GenAI into their applications to benefit from the innovations that it uncovers. The Spring open source community has released the Spring AI framework to meet this need. This framework will ease the integration of AI and GenAI into enterprise Java applications and unlock innovative use cases that reach end users quickly.
Ease of configuration
The Spring AI framework has been built with ease of use and portability in mind. Using Spring AI, developers can effortlessly add AI capabilities to their Spring Boot applications. Spring AI supports all the major AI model providers such as Google, Microsoft, Amazon, OpenAI, Anthropic and Ollama. And it provides various Spring Boot starter modules for developers to integrate with AI models.
Here is the simple sequence of steps for integrating AI functionality into a Spring Boot application.
- Add provider specific Spring Boot starter module to the project dependencies.
- Example: Maven dependency for Vertex AI Gemini
<dependency> <groupId>org.springframework.ai< /groupId > <artifactId>spring-ai-vertex-ai-gemini-spring-boot-starter< /artifactId > < /dependency > |
- Define configuration entries to specify model end point details.
- Sample configuration (in application.properties) for Google Gemini model.
# Gemini Vertex AI configuration spring.ai.vertex.ai.gemini.project- id =${DEFAULT_PRJ} spring.ai.vertex.ai.gemini.location=us-central1 spring.ai.vertex.ai.gemini.chat.options.model=gemini-1.5-flash |
- Use various abstractions provided by Spring AI to interact with the AI model.
- ChatModel: For chatting and text generation along with multi-modality [Text. Media to text].
- ImageModel: For generating images [Text to image].
- AudioModel: For transcription and text-to-speech use cases.
- EmbeddingModel: For generating embeddings from text, images and videos.
- Based on the dependencies available in the class-path, Spring AI performs the necessary plumbing to instantiate model provider-specific classes and make them available to code via generic interfaces.
private final ChatClient chatClient; private final ChatModel chatModel; @Autowired public SpringAIController(ChatModel chatModel) { chatClient = ChatClient.builder(chatModel).build(); this.chatModel = chatModel; } |
Since Gemini-spring-boot-starter is available in the class-path, Spring AI automatically creates an instance of VertexAiGeminiChat and makes it available via the ChatModel interface in the above code.
Only organisations that can pivot quickly, with minimal effort, will be able to survive in this dynamic and competitive environment. Spring AI provides the flexibility to teams that want to switch from one model provider to another. With all the abstraction layers provided by the Spring AI framework, changing a model provider is possible with just configuration changes — no code changes are needed.
The code examples given above use Google Gemini as the backend model. Switching to another model provider like Ollama is as simple as changing the configuration. A sample configuration for using Ollama with Spring AI is given below.
1. Replace Google Gemini Maven dependency with Ollama starter module, as shown below.
<dependency> <groupId>org.springframework.ai< /groupId > <artifactId>spring-ai-ollama-spring-boot-starter< /artifactId > < /dependency > |
2. In the application.properties configuration file, specify the Ollama end point and model to be used.
#Ollama Configuration spring.ai.ollama.base-url=http: //localhost :11434 spring.ai.ollama.chat.options.model=tinyllama |
With this configuration, Spring AI will automatically initialise Ollama-specific classes for ChatModel, etc, and make them available.
Implementing GenAI use cases using Spring AI
Let’s look at how to use Spring AI code and classes to implement and integrate various GenAI use cases into applications. The sample code and the responses given below are based on using Google as the model provider and Gemini-flash model as shown in the configuration section above.
Text and language handling
Implementing a text or language based GenAI use case usually implies either processing text or a QA chat scenario. Both can be done using a Spring AI ChatClient interface as depicted in the code below. Consider the following example use case where we ask the model to explain the binary search algorithm in five points. The Java code using Spring AI is:
String userPrompt = “Explain binary search algorithm in 5 bullet points”; Prompt prompt = new Prompt(userPrompt); String response = chatClient.prompt(prompt).call().content(); |
Sample output from the above code is:
Here’s a breakdown of the binary search in five bullet points. Sorted data: Binary search works only on sorted data (ascending or descending). Divide and conquer: It repeatedly divides the search interval in half. Midpoint check: It compares the target value to the middle element of the current interval. Narrowing the search: If the target is less than the middle, the search continues in the left half; if greater, it continues in the right half. Efficient: Binary search is highly efficient, with a time complexity of O(log n), making it much faster than linear search for large datasets. |
We can see the model has generated a response of five points according to the text prompt provided in the input.
Multimodal support
Multimodal processing, which is the ability to process images, audio and video content, is one of the biggest achievements of GenAI models. Extracting text from media, summarising information from audio or video files, and the ability to generate output based on the input media are significant use cases that are made possible by GenAI models. And thanks to Spring AI, integration of such capabilities into regular applications is possible with just a few lines of code.
Consider the following multi-model use case of generating text output from media. We provide a picture of the flight departure schedule to the model and ask it to analyse the image and provide the list of flights meeting a criterion. The Java code using Spring AI is:
String userPrompt = “Analyze the content in provided image, and provide list of flights that are not delayed or cancelled”; ClassPathResource resource = new ClassPathResource(“ /flight_schedule .png “); UserMessage message = new UserMessage(userPrompt, List.of(new Media(MimeTypeUtils.IMAGE_PNG,resource))); ChatResponse response = chatModel.call(new Prompt(List.of(message))); |
The image supplied to the above code snippet is shown in Figure 1.

Sample output from the above code is:
The following flights are not delayed or cancelled: - 12:04 PARIS (A23 ON TIME) - 12:19 HONG KONG (B25 ON TIME) - 12:21 BERLIN (B17 ON TIME) - 12:23 PEKING (A07 ON TIME) |
The power of the GenAI models is evident from the response generated above where the model parsed the media and extracted the information, and generated output text based on the instructions provided in the input prompt.
Support for RAG
Spring AI offers VectorStore abstraction, which can be used to interact with various vector databases. This makes it possible to add retrieval augmented generation (RAG) capabilities to Java applications. Advanced capabilities like RAG are very helpful for organisations in enriching the commoditised GenAI models with domain-specific and industry-specific knowledge. This makes the models more context-aware, and helps in generating tuned, custom results that are more useful compared to generic responses.
These are only a few of the key features provided by Spring AI. The community has also added a lot of critical supporting capabilities that makes the Spring AI framework truly enterprise-grade for use. Features like observability provide insights into AI related operations. Interfaces like ‘Evaluator’ help to evaluate AI models and can be used to protect applications from problems like hallucinations by AI models.
The Spring open source frameworks have served Java applications for decades. Their innovations, stability and support have immensely benefited the Java ecosystem. Spring AI is another great framework that is being enhanced iteratively with great features. This will help the Java developer ecosystem and organisations to quickly integrate their applications with the GenAI tech advancements.