![](/img/trans.png)
[英]“adviceDidNotMatch” warning when i try adding a new advice to an aspectJ
[英]Combining multiple pointcuts AspectJ returns adviceDidNotMatch warning
我試圖結合getter和setter的多個切入點以創建一個建議,如果兩個切入點都被執行,則將被執行。 我已經在正常的AspectJ類和注解@Aspect類中進行了嘗試,但是仍然給我警告warningDidNotMatch,最終該建議未執行。 奇怪的是,如果我用||更改&&(AND) (OR)有效,但是為什么&&根本不起作用?
這是在普通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()
是互斥的。 因此,將它們與&&
組合-即創建兩個互不相交的連接點集的交集-邏輯上導致空結果集。 抱歉,您的組合切入點不匹配。 您應該使用||
正如他/她的評論中所建議的那樣。
如果您想實現其他目標,請以通俗易懂的方式進行解釋,必要時提供示例,評論或更新您的問題。 我真的沒有得到你真正想要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.