我正在尝试组合 getter 和 setter 的多个切入点来创建一个建议,如果两个切入点都被执行,该建议将被执行。我已经在普通的 AspectJ 类和注释 @Aspect 类中尝试过,但它仍然给我警告adviceDidNotMatch,最终建议不被执行。奇怪的是,如果我用 || 改变 && (AND) (或者)它有效,但为什么 && 根本不起作用?
这是在普通 AspectJ 类中声明的建议。
package testMaven;
pointcut getter() : execution(* testMaven.testing.getDd(..));
before() : getter(){
System.out.println("test get");
}
pointcut setter() : execution(* testMaven.testing.setDd(..));
before() : setter(){
System.out.println("test set");
}
pointcut combine(): getter() && setter();
before(): combine(){
System.out.println("testing combine");
}
}
这是注释@Aspect类中声明的建议
package testMaven;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.Before;
@Aspect
public class aspecter {
@Pointcut("call (* testMaven.testing.getDd(..))")
public void getter(){
}
@Pointcut("call (* testMaven.testing.setDd(..))")
public void setter(){}
@Pointcut("execution (* testMaven.tester.setZ(..))")
public void setterZ(){}
@Before("setterZ()")
public void settingZ(){
System.out.println("before set Z");
}
@Pointcut("getter() && setter()")
public void getterSetter(){}
@After("getterSetter()")
public void testerd(){
System.out.println("works");
}
@Pointcut("getter() && setterZ()")
public void getterSetter2(){}
@After("getterSetter2()")
public void testinger(){
System.out.println("ok");
}
}
这是我想要建议的测试类(class):
package testMaven;
public class testing {
public int dd;
public int getDd() {
return dd;
}
public void setDd(int dd) {
this.dd = dd;
}
}
package testMaven;
public class testing {
public int dd;
public int getDd() {
return dd;
}
public void setDd(int dd) {
this.dd = dd;
}
public void aa(int a){
System.out.println(a);
}
}
这是主类:
package testMaven;
public class MainApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
testing test = new testing();
test.aa(2);
test.setDd(3);
tester et = new tester();
et.setZ(3);
et.printNo(1000);
System.out.println(test.getDd());
}
}
我的代码有问题吗?如有任何帮助,我们将不胜感激。
谢谢
最佳答案
您询问您的代码是否有问题。答案是肯定的。两个切入点 setter()
和 getter()
是互斥的。因此,将它们与 &&
结合起来 - 即创建两个相互分离的连接点集的交集 - 逻辑上会导致一个空结果集。因此,您的组合切入点不匹配。您应该按照 uniknow 在他/她的评论中建议的那样使用 ||
。
如果您想实现其他目标,请以易于理解的方式进行解释,如有必要,请通过举例、评论或更新您的问题来解释。我真的没有得到你真正想要的。
关于java - 组合多个切入点 AspectJ 返回adviceDidNotMatch 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37542171/