使用 Spring Boot
本页适合已有 Spring Boot 项目的读者,示例使用 Java 17 或更高版本。 如果使用 Spring Boot 2,请参考 使用 Spring Boot 2。
安装
准备一个 Spring Boot 3 项目,并添加 simbot starter:
implementation("love.forte.simbot:simbot-core-spring-boot-starter:5.0.0-Alpha2")
implementation 'love.forte.simbot:simbot-core-spring-boot-starter:5.0.0-Alpha2'
<dependency>
<groupId>love.forte.simbot</groupId>
<artifactId>simbot-core-spring-boot-starter</artifactId>
<version>5.0.0-Alpha2</version>
</dependency>
完整的项目准备和依赖说明见 Spring Boot。
使用
启用 simbot
在已有的 Spring Boot 启动类上添加 love.forte.simbot.spring.EnableSimbot。
import love.forte.simbot.spring.EnableSimbot
@EnableSimbot
@SpringBootApplication(proxyBeanMethods = false)
class MyApplication
import love.forte.simbot.spring.EnableSimbot;
@EnableSimbot
@SpringBootApplication
public class MyApplication {
// 保留已有的启动入口。
}
编写事件处理器
在 Spring 扫描范围内创建组件,并使用 @Listener 标记处理函数:
import love.forte.simbot.event.Event
import love.forte.simbot.quantcat.common.annotations.Listener
import org.springframework.stereotype.Component
@Component
class MyEventHandlers {
@Listener
suspend fun onEvent(event: Event) {
println("收到事件: $event")
}
}
import love.forte.simbot.event.Event;
import love.forte.simbot.quantcat.common.annotations.Listener;
import org.springframework.stereotype.Component;
@Component
public class MyEventHandlers {
@Listener
public void onEvent(Event event) {
System.out.println("收到事件: " + event);
}
}
Event 可监听多种事件。需要处理消息时,可以换成具体的消息事件类型,参见 事件监听与处理。
注册拦截器
将 SimbotInterceptorRegistrar 声明为 Spring Bean,可在 Bot 自动加载前注册消息发送等应用拦截器。参见 注册应用拦截器。
安装组件以及组件配置
选择 选择组件库 中的平台组件,按照它的手册添加依赖和 Ktor 客户端引擎。 组件提供平台相关的 Bot 和事件,具体的凭据与权限由目标平台决定。
注册Bot
支持配置文件自动注册的组件可从资源目录下的 simbot-bots/ 加载 Bot 配置,文件名以 .bot.json 结尾。配置字段见所选组件的使用手册。
默认情况下,starter 会扫描配置文件并启动注册的 Bot。扫描路径、自动启动等选项见 注册Bot。
运行或打包
启动 Spring Boot 应用,并在目标平台触发一个已订阅的事件。监听器输出“收到事件”表示事件已进入应用。 如果没有输出,请检查 Bot 是否启动、平台权限和事件订阅配置,再参考 常见问题。
应用启动后的其他配置方式、主动获取 Bot 和打包说明见 Spring Boot。
19 November 2026