第二个没有编译,因为
within()
需要类型签名,而不是方法签名。你所指的答案完全错了,我不知道为什么会被接受。我刚刚写了
a new one
为了纠正错误信息。
您最后的尝试基本上是正确的,但只会忽略二传手,而不会忽略获得者。尝试以下操作:
驱动程序应用程序:
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
请注意
-
如果有布尔值的getter,如
isActive()
如果要忽略它们,必须通过以下方式扩展切入点
&& !execution(boolean is*())
.
-
那些带有名称模式的切入点总是启发式的,所以要小心方法名称,例如
getaway
,
settleConflict
,
isolate
. 他们也会被忽视。