如何从ProceedingJoinPoint获取类级别注释

时间:2015-12-14 10:29:01

标签: 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;
    }
}

这是我的注释

@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    String reason();
}

这是班级

@MyAnnotation(reason="yes")
public class SomeClassImpl implements SomeClass {
}

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

1 个答案:

答案 0 :(得分: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();
    }
}
相关问题