当前位置: 首页 > 知识库问答 >
问题:

如何从过程连接点(ProceedingJoinPoint)获取方法的注释值?

田硕
2023-03-14

我有以下注释。

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {

}
public class SomeAspect{

 @Around("execution(public * *(..)) && @annotation(com.mycompany.MyAnnotation)")
    public Object procede(ProceedingJoinPoint call) throws Throwable {

  //Some logic

}

}
public class SomeOther{

@MyAnnotation("ABC") 
public String someMethod(String name){


}


}

在上面的类中,我在@MyAnnoting中传递了“ABC”。现在我如何在某些spect.java类的程序方法中访问“ABC”值?

谢谢

共有3个答案

黄宏旷
2023-03-14

这同样有效——您可以使用类上的反射来获取注释信息。

Annotation anno = MyClass.class.getAnnotation(MyAnnotation.class);

或者

Annotation anno = MyClass.class.getDeclaredMethod("somethod").getAnnotation(MyAnnotation.class);

只有当您的注释在运行时可用(您已正确声明)时,这才有效。

@Retention(RetentionPolicy.RUNTIME)
劳通
2023-03-14

实际上,我认为我们可以通过另一种方式获取,而不仅仅是从程序连接点获取,这肯定需要我们使用反射

直接使用注释进行如下尝试:添加com。我的公司。MyAnnotation yourAnnotation(MyAnnotation yourAnnotation)在您的建议参数中和周围的注释(yourAnnotation)。

@Around("execution(public * *(..)) && @annotation(yourAnnotation)")
public Object procede(ProceedingJoinPoint pjp, com.mycompany.MyAnnotation yourAnnotation) {
    ...
    yourAnnotation.value(); // get your annotation value directly;
    ...
}

com.mycompany.我的注释在建议参数只是工作

@Around("execution(public * *(..)) && @annotation(com.mycompany.MyAnnotation)")

您的注释可以是有效的变量名,因为params中的MyAnnotation已经指出它应该是哪个注释。这里,您的注释仅用于检索注释实例。

如果您想传递更多参数,可以尝试args()

欲知更多详情,请查看其官方文件。对于注释值,您可以只搜索可审核的。

施刚毅
2023-03-14

您可以从过程连接点(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();

编辑

如何获得价值,如果我有@我的注释(ABC)在类级别?

Class也是AnttatedElement,因此您可以像从method中获取它一样。例如。可以使用

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

因为您使用的是spring,所以可能还需要使用spring的注释utils。findAnnotation(…) 。它像spring一样搜索注释。E、 g.还研究了超类和接口方法等。

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

编辑

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

 类似资料:
  • 问题内容: 我有以下注释。 MyAnnotation.java SomeAspect.java SomeOther.java 在上面的课中, @MyAnnotation中* 传递了“ ABC ” 。现在如何在 SomeAspect.java 类的 处理 方法中访问“ ABC ”值? *** 谢谢! 问题答案: 您可以从ProceedingJoinPoint获取签名,如果方法调用,只需将其转换为Me

  • 我有一个方面,处理所有有自定义注释的方法。 注释有一个枚举参数,我必须在方面中获得值: 有没有办法从ProcedingJoinPoint获得实现方法的签名?

  • 问题内容: 我想用Spring / AOP和注解实现声明式安全性。如您在下一个代码示例中看到的,我使用参数“ allowedRoles”定义了“受限注释”,用于定义允许谁执行建议的方法。 现在的问题是,在我的建议中,我无法访问已定义的注释: 上面的方法总是返回null(根本没有找到注释)。有一个简单的解决方案吗? 我阅读了有关使用AspectJ代理的内容,但我不希望使用此代理。 问题答案: 我认为

  • 我想从方法中检索注释(自定义编写的注释)。通常我可以通过访问 但是如果bean由CDI容器管理(我使用的是OpenWebBeans),那么类在运行时会得到增强。然后我必须使用超类来请求注释。目前,我试图通过在类名中查找“$$”来检测是否管理该类。但对我来说,这似乎是一个非常肮脏的解决方案。 有什么好方法可以从CDI管理的bean中检索注释吗? 详细地说,我的代码是这样的:我创建了一个注释“Cool

  • 在我的方面方法中,我需要获取name(自定义注释的参数)的值 由用户调用的方法:

  • 作为我的一个要求,我创建了一个包含多个方法的inteface,每个方法都用我自己的java自定义注释进行了注释。我的实现类有一个spring aop方面,但我无法获得aop方面的自定义注释。在进行调试后,我明白了我的自定义注释是接口的一部分,而不是在实现类中。