How to get a method's annotation value from a

2019-01-21 07:55发布

I have below annotation.

MyAnnotation.java

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

}

SomeAspect.java

public class SomeAspect{

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

  //Some logic

}

}

SomeOther.java

public class SomeOther{

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


}


}

In above class am passing "ABC" with in @MyAnnotation. Now how can i access "ABC" value in procede method of SomeAspect.java class?

Thanks!

3条回答
手持菜刀,她持情操
2楼-- · 2019-01-21 08:24

You can get the Signature from a ProceedingJoinPoint and in case of a method invocation just cast it to a 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);
}

But you should first add an annotation attribute. Your example code doesn't have one, e.g.

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

    String value();
}

Then you can access it

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

EDIT

How to get value if I have @MyAnnotation("ABC") at class level ?

A Class is also an AnnotatedElement, so you can get it the same way as from a Method. E.g. An annotation of the method's declaring class can be obtained using

 Method method = ...;
 Class<?> declaringClass = method.getDeclaringClass();
 MyAnnotation myAnnotation = declaringClass.getAnnotation(MyAnnotation.class)
查看更多
We Are One
3楼-- · 2019-01-21 08:35

Actually I think can get the value in another way round instead of just from ProceedingJoinPoint, which will definitely require us to make use of reflection.

Have a try as follows using annotation directly: add com.mycompany.MyAnnotation yourAnnotation in your advice params and @annotation(yourAnnotation) in @Around.

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

com.mycompany.MyAnnotation in advice params just work as that in

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

yourAnnotation can be valid variable name since MyAnnotation in params already points out which annotation it should be. Here yourAnnotation is used to retrieve the annotation instance only.

If you want to pass more params you can try args().

For more details, do please check its official doc. For Annotation value, you can just search @Auditable.

查看更多
Explosion°爆炸
4楼-- · 2019-01-21 08:37

This works as well - You can fetch annotation information using reflection on the class.

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

Or

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

This works only if your annotation is available at runtime, which you have declared correctly.

@Retention(RetentionPolicy.RUNTIME)
查看更多
登录 后发表回答