首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从ProceedingJoinPoint获取类级批注

如何从ProceedingJoinPoint获取类级批注
EN

Stack Overflow用户
提问于 2015-12-14 18:29:01
回答 1查看 1.5K关注 0票数 4

我正在尝试用@Aspect实现一个拦截器。我需要获取类级别的注释

这是我的拦截器

代码语言:javascript
运行
复制
@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;
    }
}

下面是我的注解

代码语言:javascript
运行
复制
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    String reason();
}

这是一个类

代码语言:javascript
运行
复制
@MyAnnotation(reason="yes")
public class SomeClassImpl implements SomeClass {
}

在interceptor中,我需要获取注释和赋值给reason属性的值。

EN

回答 1

Stack Overflow用户

发布于 2015-12-14 18:44:20

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

代码语言:javascript
运行
复制
@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();
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34264713

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档