java - 如何从 ProceedingJoinPoint 获取类级别注释

标签 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 属性的值。

最佳答案

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

@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();
    }
}

关于java - 如何从 ProceedingJoinPoint 获取类级别注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34264713/

相关文章:

java - BlackBerry 布局管理器问题,虚拟大小

java - 切入点与继承的混淆

java - 在 64 位机器上运行时出现 NoClassDefFoundError

spring - 为什么在切入点表达式中使用两个星号来匹配返回类型?

java - Spring AOP异常处理如果存在catch block 则不执行建议

java - Jersey 多个构造函数@inject

Java 使用正则表达式匹配多个标记

java - Aspectj 切入点表达式不适用于单个类中的一种方法,但适用于另一种方法

java - 使用泛型类型作为切入点的返回类型

java - 在子类中创建打印父类(super class)方法的方法?