我想通过反射访问Method.问题是Method在界面中注释:
public interface MyRepository extends CrudRepository {
@CustomAnnotation
MyClass findByName(String name);
}
如您所见,我使用Spring提供了一个实现此Repository接口的类.
我想创建一个方法,它将获得一个Repository并调用所有使用@CustomAnnotation注释的方法.
public void do(Repository, ?> repository){
Method[] methods=repository.getClass().getMethodThatAreAnnotatedInInterfaceWith(CustomAnnotation.class);
....
}
因为接口的实现不会出现接口的注释,所以我不知道如何查询这些方法.
解决方法:
Find a single Annotation of annotationType on the supplied Method, traversing its super methods (i.e., from superclasses and interfaces) if the annotation is not directly present on the given method itself.
迭代getClass().get [Declared] Methods()的方法,并为每个方法检查是否使用上述实用程序使用注释进行注释.
标签:java,reflection,spring
来源: https://codeday.me/bug/20190528/1167896.html