具有注释参数的AspectJ切入点 [英] aspectj pointcut with annotation parameters

查看:510
本文介绍了具有注释参数的AspectJ切入点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Aspectj来拦截用@Profile(description="something")

I am using aspectj to intercept methods that are annotated with @Profile(description="something")

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Profile {
    public String description() default "";
}

@Around("com.merc.aop.ctw.aspect.PointcutDefinitions.logAnnotatedMethods(profile)")
public Object profile(ProceedingJoinPoint pjp, Profile profile) throws Throwable {
    ....
}

@Pointcut("@annotation(com.merc.annotations.Profile)")
protected void logAnnotatedMethods(Profile profile) {
}

但是在使用AJC进行编译时出现以下错误消息

But I get the following error msg while compileing using AJC

formal unbound in pointcut 

推荐答案

@Pointcut("@annotation(com.merc.annotations.Profile)")
protected void logAnnotatedMethods(Profile profile) {
}

这是不正确的,@annotation()想要一个参数名称,而不是一个参数类型.

This is not correct, @annotation() wants a parameter name, not a parameter type.

如果您的类是使用调试代码编译的,则pointcut参数必须与方法参数具有相同的名称,否则,您需要依赖于唯一的参数类型,或者使用参数:

If your class is compiled with debug code, the pointcut parameter must have the same name as the method parameter, if not, you need to either rely on the parameter types being unique or explicitly write out your parameter names using the argNames parameter:

@Pointcut(value="@annotation(profile)",argNames="profile")
protected void logAnnotatedMethods(Profile arg) {    }

参考:

  • @Pointcut javadocs

这篇关于具有注释参数的AspectJ切入点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