具有注释参数的aspectj切入点

use*_*201 15 aspectj

我使用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) {
}
Run Code Online (Sandbox Code Playgroud)

但是在使用AJC编译时我得到以下错误消息

formal unbound in pointcut 
Run Code Online (Sandbox Code Playgroud)

Sea*_*oyd 20

@Pointcut("@annotation(com.merc.annotations.Profile)")
protected void logAnnotatedMethods(Profile profile) {
}
Run Code Online (Sandbox Code Playgroud)

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

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

@Pointcut(value="@annotation(profile)",argNames="profile")
protected void logAnnotatedMethods(Profile arg) {    }
Run Code Online (Sandbox Code Playgroud)

参考:

  • 我不同意。只需使用 @annotation 和注释类型作为参数,它就像 Spring 3.1 中的魅力一样,正如文档所说的那样。http://static.springsource.org/spring/docs/3.0.3.RELEASE/spring-framework-reference/html/aop.html (2认同)
  • 但是然后:这是一个AspectJ问题,而不是Spring AOP问题,因此Spring文档几乎没有关系。但是我是从《 AspectJ in Action:@annotation(TypePattern或ObjectIdentifier)》一书中得出的,再次表明我们都是对的,就像在Spring AOP中一样 (2认同)

use*_*201 7

我在玩,发现以下工作

@Pointcut("@annotation(profile)")
protected void logAnnotatedMethods(Profile profile) {
}
Run Code Online (Sandbox Code Playgroud)