如何从 ProceedingJoinPoint 获取类级别注释

PHP*_*ger 5 java aspectj spring-aop

我正在尝试使用@Aspect 实现拦截器。我需要获得类级别注释

这是我的拦截器

@Aspect
public class MyInterceptor {
    @Around("execution(* com.test.example..*(..))")
    public Object intercept(ProceedingJoinPoint pjp) throws Throwable {
        Object result;
        try {
            result = pjp.proceed();
        } catch (Throwable e) {
            throw e;
        }
        return result;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的注释

@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    String reason();
}
Run Code Online (Sandbox Code Playgroud)

这是课程

@MyAnnotation(reason="yes")
public class SomeClassImpl implements SomeClass {
}
Run Code Online (Sandbox Code Playgroud)

在拦截器中,我需要获取注释和分配给原因属性的值。

akk*_*kki 2

拦截器类获取在类级别标记的注释的值

@Aspect
@Component
public class MyInterceptor {
    @Around("@target(annotation)")
    public Object intercept(ProceedingJoinPoint joinPoint, MyAnnotation annotation) throws Throwable {
        System.out.println(" called with '" + annotation.reason() + "'");
        return joinPoint.proceed();
    }
}
Run Code Online (Sandbox Code Playgroud)