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 分钟前
智慧无人机图像识别分析技术
人工智能·目标检测·计算机视觉·目标跟踪·视觉检测·无人机
阿俊-全栈开发12 分钟前
LikeShop 多商户 Java 版商城微服务模块拆分与部署架构演进实践
java·微服务·架构·likeshop开源商城·多商户源码
染指111021 分钟前
162.Agent-LangGraph-检查点的作用
人工智能·python·langchain·agents
FII工业富联科技服务22 分钟前
从灯塔工厂到规模化智能运营:Omniverse Sim2Real与多智能体协同架构解析
人工智能·数字孪生·ai智能体·工业机器人·omniverse·柔性制造·灯塔工厂
合尘猫25 分钟前
6GB 笔记本本地跑 Z-Image Turbo:一次从 cu121 到 cu130 的踩坑记录
人工智能·ai·大模型·本地部署
2401_8322981026 分钟前
当思考本身被解放:AI 时代的人与机器
人工智能
北方的银狐-Zero28 分钟前
Palantir 平替产品出世:OntoL 8.0 发布,把「企业级本体决策平台」产品复刻出来了
人工智能·本体论·本体产品
赤龙ERP30 分钟前
ERP周报:连接器开闸、国产GPU进场与智能工厂253家
数据库·人工智能·大模型·rpa·erp·企业数字化·builderpattern
Wang's Blog35 分钟前
Java 项目实战:交友项目 - 联系人列表
java·开发语言·交友
AI编程教父36 分钟前
企业AI转型做自动化,哪些靠规则、哪些交给AI?红烁AI企业AI培训用两类任务对比
人工智能