Spring AI 1.0 正式发布!核心内容和智能体详解
在经历了八个里程碑式的版本之后(M1~M8),Spring AI 1.0 正式版本,终于在 2025 年 5 月 20 日正式发布了,这是另一个新高度的里程碑式的版本,标志着 Spring 生态系统正式全面拥抱人工智能技术,并且意味着 Spring AI 将会给企业带来稳定 API 支持。
1.核心特性
Spring AI 1.0 的核心是 ChatClient 接口,这是一个可移植且易于使用的 API,是与 AI 模型交互的主要接口。
它支持调用 20 多种 AI 模型,从 Anthropic 到 ZhiPu AI,并支持多模态输入和输出(当底层模型支持时)以及结构化响应(通常以 JSON 格式,便于应用程序处理输出)。
1.1 单模型ChatClient使用
在项目中只有一个模型时,创建全局的 ChatClient:
@RestController
class MyController {
private final ChatClient chatClient;
public MyController(ChatClient.Builder chatClientBuilder) {
this.chatClient = chatClientBuilder.build();
}
@GetMapping("/ai")
String generation(String userInput) {
return this.chatClient.prompt()
.user(userInput)
.call()
.content();
}
}
1.2 多模型ChatClient使用
在项目中有多个模型时,为这一个模型创建全局的 ChatClient:
// Create ChatClient instances programmatically
ChatModel myChatModel = ... // already autoconfigured by Spring Boot
ChatClient chatClient = ChatClient.create(myChatModel);
// Or use the builder for more control
ChatClient.Builder builder = ChatClient.builder(myChatModel);
ChatClient customChatClient = builder
.defaultSystemPrompt("You are a helpful assistant.")
.build();
1.3 不同模型类型的ChatClients
当项目中有多个模型时,为每个模型定义单独的 ChatClient:
import org.springframework.ai.chat.ChatClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ChatClientConfig {
@Bean
public ChatClient openAiChatClient(OpenAiChatModel chatModel) {
return ChatClient.create(chatModel);
}
@Bean
public ChatClient anthropicChatClient(AnthropicChatModel chatModel) {
return ChatClient.create(chatModel);
}
}
然后,您可以使用 @Qualifier 指定大模型对应的 ChatClient:
@Configuration
public class ChatClientExample {
@Bean
CommandLineRunner cli(
@Qualifier("openAiChatClient") ChatClient openAiChatClient,
@Qualifier("anthropicChatClient") ChatClient anthropicChatClient) {
return args -> {
var scanner = new Scanner(System.in);
ChatClient chat;
// Model selection
System.out.println("\nSelect your AI model:");
System.out.println("1. OpenAI");
System.out.println("2. Anthropic");
System.out.print("Enter your choice (1 or 2): ");
String choice = scanner.nextLine().trim();
if (choice.equals("1")) {
chat = openAiChatClient;
System.out.println("Using OpenAI model");
} else {
chat = anthropicChatClient;
System.out.println("Using Anthropic model");
}
// Use the selected chat client
System.out.print("\nEnter your question: ");
String input = scanner.nextLine();
String response = chat.prompt(input).call().content();
System.out.println("ASSISTANT: " + response);
scanner.close();
};
}
}
2.主要功能亮点
- 检索增强生成(RAG):Spring AI 提供了便携式向量存储抽象,支持 20 种不同的向量数据库,从 Azure Cosmos DB 到 Weaviate,像常见的 Cassandra、PostgreSQL/PGVector、MongoDB Atlas、Milvus、Pinecone 和 Redis 等向量数据库存储都是支持的。还包括一个轻量级、可配置的 ETL 框架,用于将数据导入向量存储。
- 对话记忆:通过 ChatMemory 接口管理消息的存储和检索,支持 JDBC、Cassandra 和 Neo4j 等持久化存储。
- 工具调用:通过 @Tool 注解可以轻松定义工具,让 AI 模型能够获取外部信息或执行实际动作。
- 评估与测试:提供 Evaluator 接口和内置的 RelevancyEvaluator、FactCheckingEvaluator,帮助开发者评估 AI 生成内容的准确性和相关性。
- 可观测性:与 Micrometer 集成,提供模型延迟、令牌使用情况等关键指标的详细遥测数据。
3.模型上下文协议(MCP)支持
Spring AI 1.0 全面支持 Model Context Protocol (MCP),这是一个标准化协议,使 AI 模型能够与外部工具、提示和资源进行交互。Spring AI 提供了客户端和服务器端的 MCP支持,简化了 MCP 工具的使用和创建。
最简单的 MCP 自定义服务器端实现:
@Service
public class WeatherService {
@Tool(description = "Get weather information by city name")
public String getWeather(String cityName) {
// 伪代码
return "The weather in " + cityName + " is 21°C and sunny.";
}
}
@SpringBootApplication
public class McpServerApplication {
private static final Logger logger = LoggerFactory.getLogger(McpServerApplication.class);
public static void main(String[] args) {
SpringApplication.run(McpServerApplication.class, args);
}
@Bean
public ToolCallbackProvider weatherTools(WeatherService weatherService) {
return MethodToolCallbackProvider.builder().toolObjects(weatherService).build();
}
}
最简单的 MCP 客户端核心代码实现:
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ClientController {
@Autowired
private ChatClient chatClient;
@RequestMapping("/chat")
public String chat(@RequestParam(value = "msg",defaultValue = "今天天气如何?") String msg) {
String response = chatClient.prompt()
.user(msg)
.call()
.content();
System.out.println("响应结果: " + response);
return response;
}
}
4.AI Agent(智能体)支持
AI Agent 的核心是“利用 AI 模型与其环境交互,以解决用户定义的任务”。有效的 AI Agent 将规划、记忆和作相结合,以完成用户分配的任务。
Spring AI 1.0 支持两种主要类型的 Agent:
- 工作流驱动代理:通过预定义路径编排 LLM 和工具,一种更可控的 Agents 实现方法,其中 LLM 和工具通过预定义的路径进行编排。这些工作流是规范性的,可指导 AI 完成既定的作序列以实现可预测的结果。
- 自主驱动代理:允许 LLM 自主规划和执行处理步骤。这种方式代理将自己决定要调用的路径,决定使用哪些工具以及以什么顺序使用。
虽然完全自主代理的灵活性很有吸引力,但工作流为定义明确的任务提供了更好的可预测性和一致性。具体使用哪种类型,取决于您的具体要求和风险承受能力。
让我们看看 Spring AI 如何通过五种基本模式来实现这些概念,每种模式都服务于特定的用例:
4.1 Chain 工作流模式
该模式将复杂任务分解为一系列步骤,其中每个 LLM 调用都会处理前一个 LLM 调用的输出。
Chain Workflow 模式体现了将复杂任务分解为更简单、更易于管理的步骤的原则。

