簡體   English   中英

如何配置AspectJ忽略getter和setter

[英]How to configure aspectj ignore getters and setters

我有一個方面目前可以捕獲包中的所有公共方法執行。

我想對其進行修改,以同時排除setter和getter,所以我嘗試了,這些是我嘗試過的變體:

這是可行的,但顯然對設置方法或獲取方法沒有任何作用。

@Around("execution(public * *(..)) && !within(com.walterjwhite.logging..*)")

這不會編譯:

@Around("execution(public * *(..)) && !within(* set*(..))")

這可以編譯,但不會阻止捕獲設置器/獲取器:

@Around("execution(public * *(..)) && !execution(* set*(..))")

我也參考了這篇文章,但這沒有用。 嘗試編譯方面時出現編譯錯誤。

如何在AspectJ中排除獲取器和設置器?

第二個不編譯,因為within()需要類型簽名,而不是方法簽名。 您所指的答案是完全錯誤的,我不知道為什么會接受。 我只是寫了一個新書 ,以糾正那些虛假信息。

您的最后一次嘗試基本上是正確的,但僅忽略設置方法,而不忽略獲取方法。 嘗試這個:

驅動程序應用程序:

package de.scrum_master.app;

public class Application {
  private int id;
  private String name;

  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public void doSomething() {
    System.out.println("Doing something");
  }

  public static void main(String[] args) {
    Application application = new Application();
    application.setId(11);
    application.setName("John Doe");
    application.doSomething();
    System.out.println(application.getId() + " - " + application.getName());
  }
}

方面:

package de.scrum_master.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class MyAspect {
  @Around("execution(public * *(..)) && !execution(void set*(*)) && !execution(!void get*())")
  public Object myAdvice(ProceedingJoinPoint thisJoinPoint) throws Throwable {
    System.out.println(thisJoinPoint);
    return thisJoinPoint.proceed();
  }
}

控制台日志:

execution(void de.scrum_master.app.Application.main(String[]))
execution(void de.scrum_master.app.Application.doSomething())
Doing something
11 - John Doe

請注意

  • 如果您有類似isActive()這樣的布爾值的吸氣劑,並且也想忽略它們,則必須通過&& !execution(boolean is*())擴展切入點。
  • 帶有名稱模式的切入點始終只是啟發式的,因此請提防方法名稱,例如getawaysettleConflictisolate 他們也將被忽略。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM