Spring Boot如何扫描自定义注解并调用对应方法,模仿RestController与PostMapping
实现思路
核心逻辑分为三步:类路径扫描找到标注@PushNotificationHandler的类、实例化类对象后扫描其内部标注@PushNotificationDispatcher的方法并缓存、收到推送时统一调用所有缓存的方法。以下分别给出Spring生态和纯JVM环境的实现方案:
方案1:Spring Boot 环境实现(和Spring Controller逻辑完全对齐)
Spring 已经内置了Bean管理和扫描能力,直接复用ApplicationContext的能力即可,不需要额外引入第三方依赖。
1. 定义处理器注册表,初始化时扫描所有合法的分发方法
@Component class PushHandlerRegistry(applicationContext: ApplicationContext) { // 缓存结构:Bean实例 + 可执行的分发方法 val dispatcherMethods: MutableList<Pair<Any, Method>> = mutableListOf() init { // 直接从Spring上下文拿到所有标注了@PushNotificationHandler的托管Bean val handlerBeans = applicationContext.getBeansWithAnnotation(PushNotificationHandler::class.java) handlerBeans.values.forEach { bean -> bean.javaClass.declaredMethods.forEach { method -> // 过滤出标注了@PushNotificationDispatcher的方法 if (method.isAnnotationPresent(PushNotificationDispatcher::class.java)) { // 做参数合法性校验,可根据自己的需求扩展 val params = method.parameters check(params.size == 1 && params[0].type == PushNotification::class.java) { "方法${method.name}标注了@PushNotificationDispatcher,必须仅接收1个PushNotification类型参数" } // 允许调用非public方法 method.isAccessible = true dispatcherMethods.add(bean to method) } } } } }
2. 定义统一分发服务,收到推送时执行所有注册的方法
@Component class PushDispatchService( private val handlerRegistry: PushHandlerRegistry ) { fun dispatch(pushNotification: PushNotification) { handlerRegistry.dispatcherMethods.forEach { (bean, method) -> try { method.invoke(bean, pushNotification) } catch (e: InvocationTargetException) { // 解包反射调用的原始异常 throw e.targetException } catch (e: Exception) { // 可自定义异常兜底逻辑 throw RuntimeException("推送分发失败", e) } } } }
使用时直接注入PushDispatchService调用dispatch方法即可,所有应用侧定义的Handler都会被自动执行。
方案2:纯Kotlin/JVM 环境实现(无Spring依赖)
需要引入第三方类路径扫描库Reflections完成扫描逻辑:
1. 引入依赖
Maven 依赖:
<dependency> <groupId>org.reflections</groupId> <artifactId>reflections</artifactId> <version>0.10.2</version> </dependency>
Gradle 依赖:
implementation 'org.reflections:reflections:0.10.2'
2. 实现扫描+分发逻辑
class PushDispatcher(private val baseScanPackage: String) { private val dispatcherMethods: MutableList<Pair<Any, Method>> = mutableListOf() init { // 扫描指定包下所有标注了@PushNotificationHandler的类 val reflections = Reflections(baseScanPackage) val handlerClasses = reflections.getTypesAnnotatedWith(PushNotificationHandler::class.java) handlerClasses.forEach { clazz -> // 反射实例化类,要求Handler类提供无参构造函数 val instance = clazz.getDeclaredConstructor().newInstance() clazz.declaredMethods.forEach { method -> if (method.isAnnotationPresent(PushNotificationDispatcher::class.java)) { // 参数校验和Spring场景一致 check(method.parameters.size == 1 && method.parameters[0].type == PushNotification::class.java) { "方法${method.name}参数不符合要求" } method.isAccessible = true dispatcherMethods.add(instance to method) } } } } fun dispatch(pushNotification: PushNotification) { dispatcherMethods.forEach { (instance, method) -> method.invoke(instance, pushNotification) } } }
使用时初始化PushDispatcher传入应用侧的基础包名即可。
扩展能力
如果需要实现类似Spring的参数解析、返回值处理、异步调度等能力,只需要在原有逻辑上扩展:
- 新增参数解析器接口,方法调用前解析参数
- 新增返回值处理器接口,方法执行后处理返回结果
- 调用方法时提交到线程池即可实现异步执行
内容的提问来源于stack exchange,提问作者Karim H
相关产品推荐
相关产品推荐

