Spring AI 2.0 高阶实战续篇|会话记忆+RAG知识库+AI智能工具调用,企业级生产落地完整源码

一、前言

在上一篇《Spring AI 2.0 最新实战来了,Java AI 正式起飞!》中,我们完成了 Spring AI 2.0 基础环境搭建、同步问答、流式响应、角色提示词、结构化输出等基础功能落地。

但在真实企业级 AI 项目开发中,基础问答能力仅能满足 Demo 演示需求,无法适配线上复杂业务场景。企业智能化落地,必须攻克三大核心难题:

1、对话无上下文记忆,多轮会话断层,用户体验极差;

2、大模型存在天然幻觉,无法加载企业私有业务文档,回答数据滞后、准确率低;

3、仅支持被动文本问答,无法联动业务接口、查询数据库,不具备真正智能体能力。

本文作为Spring AI 2.0 独家高阶续篇 ,聚焦生产刚需能力,手把手实现 持久化多轮会话记忆、RAG 私有知识库检索、AI 自动化工具调用 三大核心功能,所有代码可直接复制上线,补齐 Java 企业级 AI 落地最后一块短板。

二、Spring AI 2.0 高阶核心架构解析

Spring AI 2.0 相较于 1.x 版本,最大的升级并非语法优化,而是企业级标准化架构重构,新增三大核心高阶能力,也是本文实战的核心底层支撑:

2.1 Advisors 链路增强机制

框架内置标准化切面链路,无需手动 AOP 开发,可快速植入会话记忆、权限拦截、日志监控、检索增强等通用能力,是企业 AI 标准化开发的核心基石。

2.2 原生 RAG 检索架构

内置向量检索、文档解析、相似度匹配能力,屏蔽底层向量数据库适配细节,开箱即用,彻底解决大模型幻觉问题。

2.3 全新标准化 Tool 工具体系

废弃 1.x 老旧繁琐的 FunctionCallback 回调模式,采用 @Tool 注解极简开发,支持 AI 自主识别、自主调用业务工具,真正实现智能体自动化能力。

三、实战一:多轮会话记忆功能落地

Spring AI 2.0 默认对话为无状态模式,每次请求都会重置上下文。通过 MessageChatMemoryAdvisor 可快速实现多轮会话记忆,支持连续对话场景。

3.1 基础内存版会话配置(测试环境)

内存模式适用于本地测试、小型演示项目,零配置快速实现记忆对话:

|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| java import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.chat.memory.InMemoryChatMemory; import org.springframework.ai.chat.memory.ChatMemory; import org.springframework.ai.chat.advisors.MessageChatMemoryAdvisor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * AI 会话记忆配置类 * 适配 Spring AI 2.0 最新链路机制 */ @Configuration public class AiMemoryConfig { /** * 初始化内存级会话存储 * 特点:重启失效、无需依赖中间件,适合测试场景 */ @Bean public ChatMemory chatMemory() { return new InMemoryChatMemory(); } /** * 构建带记忆能力的 ChatClient * 自动植入上下文记忆链路,全局生效 */ @Bean public ChatClient memoryChatClient(ChatClient.Builder builder, ChatMemory chatMemory) { return builder .defaultAdvisors(MessageChatMemoryAdvisor.builder(chatMemory).build()) .build(); } } |

3.2 记忆对话业务接口

