복붙노트

[SPRING] ProceedingJoinPoint에서 메소드의 주석 값을 얻는 방법?

SPRING

ProceedingJoinPoint에서 메소드의 주석 값을 얻는 방법?

나는 주석 아래에있다.

@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){


}


}

위의 클래스에서는 @MyAnnotation에 "ABC"를 전달합니다. 이제 SomeAspect.java 클래스의 procede 메소드에서 "ABC"값에 어떻게 액세스 할 수 있습니까?

감사!

해결법

  1. ==============================

    1.ProceedingJoinPoint에서 Signature를 가져올 수 있으며 메소드 호출의 경우에는 MethodSignature로만 전달할 수 있습니다.

    ProceedingJoinPoint에서 Signature를 가져올 수 있으며 메소드 호출의 경우에는 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();
    

    편집하다

    클래스 또한 AnnotatedElement이므로 Method와 동일한 방식으로 가져올 수 있습니다. 예 : 메소드 선언 클래스의 주석은 다음을 사용하여 얻을 수 있습니다.

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

    2.실제로 ProceedingJoinPoint 대신에 다른 방식으로 값을 얻을 수 있다고 생각합니다. ProceedingJoinPoint는 리플렉션을 사용해야합니다.

    실제로 ProceedingJoinPoint 대신에 다른 방식으로 값을 얻을 수 있다고 생각합니다. ProceedingJoinPoint는 리플렉션을 사용해야합니다.

    특수 효과를 직접 사용하여 다음과 같이 시도해보십시오. @Around의 advice params 및 @annotation (yourAnnotation)에 com.mycompany.MyAnnotation yourAnnotation을 추가하십시오.

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

    조언 매개 변수의 com.mycompany.MyAnnotation은

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

    params의 MyAnnotation은 어떤 주석이 있어야하는지 이미 지적 했으므로 yourAnnotation은 유효한 변수 이름이 될 수 있습니다. 여기에서 yourAnnotation은 주석 인스턴스 만 검색하는 데 사용됩니다.

    더 많은 매개 변수를 전달하려면 args ()를 사용해보십시오.

    자세한 내용은 공식 문서를 확인하십시오. 주석 값의 경우 @Auditable 만 검색하면됩니다.

  3. ==============================

    3.이것도 작동합니다 - 클래스의 리플렉션을 사용하여 주석 정보를 가져올 수 있습니다.

    이것도 작동합니다 - 클래스의 리플렉션을 사용하여 주석 정보를 가져올 수 있습니다.

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

    또는

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

    이는 올바르게 선언 한 런타임에 주석을 사용할 수있는 경우에만 작동합니다.

    @Retention(RetentionPolicy.RUNTIME)
    
  4. from https://stackoverflow.com/questions/21275819/how-to-get-a-methods-annotation-value-from-a-proceedingjoinpoint by cc-by-sa and MIT license