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 就行。


相关推荐
Resean02231 小时前
Spring 源码分析(六)循环依赖与三级缓存
java·spring·缓存
在爪洼山玩泥巴2 小时前
fastjson2 JSONPath 取不存在路径,为什么会抛 “not support”?
java·开发语言·jsonpath·fastjson
智联视频超融合平台2 小时前
视频 AI 的误报之战,正在从调参数卷到改架构
人工智能·大模型·边缘计算·视频监控·安防
水如烟3 小时前
孤能子视角:智能的边界不在算法强弱,在可形式化性——从概率法到因果推理,选择池的边界一直在那里
人工智能
万里侯4 小时前
日志聚类之后别把 noise 扔掉:降噪应该改变视图,不抹掉证据
人工智能·微服务·ai·容器·k8s
澹锦汐4 小时前
纯前端秋日银杏与枫叶落叶色板智能聚类器:基于 K-Means 色彩量化与直方图自动配方提取
前端·人工智能·ai·语言模型·vue·react·k-means
星落梦生11 小时前
SpringAOP基础笔记(7)
java·开发语言
71777712 小时前
国内 AI 研发效能工具怎么选:2026 年基于公开信息的选型框架与 Gitee 实践
人工智能·gitee
金銀銅鐵12 小时前
带您看懂字节码指令(四):一条指令里有什么
java·jvm