spring ai sample code examples and tutorials

Spring AI is a revolutionary framework that integrates artificial intelligence capabilities into the popular Spring Boot platform. Whether you're a seasoned developer looking to explore AI or a curious beginner, understanding Spring AI's sample code is a great way to start. This article will provide an in-depth look at Spring AI's sample code, guiding you through its intricacies and helping you get started with AI-powered applications.

【Color Palette】虹を閉じ込めて|A Trapped Rainbow
【Color Palette】虹を閉じ込めて|A Trapped Rainbow

Before diving into the code, it's essential to understand what makes Spring AI unique. Spring AI simplifies AI integration by providing out-of-the-box Deep Java Library (DJL) support, allowing developers to build, train, and deploy AI models directly within their Spring Boot applications. Now, let's explore the Spring AI sample code and see what it entails.

Dreamy Spring Color Palette That'll Transform Your Next Project
Dreamy Spring Color Palette That'll Transform Your Next Project

Setting Up a Spring AI Project

The first step in understanding the Spring AI sample code is to set up a Spring AI project. Spring AI uses Spring Boot's starter dependency management to get started quickly. In your Maven or Gradle build file, add the Spring AI starter dependency:

AI Developer Prompting Cheat Sheet (2026) | Copy-Paste Prompts for Coding, Debugging & Refactoring
AI Developer Prompting Cheat Sheet (2026) | Copy-Paste Prompts for Coding, Debugging & Refactoring

```xml org.springframework.ai spring-ai-starter 0.2.0.M2 ```

Once the dependency is added, you're ready to explore the Spring AI sample code. Let's dive into some of the essential components and examples provided by Spring AI.

NDarrays and Tensors in Spring AI

2025 saw a massive shift in how we perceive coding.  It's 2026 now, and companies are still lagging behind. I used to think you needed developers to build products. Then I launched… | Chris Donnelly | 216 comments
2025 saw a massive shift in how we perceive coding. It's 2026 now, and companies are still lagging behind. I used to think you needed developers to build products. Then I launched… | Chris Donnelly | 216 comments

Spring AI's sample code introduction begins with exploring NDarrays and tensors, fundamental data structures in AI. NDarrays are multi-dimensional arrays, while tensors represent complex data structures consisting of arrays. Spring AI's sample code demonstrates creating and manipulating NDarrays and tensors using DJL:

```java import org.nd4j.jita.conf.CUDAExecution; import org.nd4j.jita.conf.NativeTag; import org.springframework.ai.ndarray.NDManager; CUDAExecution.getInstance().getConfiguration().allowMultiGPU(false); CUDAExecution.getInstance().getConfiguration().blocking(false); NDManager ndManager = NDManager.newBaseManager(); NDArray tensor = ndManager.create(new float[][]{{1, 2, 3}, {4, 5, 6}}, 'f'); ```

The above code snippet demonstrates creating an NDManager instance and initializing an NDArray representing a 2D tensor with float data type.

IAE peripherals and NNservices in Spring AI

Canva Spring Aesthetic Hex Codes
Canva Spring Aesthetic Hex Codes

IAE (Intelligent Application Environment) peripherals and NNs (Neural Networks) services are essential components in Spring AI that facilitate AI model training and inference. The sample code demonstrates how to create IAE peripherals and NNs services, enabling communication between your Spring Boot application and AI models:

```java import org.springframework.ai.iae.peripherals.file.FilePeripheral; import org.springframework.ai.nnservices.NNService; FilePeripheral filePeripheral = new FilePeripheral("model.ndjson"); NNService nnService = new NNService(filePeripheral); ```

The above code snippet creates a FilePeripheral for loading a pre-trained AI model and initializes an NNService to interact with the model.

AI Model Training and Inference with Spring AI

Стеклянное сердце /Sora prompt
Стеклянное сердце /Sora prompt

Another crucial aspect of the Spring AI sample code is the demonstration of AI model training and inference. Spring AI utilizes DJL to build and train AI models, making the process accessible to Spring Boot developers.

Building and Training an AI Model with Spring AI

