新答案:
这是一个示例驱动程序类Child
:
public class Child {
private String name;
public Child(String name) {
this.name = name;
System.out.println("Constructing child named " + this.name);
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public static void main(String[] args) {
Child myChild = new Child("Penélope");
System.out.println("My child is named " + myChild.getName());
myChild.setName("María Elena");
System.out.println("My child is now named " + myChild.getName());
}
}
这是一个MyAspect
拦截公共方法和构造函数执行Child
的切面,但不包括切面构造函数:
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class MyAspect {
private Child child;
public MyAspect() {
child = new Child("Scarlett");
System.out.println("Aspect child is named " + child.getName());
child.setName("Cristina");
System.out.println("Aspect child is now named " + child.getName());
}
@Around(
"(execution(public * Child.*(..)) || execution(public Child.new(..)))" +
"&& !cflow(initialization(MyAspect.new()))"
)
public Object wrap(ProceedingJoinPoint point) throws Throwable {
System.out.println(point.getStaticPart());
return point.proceed();
}
}
这是示例输出:
Constructing child named Scarlett
Aspect child is named Scarlett
Aspect child is now named Cristina
execution(void Child.main(String[]))
execution(Child(String))
Constructing child named Penélope
execution(String Child.getName())
My child is named Penélope
execution(void Child.setName(String))
execution(String Child.getName())
My child is now named María Elena
您可以看到Child
在方面构造过程中没有截取任何连接点。顺便说一句,否则你NoAspectBoundException
无论如何都会得到。;-)
老答案:
您可以将切入点与(未经测试的)类似的东西结合起来:
... && !cflow(MyAspect.new())
这应该排除您不希望拦截的内容。