use*_*582 5 java reflection spring
我想通过反射访问方法。问题是该方法在接口中注释:
\n\npublic interface MyRepository extends CrudRepository<MyClass, Long> {\n\n @CustomAnnotation\n MyClass findByName(String name);\n}\nRun Code Online (Sandbox Code Playgroud)\n\n正如您所看到的,我使用 Spring,它提供了一个将实现此 Repository 接口的类。\n我想创建一个方法,它将获取一个 Repository 并调用所有用@CustomAnnotation.
public void do(Repository<?, ?> repository){\n Method[] methods=repository.getClass().getMethodThatAreAnnotatedInInterfaceWith(CustomAnnotation.class);\n ....\n}\nRun Code Online (Sandbox Code Playgroud)\n\n因为接口的实现不会有接口的注释,所以我不知道如何查询这些方法。
\n由于您使用的是 Spring,因此请使用AnnotationUtils#findAnnotation(Method method, Class<A> annotationType):
如果注释没有直接出现在给定方法本身上,则在提供的方法上查找注释类型的单个注释,遍历其超级方法(即,从超类和接口)。
迭代getClass().get[Declared]Methods()它们的方法,并使用上面的实用程序检查每个方法是否用您的注释进行了注释。
| 归档时间: |
|
| 查看次数: |
4046 次 |
| 最近记录: |