java拦截一个方法两次
我有两个方面将要执行一些验证。方面截取了用@NotNull
和@IsUnique
注释注释的所有方法。例如:
@NotNull
@IsUnique
public void save(Player p){
// persisting
}
方面:
public aspect NotNullInterceptor{
pointcut NotNull(): execution(public * (@NotNull *).*(..));
before() : NotNull() {
//Validation and handling
}
}
public aspect IsUniqueInterceptor{
pointcut IsUnuque(): execution(public * (@IsUnique *).*(..));
before() : IsUnuque() {
//Validation and handling
}
}
问题是我需要在IsUnique
验证之前严格执行NotNull
验证,以避免抛出NullPointerException
如果我将@NotNull
注释放在@IsUnique
注释之前,是否可靠
# 1 楼答案
不是。切入点是一个布尔函数,用于确定是否应用某个方面。它不影响方面的顺序。此外,一般来说,从切入点表达式中推断出这样的顺序是不可能的。例如,考虑:
以及切入点表达式:
第一个切入点应该首先匹配是因为
@First
在@Second
之前,还是最后匹配是因为@Third
在@Second
之后