使用场景
- 具有明确顺序步骤的任务。
- 当您想用延迟换取更高的准确性时。
- 当每个步骤都基于上一步的输出时。
以下是 Spring AI 实现中的一个实际示例:
public class ChainWorkflow {
private final ChatClient chatClient;
private final String[] systemPrompts;
public String chain(String userInput) {
String response = userInput;
for (String prompt : systemPrompts) {
String input = String.format("{%s}\n {%s}", prompt, response);
response = chatClient.prompt(input).call().content();
}
return response;
}
}
此实现演示了几个关键原则:
- 每个步骤都有重点。
- 一个步骤的输出成为下一个步骤的输入。
- 该链易于扩展和维护。
4.2 并行化工作流
LLM 可以同时处理任务,并以编程方式聚合其输出。

使用场景
- 处理大量相似但独立的项目。
- 需要多个独立视角的任务。
- 当处理时间至关重要且任务可并行化时。
简单代码实现:
List<String> parallelResponse = new ParallelizationWorkflow(chatClient)
.parallel(
"Analyze how market changes will impact this stakeholder group.",
List.of(
"Customers: ...",
"Employees: ...",
"Investors: ...",
"Suppliers: ..."
),
4
);
4.3 路由工作流
路由模式实现了智能任务分配,从而支持对不同类型的输入进行专门处理。

