小崔打开 IDEA 准备引入 Spring AI 依赖,看到一堆 Starter 瞬间迷茫:"spring-ai-openai、spring-ai-ollama、spring-ai-pgvector……到底要加哪个?"白歌走过去敲了敲屏幕:"先加 BOM,再加你需要的 Starter。BOM 管版本,Starter 管功能。"
核心模块与依赖关系
定义与作用
Spring AI 采用模块化架构,核心抽象与具体实现分离。理解模块依赖关系,才能正确配置 pom.xml、避免版本冲突、按需引入功能。
核心原则:spring-ai-core 定义接口(ChatClient、ChatModel 等),各 Starter 模块提供具体实现。你的代码只依赖 core 的抽象,运行时由 Spring Boot 自动注入对应实现。
核心原理:模块依赖图
图释:所有模块的版本由 Spring AI BOM 统一管理,避免手动指定版本号。核心层(spring-ai-core + autoconfigure)是必选的,各 Starter 按需选择。
各模块职责速查
| 模块 | 一句话职责 |
|---|---|
spring-ai-core | 定义 ChatClient、ChatModel、Prompt、Message、Advisor 等核心接口 |
spring-ai-autoconfigure | Spring Boot 自动配置,读取 YAML 属性并创建 Bean |
spring-ai-openai | OpenAI GPT 系列模型的集成 Starter |
spring-ai-ollama | Ollama 本地模型的集成 Starter |
spring-ai-anthropic | Anthropic Claude 系列模型的集成 Starter |
spring-ai-pgvector | PostgreSQL + PGVector 向量存储集成 |
spring-ai-redis | Redis Stack 向量存储集成 |
spring-ai-milvus | Milvus 向量数据库集成 |
spring-ai-mcp | MCP(Model Context Protocol)客户端/服务端支持 |
版本管理:Spring AI BOM
Spring AI 提供 BOM(Bill of Materials) 统一管理所有模块版本。类似于 Spring Boot 的 spring-boot-dependencies,你用 BOM 后无需为每个 AI 依赖指定 <version>。
完整 pom.xml 示例
小崔为飞翔科技学生管理系统引入 Spring AI 依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/>
</parent>
<groupId>com.feixiang</groupId>
<artifactId>student-ai-service</artifactId>
<version>1.0.0</version>
<properties>
<java.version>17</java.version>
<spring-ai.version>1.0.0</spring-ai.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- Spring AI BOM:统一管理所有 AI 模块版本 -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Spring Boot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring AI 核心(必选,通常由 Starter 传递引入) -->
<!-- Spring AI OpenAI Starter -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
<!-- 本地开发用 Ollama(可选) -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
</dependency>
<!-- RAG 场景用 PGVector(后续章节用到) -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-pgvector-store-spring-boot-starter</artifactId>
</dependency>
<!-- Spring Boot Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
关键点说明:
- BOM 在
<dependencyManagement>中,不是<dependencies>,它只管理版本不实际引入 JAR - Starter 在
<dependencies>中不写<version>,版本由 BOM 控制 - 核心模块
spring-ai-core无需显式引入,Starter 会自动传递依赖
易错场景与面试考点
易错场景一:忘记引入 BOM,手动指定版本导致不兼容
<!-- ❌ 错误:没有 BOM 管理,手动指定版本 -->
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-pgvector-store-spring-boot-starter</artifactId>
<version>0.8.1</version> <!-- 版本不一致! -->
</dependency>
</dependencies>
问题分析
spring-ai-openai 1.0.0 依赖 spring-ai-core 1.0.0,而 spring-ai-pgvector 0.8.1 依赖 spring-ai-core 0.8.1。Maven 的"最近依赖优先"策略可能导致 spring-ai-core 被解析为 0.8.1,运行时抛 NoSuchMethodError。
<!-- ✅ 正确做法:BOM 统一管理,所有 AI 模块不写版本 -->
<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>
易错场景二:Snapshots/Milestones 仓库未配置
<!-- ❌ 错误:使用 Milestone 版本但未配置 Spring Milestones 仓库 -->
<spring-ai.version>1.0.0-M6</spring-ai.version>
问题分析
GA 版本(如 1.0.0)发布到 Maven Central,但 Milestone 版本需要额外配置 Spring Milestones 仓库:
<!-- ✅ 正确:添加 Spring Milestones 仓库 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
面试高频题
Q1:Spring AI BOM 的作用是什么?
BOM(Bill of Materials)统一管理所有 Spring AI 模块的版本号,确保
spring-ai-core、spring-ai-openai、spring-ai-pgvector等模块使用兼容的版本。开发者在<dependencies>中声明 Starter 时无需写<version>,由 BOM 自动管理,避免版本冲突。
Q2:为什么核心模块 spring-ai-core 不需要显式引入?
每个 Starter(如
spring-ai-openai-spring-boot-starter)在其pom.xml中已经依赖了spring-ai-core,Maven 的传递依赖机制会自动引入。你只需要引入需要的 Starter 即可。
本章小结
- Spring AI 采用模块化架构,BOM 管版本,Starter 管功能
spring-ai-core定义接口,各 Starter 提供具体实现- 使用 BOM 后,Starter 依赖无需指定版本号
- 按需引入 Starter:对话用 OpenAI/Ollama,向量存储用 PGVector/Redis,MCP 用
spring-ai-mcp - 避免手动指定版本,避免忘记配置 Milestones 仓库