那是A类:
public class A {
public B b;
public void justDoIt1(){
b.getB();
}
@SomeAnnotation
public void justDoIt2(){
b.getB();
}
}和乙级:
public class B{
public void getB(){
System.out.println("get");
}
}我们如何创建执行B.getB()的切入点,这个切入点是从带有@SomeAnnotation注解的方法中调用的。
这是我尝试过的
@Aspect
public class LocalizationAspect {
@Before(value = "@within(Localize) && execution(* B.getB())")
public void aspectStuff() {
System.out.println("aspect");
}
}为了说明我的观点:预期的输出将是在调用justDoIt2()时;
方面得到
但是当调用justDoIt1()时;
到达
注意:我正在使用SpringAOP (也许它对此有一些限制),对此有帮助吗?
发布于 2014-07-10 08:17:48
如果我使用的是普通的AspectJ,我会这样做:
execution(* B.getB()) && cflow(@withincode(SomeAnnotation))
“在带有SomeAnnotation注解的方法的控制流中执行SomeAnnotation ()。但这确实意味着如果流的深度更深,它就会被捕获,这可能不是您想要的。例如,如果带有SomeAnnotation注释的方法调用其他调用的东西,然后调用getB(),则会被这个建议捕获。
不过,我不知道它在Spring下的表现如何。
编辑:进一步考虑,上面的切入点可能不是最优的,因为@withincode()可能会创建比绝对必要的更多的字节代码。更理想的版本可能是:
execution(* B.getB()) && cflow(execution(@SomeAnnotation * *(..)))
@withincode(SomeAnnotation)将通知标记为@SomeAnnotation的方法中的所有连接点,但您可能只对执行连接点感兴趣。
https://stackoverflow.com/questions/24658275
复制相似问题