Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring Boot-使用自定义注释获取所有方法_Spring_Spring Boot_Reflection_Annotations_Kotlin - Fatal编程技术网

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);