Spring AI 从入门到精通-Spring AI 是什么

1. 先别急着写代码------搞懂 Spring AI 是什么

1.1 故事时间:三个程序员和 AI

想象一下,你公司有三个程序员,老板说:"给我们的系统加上 AI 功能。"

程序员 A 直接去调 OpenAI 的 HTTP API。他写了 500 行代码处理请求、解析 JSON、管理连接池、处理错误重试。两个月后,老板说"换成 Claude 吧"。程序员 A 哭了。

程序员 B 用了 LangChain。Python 写得很爽,但公司项目是 Java/Spring Boot 的。他需要维护两套代码、两套部署、两套监控。他也很累。

程序员 C 用了 Spring AI。他把 spring-ai-openai-starter 换成 spring-ai-anthropic-starter,改了一行配置,其他代码完全不用动。他下班了。

这就是 Spring AI 的核心价值:可移植性。 它在 Spring 生态上构建了一层抽象,让你用统一的 API 操作不同的 AI 模型,就像 JDBC 让你用统一的 API 操作不同的数据库。

1.2 Spring AI 到底是什么?

用一句话说:Spring AI 是一个让你用 Spring 的方式构建 AI 应用的框架。

用一段话说:它把 Spring 的设计哲学------可移植的 Service Abstraction、模块化设计、POJO 驱动------搬到了 AI 领域。你不需要关心底层是 OpenAI 还是 Anthropic 还是 Ollama 本地模型,你只需要跟 Spring AI 的接口打交道。
#mermaid-svg-h0WDgKc62yfD3GLF{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-h0WDgKc62yfD3GLF .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-h0WDgKc62yfD3GLF .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-h0WDgKc62yfD3GLF .error-icon{fill:#552222;}#mermaid-svg-h0WDgKc62yfD3GLF .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-h0WDgKc62yfD3GLF .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-h0WDgKc62yfD3GLF .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-h0WDgKc62yfD3GLF .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-h0WDgKc62yfD3GLF .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-h0WDgKc62yfD3GLF .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-h0WDgKc62yfD3GLF .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-h0WDgKc62yfD3GLF .marker{fill:#333333;stroke:#333333;}#mermaid-svg-h0WDgKc62yfD3GLF .marker.cross{stroke:#333333;}#mermaid-svg-h0WDgKc62yfD3GLF svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-h0WDgKc62yfD3GLF p{margin:0;}#mermaid-svg-h0WDgKc62yfD3GLF .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-h0WDgKc62yfD3GLF .cluster-label text{fill:#333;}#mermaid-svg-h0WDgKc62yfD3GLF .cluster-label span{color:#333;}#mermaid-svg-h0WDgKc62yfD3GLF .cluster-label span p{background-color:transparent;}#mermaid-svg-h0WDgKc62yfD3GLF .label text,#mermaid-svg-h0WDgKc62yfD3GLF span{fill:#333;color:#333;}#mermaid-svg-h0WDgKc62yfD3GLF .node rect,#mermaid-svg-h0WDgKc62yfD3GLF .node circle,#mermaid-svg-h0WDgKc62yfD3GLF .node ellipse,#mermaid-svg-h0WDgKc62yfD3GLF .node polygon,#mermaid-svg-h0WDgKc62yfD3GLF .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-h0WDgKc62yfD3GLF .rough-node .label text,#mermaid-svg-h0WDgKc62yfD3GLF .node .label text,#mermaid-svg-h0WDgKc62yfD3GLF .image-shape .label,#mermaid-svg-h0WDgKc62yfD3GLF .icon-shape .label{text-anchor:middle;}#mermaid-svg-h0WDgKc62yfD3GLF .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-h0WDgKc62yfD3GLF .rough-node .label,#mermaid-svg-h0WDgKc62yfD3GLF .node .label,#mermaid-svg-h0WDgKc62yfD3GLF .image-shape .label,#mermaid-svg-h0WDgKc62yfD3GLF .icon-shape .label{text-align:center;}#mermaid-svg-h0WDgKc62yfD3GLF .node.clickable{cursor:pointer;}#mermaid-svg-h0WDgKc62yfD3GLF .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-h0WDgKc62yfD3GLF .arrowheadPath{fill:#333333;}#mermaid-svg-h0WDgKc62yfD3GLF .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-h0WDgKc62yfD3GLF .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-h0WDgKc62yfD3GLF .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-h0WDgKc62yfD3GLF .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-h0WDgKc62yfD3GLF .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-h0WDgKc62yfD3GLF .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-h0WDgKc62yfD3GLF .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-h0WDgKc62yfD3GLF .cluster text{fill:#333;}#mermaid-svg-h0WDgKc62yfD3GLF .cluster span{color:#333;}#mermaid-svg-h0WDgKc62yfD3GLF div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-h0WDgKc62yfD3GLF .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-h0WDgKc62yfD3GLF rect.text{fill:none;stroke-width:0;}#mermaid-svg-h0WDgKc62yfD3GLF .icon-shape,#mermaid-svg-h0WDgKc62yfD3GLF .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-h0WDgKc62yfD3GLF .icon-shape p,#mermaid-svg-h0WDgKc62yfD3GLF .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-h0WDgKc62yfD3GLF .icon-shape .label rect,#mermaid-svg-h0WDgKc62yfD3GLF .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-h0WDgKc62yfD3GLF .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-h0WDgKc62yfD3GLF .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-h0WDgKc62yfD3GLF :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 具体实现
Spring AI 抽象层
你的 Spring Boot 应用
ChatClient
EmbeddingModel
VectorStore
ChatModel 接口
EmbeddingModel 接口
VectorStore 接口
OpenAI
Anthropic
Ollama
PGVector
Redis
Chroma

