我正在尝试组合 getter 和 setter 的多个切入点,以创建一个在执行两个切入点时将执行的建议。我已经尝试过正常的AspectJ类和注释@Aspect类,但它仍然给了我警告建议DidNotMatch,最终建议没有执行。奇怪的是,如果我用 || 更改 &&(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");
}
}
以下是我想建议的测试类:
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 在他/她的评论中建议使用||
。
如果您想实现其他目标,请以易于理解的方式进行解释,如有必要,请举例说明、评论或更新您的问题。我真的没有得到你真正想要的。