栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何从ProceedingJoinPoint获取方法的注释值?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何从ProceedingJoinPoint获取方法的注释值?

您可以从ProceedingJoinPoint获取签名,如果方法调用,只需将其转换为MethodSignature即可。

@Around("execution(public * *(..)) && @annotation(com.mycompany.MyAnnotation)")public Object procede(ProceedingJoinPoint call) throws Throwable {    MethodSignature signature = (MethodSignature) call.getSignature();    Method method = signature.getMethod();    MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);}

但是,您应该首先添加一个注释属性。您的示例代码没有一个,例如

@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface MyAnnotation {    String value();}

然后就可以访问它

MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);String value = myAnnotation.value();

编辑

如果我在班级有@MyAnnotation(“ ABC”),如何获取价值?

A

Class
也是A
AnnotatedElement
,因此您可以从A
获得相同的方式
Method
。例如,可以使用以下方法获取方法声明类的注释:

 Method method = ...; Class<?> declaringClass = method.getDeclaringClass(); MyAnnotation myAnnotation = declaringClass.getAnnotation(MyAnnotation.class)

由于您正在使用spring,因此您可能还需要使用spring的

AnnotationUtils.findAnnotation(..)
。它像spring一样搜索注释。例如,还要查看超类和接口方法等。

 MyAnnotation foundAnnotation = AnnotationUtils.findAnnotation(method, MyAnnotation.class);

编辑

您可能还会

MergedAnnotations
对5.2中引入的spring的功能感兴趣。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/515122.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号