使用场景
- 具有不同输入类别的复杂任务。
- 当不同的输入需要专门处理时。
- 何时可以准确处理分类。
简单代码实现:
@Autowired
private ChatClient chatClient;
RoutingWorkflow workflow = new RoutingWorkflow(chatClient);
Map<String, String> routes = Map.of(
"billing", "You are a billing specialist. Help resolve billing issues...",
"technical", "You are a technical support engineer. Help solve technical problems...",
"general", "You are a customer service representative. Help with general inquiries..."
);
String input = "My account was charged twice last week";
String response = workflow.route(input, routes);
4.4 编排器

使用场景
- 无法预先预测子任务的复杂任务。
- 需要不同方法或观点的任务。
- 需要适应性问题解决的情况。
简单实现代码:
public class OrchestratorWorkersWorkflow {
public WorkerResponse process(String taskDescription) {
// 1. Orchestrator analyzes task and determines subtasks
OrchestratorResponse orchestratorResponse = // ...
// 2. Workers process subtasks in parallel
List<String> workerResponses = // ...
// 3. Results are combined into final response
return new WorkerResponse(/*...*/);
}
}
使用示例:
ChatClient chatClient = // ... initialize chat client
OrchestratorWorkersWorkflow workflow = new OrchestratorWorkersWorkflow(chatClient);
WorkerResponse response = workflow.process(
"Generate both technical and user-friendly documentation for a REST API endpoint"
);
System.out.println("Analysis: " + response.analysis());
System.out.println("Worker Outputs: " + response.workerResponses());
4.5 评估器-优化器

