命名和匿名切入点注释的不同分辨率范围?

时间:2013-02-08 07:43:39

标签: annotations aspectj pointcuts

我正在使用AspectJ注释,由于某种原因,似乎切入点的分辨率范围因命名切入点与匿名切入点而不同。

例如,在下面的代码中,如果是匿名的,则解析相同的切入点,而不是在命名时解析。但是,如果我使用通配符而不是特定类型,则命名切入点将匹配。

有什么想法吗?

import some_other_package.not_the_one_where_this_aspect_is.Account;

@Aspect
public class MyClass {


//this does not match... but matches if Account is replaced by *
@Pointcut("execution(* Account.withdraw(..)) && args(amount)")
public void withdr(double amount){}

@Before("withdr(amount)")
public void dosomething1(double amount){}


//this matches
@Before("execution(* Account.withdraw(..)) && args(amount)")
public void dosomthing2(double amount){}

}

1 个答案:

答案 0 :(得分:0)

在@AspectJ语法中,根据文档,导入无用。您需要完全限定类名或使用jokers。在第二种情况下进口是有效的,而不是预期行为的异常而非预期。你不能依赖它。

如果您这样做,两种变体都可以使用:

@Aspect
public class AccountInterceptor {
    @Pointcut("execution(* *..Account.withdraw(..)) && args(amount)")
    public void withdraw(double amount) {}

    @Before("withdraw(amount)")
    public void doSomething1(JoinPoint joinPoint, double amount) {
        System.out.println(joinPoint + " -> " + amount);
    }

    @Before("execution(* *..Account.withdraw(..)) && args(amount)")
    public void doSomething2(JoinPoint joinPoint, double amount) {
        System.out.println(joinPoint + " -> " + amount);
    }
}