【问题标题】:aspectj pointcut with annotation parameters带有注释参数的aspectj切入点
【发布时间】:2011-02-07 14:43:26
【问题描述】:

我正在使用 aspectj 来拦截带有 @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 编译时收到以下错误消息

formal unbound in pointcut 

【问题讨论】:

  • 嗨,我的要求和你的一样。我怀疑什么是“com.merc.aop.ctw.aspect.PointcutDefinitions.logAnnotatedMethods”。我注意到您创建了 logAnnotatedMethods 但我没有得到 com.merc.aop.ctw.aspect.PointcutDefinitions 是什么?请指导我。

标签: aspectj


【解决方案1】:
@Pointcut("@annotation(com.merc.annotations.Profile)")
protected void logAnnotatedMethods(Profile profile) {
}

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

如果你的类是用调试代码编译的,切入点参数必须与方法参数同名,如果不是,你需要依赖参数类型是唯一的,或者使用@987654326显式写出你的参数名称@参数:

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

参考:

【讨论】:

  • 我不同意。只需使用带有注释类型的 @annotation 作为参数,它就像在 Spring 3.1 中的魅力一样,正如文档所说的那样。 static.springsource.org/spring/docs/3.0.3.RELEASE/…
  • 实际上,文档指定了两个版本。您将在本节中找到我的版本:static.springsource.org/spring/docs/3.0.3.RELEASE/…。但是,我不知道其他版本。
  • 但是:这是一个 AspectJ 问题,而不是 Spring AOP 问题,因此 Spring 文档几乎不相关。但是我从 AspectJ in Action 中得到了这个:@annotation(TypePattern or ObjectIdentifier) 这再次表明我们都是对的,就像在 Spring AOP 中一样
【解决方案2】:

我在玩,发现以下工作

@Pointcut("@annotation(profile)")
protected void logAnnotatedMethods(Profile profile) {
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    相关资源
    最近更新 更多