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


相关推荐
@兽血沸腾4 小时前
Thread线程和synchronized锁
java·开发语言
Fxkj8885 小时前
传统企业布局新媒体:自行摸索与IP陪跑模式深度对比
人工智能·网络协议·tcp/ip·媒体
Android打工人5 小时前
c++ 、java 融为一体2,c++ 实例化Android虚拟机直接运行java,c,java 一个进程号
android·java·c++
Wang's Blog5 小时前
Vibe Coding一人即团队系列31:基于Claude Code实现登录注册与数据库联动
数据库·人工智能
geneculture5 小时前
从融智学到人机互助:技术成果交易的理论重构与战略前瞻——基于“九五至尊模型”与融智学三大判断的分析
大数据·人工智能·融智学的重要应用·哲学与科学统一性·融智时代(杂志)·序位逻辑的实例化·中国企业知识产权战略专栏
lvts_cs5 小时前
如何判断化工产业规划的定位布局是否合理
大数据·人工智能
断眉的派大星5 小时前
TensorRT 基础与核心流程完整学习笔记
人工智能·深度学习
2601_962056605 小时前
Spring Boot——日志介绍和配置
java·数据库·spring boot
运营小白5 小时前
SEONIB AI 内容自动化流水线深度评测
人工智能·自动化·内容运营·跨境电商·社交媒体·ai skill
Alan CGH5 小时前
一次非寻常的压测优化经历
java·网络·压力测试