Spring Boot-使用自定义注释获取所有方法
我有一个自定义注释:Spring Boot-使用自定义注释获取所有方法,spring,spring-boot,reflection,annotations,kotlin,Spring,Spring Boot,Reflection,Annotations,Kotlin,我有一个自定义注释: @Target(AnnotationTarget.FUNCTION) @Retention(AnnotationRetention.RUNTIME) @MustBeDocumented annotation class Listener 这样使用: @Service class MyService { @Listener fun onNewThing(thing: Thing) { ... } } 在另一个服务中,每次发生某件事时,我都
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
annotation class Listener
这样使用:
@Service
class MyService {
@Listener
fun onNewThing(thing: Thing) {
...
}
}
在另一个服务中,每次发生某件事时,我都要调用带有@Listener
注释和Thing
类型参数的每个函数。
如何在不遍历上下文中的所有bean并检查所有方法的情况下完成此操作?您可以使用org.reflections of java:
Set allMethods=new Reflections().getMethodsAnnotatedWith(yourAnnotation.class)代码>
您可以使用以下选项:
Set<Method> methodsAnnotatedWith = new Reflections("com.example.spring.aop.api", new MethodAnnotationsScanner()).getMethodsAnnotatedWith(BusinessFunction.class);
Set methodsAnnotatedWith=new Reflections(“com.example.spring.aop.api”,new MethodAnnotationsScanner()).getMethodsAnnotatedWith(BusinessFunction.class);
调用Thing的方法时,是否通知@Listener annotation的方法?你是说?我有另一个服务从外部服务接收不同类型的数据。每次我得到一个新数据时,我都会解析它,然后我想用@Listener
注释调用所有函数,使用我刚刚接收的类型的参数,使用spring AOP将更容易控制
Set<Method> methodsAnnotatedWith = new Reflections("com.example.spring.aop.api", new MethodAnnotationsScanner()).getMethodsAnnotatedWith(BusinessFunction.class);