|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| java import org.springframework.ai.chat.client.ChatClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * 带上下文记忆的AI对话接口 */ @RestController public class AiMemoryController { private final ChatClient memoryChatClient; public AiMemoryController(ChatClient memoryChatClient) { this.memoryChatClient = memoryChatClient; } @GetMapping("/ai/memory/chat") public String memoryChat(@RequestParam String message) { // 自动携带历史会话上下文,实现多轮连续对话 return memoryChatClient.prompt(message) .call() .content(); } } |

3.3 生产环境优化说明

内存模式存在明显缺陷:服务重启后会话数据全部丢失,无法适配集群、高并发场景。线上生产环境建议替换为 Redis 持久化会话,实现会话数据永久存储、集群共享。

四、实战二:RAG 私有知识库落地(解决模型幻觉)

大模型公共训练数据存在滞后性、通用性强、无企业私有数据,直接使用极易出现答案编造、信息错误等幻觉问题。RAG 检索增强生成,是企业 AI 落地的必备核心方案

本文采用 Spring AI 2.0 原生 PGVector 向量数据库方案,适配企业私有化部署场景。

4.1 引入向量数据库依赖

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| xml <!-- Spring AI 2.0 PGVector向量数据库依赖 --> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-starter-vector-store-pgvector</artifactId> <version>2.0.0</version> </dependency> |

4.2 RAG 核心配置类

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| java import org.springframework.ai.embedding.EmbeddingModel; import org.springframework.ai.vectorstore.PgVectorStore; import org.springframework.ai.vectorstore.VectorStore; import org.springframework.ai.chat.advisors.RetrieveAdvisor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.core.JdbcTemplate; /** * RAG私有知识库配置 * 实现文档向量存储、相似度检索、检索增强问答 */ @Configuration public class AiRagConfig { /** * 初始化PG向量数据库存储 * 自动完成文档向量化、向量存储、相似度匹配 */ @Bean public VectorStore vectorStore(JdbcTemplate jdbcTemplate, EmbeddingModel embeddingModel) { return new PgVectorStore(jdbcTemplate, embeddingModel); } /** * 构建RAG检索增强ChatClient * 自动关联私有知识库数据,优先基于私有文档回答 */ @Bean public ChatClient ragChatClient(ChatClient.Builder builder, VectorStore vectorStore) { return builder.defaultAdvisors(RetrieveAdvisor.builder(vectorStore).build()).build(); } } |

4.3 知识库问答接口

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| java import org.springframework.ai.chat.client.ChatClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * 私有知识库问答接口 * 杜绝模型幻觉,答案完全基于私有业务文档 */ @RestController public class AiRagController { private final ChatClient ragChatClient; public AiRagController(ChatClient ragChatClient) { this.ragChatClient = ragChatClient; } @GetMapping("/ai/rag/chat") public String ragChat(@RequestParam String question) { // 强制AI仅基于检索到的私有文档作答,禁止编造信息 String systemPrompt = "你是企业私有知识库智能助手,严格依据检索到的业务文档内容回答用户问题。若无相关文档信息,如实告知用户,严禁编造、推测答案。"; return ragChatClient.prompt() .system(systemPrompt) .user(question) .call() .content(); } } |

4.4 业务适用场景

该方案可广泛应用于企业智能客服、内部文档检索、项目手册答疑、业务规则查询、产品知识库问答等场景,是目前企业 AI 落地最稳定、成本最低的方案。

五、实战三:AI 工具调用智能体落地

Spring AI 2.0 彻底重构工具调用体系,废弃 1.x 版本繁琐的函数回调开发模式,基于 @Tool 注解实现零门槛工具开发。AI 可自主识别用户意图、自动调用 Java 业务方法,实现真正的智能体能力。

5.1 自定义业务工具类

|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| java import org.springframework.ai.tool.annotation.Tool; import org.springframework.stereotype.Component; import java.time.LocalDateTime; import java.util.HashMap; import java.util.Map; /** * AI自定义业务工具 * 所有被@Tool注解的方法,均可被大模型自主调用 */ @Component public class AiBusinessTool { /** * 服务器时间查询工具 * @return 当前服务器时间 */ @Tool(description = "获取服务器当前系统时间,用于处理时间相关问答、业务时间校验需求") public String getCurrentServerTime() { return LocalDateTime.now().toString(); } /** * 用户信息查询工具 * @param userId 用户ID * @return 用户基础信息 */ @Tool(description = "根据用户ID查询用户基础信息,参数为用户唯一ID") public Map<String, String> getUserInfo(Long userId) { Map<String, String> userMap = new HashMap<>(); userMap.put("userId", String.valueOf(userId)); userMap.put("userName", "企业注册用户"); userMap.put("status", "账号正常,可正常使用业务功能"); return userMap; } } |

5.2 工具调用统一接口

|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| java import org.springframework.ai.chat.client.ChatClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * AI工具调用接口 * 模型自主判断用户意图,自动调用对应业务工具 */ @RestController public class AiToolController { private final ChatClient chatClient; public AiToolController(ChatClient.Builder builder) { this.chatClient = builder.build(); } @GetMapping("/ai/tool/chat") public String toolChat(@RequestParam String message) { // 绑定自定义工具,AI自动识别并调用对应方法 return chatClient.prompt(message) .tools("aiBusinessTool") .call() .content(); } } |

5.3 功能实测效果

用户输入「当前服务器时间是多少」「查询ID为1001的用户信息」,AI 会自动解析用户意图、匹配对应工具、执行 Java 业务方法、获取数据并整理答案返回,完全脱离人工干预,具备基础智能体能力。

六、2026 Spring AI 2.0 生产级避坑指南

基于官方最新稳定版本,整理企业开发高频踩坑点,适配线上生产规范:

  • 版本强制升级:Spring AI 2.0 正式版强制依赖 Java21、SpringBoot 4.x,低版本框架无法兼容,升级项目需提前适配环境;
  • 模型适配调整:官方已废弃智谱 AI 原生适配,国内模型统一采用 OpenAI 兼容接口对接,无需自定义适配逻辑;
  • 工具写法统一 :彻底废弃 1.x 版本 FunctionCallback 回调写法,企业开发统一使用@Tool 注解,代码更简洁、兼容性更强;
  • 协议标准化:全模型遵循 MCP 模型上下文协议,切换国内外大模型仅需修改配置,无需改动业务代码;
  • 会话持久化:生产环境禁止使用内存会话,必须采用 Redis 持久化方案,保障集群部署、服务重启后会话不丢失。

七、总结

如果说 Spring AI 2.0 基础实战只能满足 Demo 演示,那么多轮会话记忆 + RAG 私有知识库 + AI 工具调用,构成了 Java 企业级 AI 落地的完整闭环。

这套高阶方案彻底解决了大模型幻觉、对话无上下文、无法联动业务系统三大核心痛点,让传统 SpringBoot 项目无需切换 Python 技术栈,即可快速落地标准化、工业化、可运维的 AI 智能业务。

在企业智能化全面落地的当下,SpringBoot + Spring AI 2.0 已成为 Java 后端进阶、企业技术改造的核心刚需技术栈。

八、源码获取

本文全套 可上线源码、Redis持久化会话配置、RAG文档导入工具、离线Ollama部署教程 已整理完成,需要的小伙伴可私信获取,助力快速落地企业级 AI 项目。

标签:Spring AI 2.0 Java AI实战 RAG知识库 AI智能体 SpringBoot 企业级AI开发 后端进阶