1.3 Spring AI 能做什么?

先给你一个全景图,后面每个模块都会展开讲:

模块 干什么的 类比
ChatClient 跟 AI 对话的入口 JDBC 的 JdbcTemplate
ChatModel 不同 AI 模型的抽象 JDBC 的 Driver
Embedding 把文本变成向量数字 翻译官,把"意思"翻译成数字
VectorStore 存向量、搜向量 AI 的外部记忆库
RAG 检索文档 + AI 生成回答 考试时翻书再答题
ETL 把文档变成可检索的数据 食材加工流水线
Tool Calling 让 AI 调用你的函数 给 AI 装上手和脚
Advisors 拦截和增强 AI 请求 Spring 的 Interceptor
Chat Memory 对话历史管理 聊天记录本
MCP 标准化的工具/资源协议 USB 接口标准
Structured Output 让 AI 输出 Java 对象 不是散文,是填表
Observability 监控和指标 仪表盘
Testing 评估 AI 输出质量 单元测试,但是给 AI 的

1.4 版本和依赖

Spring AI 1.0.0 已经发布到 Maven Central,支持 Spring Boot 3.4.x 和 3.5.x。

Maven:

xml 复制代码
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-bom</artifactId>
            <version>1.0.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-starter-model-openai</artifactId>
    </dependency>
</dependencies>

Gradle:

groovy 复制代码
dependencies {
    implementation platform("org.springframework.ai:spring-ai-bom:1.0.0")
    implementation 'org.springframework.ai:spring-ai-starter-model-openai'
}

⚠️ 注意: 如果你用的是快照版(Snapshot),需要额外配置 Spring Snapshot 仓库。1.0.0 正式版直接用 Maven Central 就行。


相关推荐
不知名的老吴1 小时前
机器学习评价之基础指标
人工智能·算法·机器学习
申通之声1 小时前
3年稳定率90%+:申通五星管理经
大数据·人工智能·交通物流
陕西企来客1 小时前
西安豆包获客技巧深度解析:核心问题与原因分析
人工智能
XiYang-DING1 小时前
【Java EE】TOCTOU
java·java-ee
超人也会哭️呀1 小时前
视觉模型中的坐标漂移
人工智能·ai·llm·ocr·vlm·视觉模型·dots.ocr
.千余1 小时前
【C++】 String 常用操作:增删查改 | 查找 | 截取 | IO
java·服务器·开发语言·c++·笔记·学习
长栎1 小时前
面试官说你的单例线程不安全,你真能现场修好?
java
码云骑士1 小时前
【Java基础】JDK安装常见问题教辅-从踩坑到排雷
java·开发语言