使用场景
- 存在明确的评估标准。
- 迭代优化提供可衡量的价值。
- 任务受益于多轮批评。
public class EvaluatorOptimizerWorkflow {
public RefinedResponse loop(String task) {
Generation generation = generate(task, context);
EvaluationResponse evaluation = evaluate(generation.response(), task);
return new RefinedResponse(finalSolution, chainOfThought);
}
}
使用示例:
ChatClient chatClient = // ... initialize chat client
EvaluatorOptimizerWorkflow workflow = new EvaluatorOptimizerWorkflow(chatClient);
RefinedResponse response = workflow.loop(
"Create a Java class implementing a thread-safe counter"
);
System.out.println("Final Solution: " + response.solution());
System.out.println("Evolution: " + response.chainOfThought());
5.开始使用SpringAI
开发者可以通过 Maven 中央仓库获取 Spring AI 1.0 的所有组件。使用提供的 bom 导入依赖:
<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>
也可以在 Spring Initializr 网站上创建 1.0 GA 应用程序,并参考参考文档中的"Getting Started"部分。
小结
Spring AI 1.0 的发布标志着企业级 Java 应用程序开发进入了一个新时代,使开发者能够轻松地将最先进的 AI 能力集成到他们的 Spring 应用程序中。
本文已收录到我的技术小站 www.javacn.site,其中包含的内容有:Spring AI、LangChain4j、Dify、AI Agent、MCP、Function Call、RAG、向量数据库、Prompt、多模态、向量数据库、嵌入模型等内容。
Spring AI 1.0 正式发布!核心内容和智能体详解的更多相关文章
- Spring Boot 2.0正式发布,新特性解读
作者|翟永超 Spring Boot 2.0 来啦,有哪些新特性?升级吗? 写在前面 北京时间 3 月 1 日,经过漫长的等待之后,Spring Boot 2.0 正式发布.作为 Spring 生态中 ...
- 重磅:Spring Boot 2.0 正式发布!
Spring Boot 2.0 正式发布! 2018/03/01最新消息,传得沸沸扬扬的Spring Boot 2.0 正式发布了. 小编去看了下Spring Boot的官网,正式版本已经释放出来了! ...
- 【转】Zabbix 3.0 从入门到精通(zabbix使用详解)
[转]Zabbix 3.0 从入门到精通(zabbix使用详解) 第1章 zabbix监控 1.1 为什么要监控 在需要的时刻,提前提醒我们服务器出问题了 当出问题之后,可以找到问题的根源 网站/ ...
- 整合Spring时Service层为什么不做全局包扫描详解
合Spring时Service层为什么不做全局包扫描详解 一.Spring和SpringMVC的父子容器关系 1.讲问题之前要先明白一个关系 一般来说,我们在整合Spring和SpringMVC这两个 ...
- spring盒springMVC整合父子容器问题:整合Spring时Service层为什么不做全局包扫描详解
整合Spring时Service层为什么不做全局包扫描详解 一.Spring和SpringMVC的父子容器关系 1.讲问题之前要先明白一个关系 一般来说,我们在整合Spring和SpringMVC这两 ...
- CSS进阶内容—浮动和定位详解
CSS进阶内容-浮动和定位详解 我们在学习了CSS的基本知识和盒子之后,就该了解一下网页的整体构成了 当然如果没有学习之前的知识,可以到我的主页中查看之前的文章:秋落雨微凉 - 博客园 CSS的三种布 ...
- Java6.0中Comparable接口与Comparator接口详解
Java6.0中Comparable接口与Comparator接口详解 说到现在,读者应该对Comparable接口有了大概的了解,但是为什么又要有一个Comparator接口呢?难道Java的开发者 ...
- CentOS7+CDH5.14.0安装全流程记录,图文详解全程实测-总目录
CentOS7+CDH5.14.0安装全流程记录,图文详解全程实测-总目录: 0.Windows 10本机下载Xshell,以方便往Linux主机上上传大文件 1.CentOS7+CDH5.14.0安 ...
- 业余草分享 Spring Boot 2.0 正式发布的新特性
就在昨天Spring Boot2.0.0.RELEASE正式发布,今天早上在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误, ...
- Spring Boot 最核心的 3 个注解详解
最近面试一些 Java 开发者,他们其中有些在公司实际用过 Spring Boot, 有些是自己兴趣爱好在业余自己学习过.然而,当我问他们 Spring Boot 最核心的 3 个注解是什么,令我失望 ...
随机推荐
- docker - [03] docker原理
题记 一.docker是怎么工作的 docker是一个CS(Client - Server)结构的系统,docker的守护进程运行在主机上,通过Socket从客户端访问. docker Server接 ...
- Flume - [05] Hbase sink
一.概述 此接收器将数据写入Hbase.Hbase配置是从类路径中遇到的第一个Hbase-site.xml获取的.由配置指定的实现 HbaseEventSerializer 的类用于将事件转换为 ...
- Maxmspjitter实现实时抓取摄像头画面并制成序列图 (定时抓拍)
实时监控.拍照.录像外设的影像画面在B站上我已有所总结,见下面的网址. https://www.bilibili.com/read/cv13149329?spm_id_from=333.999.0.0 ...
- Twain Capabilities属性
Asynchronous Device Events 异步设备事件 CAP_DEVICEEVENT MSG_SET选择应用程序希望Twain源报告的事件; MSG_RESET返回Twain源的首选设置 ...
- 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
大家好,我是小康.今天我们来聊一个藏在C++标准库中的"定时炸弹",它看起来人畜无害,但却坑了无数C++程序员. 前言:当你以为自己用的是vector,结果却不是 嘿,各位码农兄弟 ...
- SpringBoot+微信支付-JSAPI
引入微信支付SDK Maven: com.github.wechatpay-apiv3:wechatpay-java-core:0.2.12 Maven: com.github.wechatpay-a ...
- bug|Git Hooks pre-commit|git 提交代码报错|error: 'describe' 'it' 'expect' is not defined (no-undef)|pre-commit hook failed (add --no-verify to bypass)|
前言 今天学习 jest 的 vue-test-utils 的配置及使用. 报错原因为 jest 全局变量 git 提交代码报错,使用除了参考链接里的解决方案,正好复习一下之前学习的 Git Hook ...
- Fetch 别名查找
if (PlanClass.Attributes.Contains("new_excelcolor_avg")) ...
- 【SpringCloud】SpringCloud Alibaba Sentinel实现熔断与限流
SpringCloud Alibaba Sentinel实现熔断与限流 限流与降级 限流 blockHandler 降级 fallback 降级需要运行时出现异常才会触发,而限流一旦触发,你连运行的机 ...
- 【SpringCloud】SpringCloud Stream消息驱动
SpringCloud Stream消息驱动 消息驱动概述 是什么 什么是SpringCloudStream 官方定义Spring Cloud Stream是一个构建消息驱动微服务的框架. 应用程序通 ...