10 Best AI Code Generators to Try in 2025#ai #codingtools #aitools #programming #developers
10 Best AI Code Generators to Try in 2025#ai #codingtools #aitools #programming #developers
a spiral notebook with the words 30 algotriums written on it and colored pencils next to it
a spiral notebook with the words 30 algotriums written on it and colored pencils next to it
30+ Best Artificial Intelligence Project Ideas with Source Code
30+ Best Artificial Intelligence Project Ideas with Source Code
AI Code Generator Tool Online – Generate Smart Code Instantly
AI Code Generator Tool Online – Generate Smart Code Instantly
a poster with many different types of logos and numbers on it's sides, including the
a poster with many different types of logos and numbers on it's sides, including the
QR Code AI Art Generator
QR Code AI Art Generator
AI Image Style Guide: Words That Make Prompts Look Like Real Photos
AI Image Style Guide: Words That Make Prompts Look Like Real Photos
a woman in a white dress with flowers on her head and veil over her shoulders
a woman in a white dress with flowers on her head and veil over her shoulders
four different colors with the names of each type in them, including pink, green, blue
four different colors with the names of each type in them, including pink, green, blue
Free Printable Spring Coding Worksheets
Free Printable Spring Coding Worksheets
25 Magical AI Prompt Ideas for Children's Storybooks
25 Magical AI Prompt Ideas for Children's Storybooks
AI Tools & Productivity
AI Tools & Productivity
6 Free AI Apps That Replace Every Paid Subscription in 2026
6 Free AI Apps That Replace Every Paid Subscription in 2026
Prompt for Midjourney | AI Generatoin | free | KREA
Prompt for Midjourney | AI Generatoin | free | KREA
Fields of Flower - Beautiful and Relaxing Vibes
Fields of Flower - Beautiful and Relaxing Vibes
Krishna Agrawal (@Krishnasagrawal) on X
Krishna Agrawal (@Krishnasagrawal) on X
Want to Grow Faster? Use Chapple AI Code Generator
Want to Grow Faster? Use Chapple AI Code Generator
15 Powerful AI Agents for Automation, Coding & Productivity (2026 Guide)
15 Powerful AI Agents for Automation, Coding & Productivity (2026 Guide)
Top Free AI Coding Tools in 2026 – Best AI Tools for Developers
Top Free AI Coding Tools in 2026 – Best AI Tools for Developers
an anime character with long blonde hair
an anime character with long blonde hair

The sample code illustrates how to create a simple feed-forward neural network using DJL and train it using the MNIST handwritten digit dataset:

```java import org.deeplearning4j.nn.conf.WorkspaceMode; import org.deeplearning4j.nn.graph.ComputationGraph; import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; import org.springframework.ai.nn.recipes.mnist.MnistDataIterator; DataSetIterator mnistTrain = new MnistDataIterator(60000, 10000, true); DataSetIterator mnistTest = new MnistDataIterator(20000, 10000, false); ComputationGraph model = new ComputationGraph(cg); model.fit(mnistTrain); ```

The above code snippet demonstrates using the MNISTDataIterator to load the MNIST dataset, creating a simple neural network, and training the model using the training data.

AI Model Inference with Spring AI

After training your AI model, Spring AI enables seamless integration with your Spring Boot application for model inference:

```java import org.nd4j.jita.conf.CUDAExecution; import org.springframework.ai.nn.recipes.mnist.MnistInference; CUDAExecution.getInstance().getConfiguration().blocking(false); CUDAExecution.getInstance().getConfiguration().allowMultiGPU(false); NNService nnService = new NNService(new FilePeripheral("model.ndjson")); MnistInference mnistInference = new MnistInference(nnService); int result = mnistInference.infer(imageData); // imageData is the input image to classify ```

The above code snippet demonstrates using the pre-trained MNIST model for inference, passing an input image, and retrieving the predicted digit.

Spring AI's sample code provides an extensive range of examples, guiding developers through various aspects of AI integration using Spring Boot. By exploring the Spring AI sample code, you'll gain a solid understanding of the framework and be well-equipped to build AI-powered applications. Start your journey with Spring AI today and unlock the full potential of AI integration with your favorite application platform.

Related Articles

Sample R Code How To Run Python Script Step By Step Sample Python Codes For Beginners Data Science Code Example Python Random Address How To Write Ai In Python Crewai Sample Code Python Gui Sample Gurobi Python Example Simple Python Game Code Github