我正在努力创建一个aspectj切入点,因为我无法弄清楚如何将切入点限制为从对象的构造函数到其自己的方法的调用(但排除调用其他实例上的方法同一类)。
背景:
我有一个应用程序,对象需要在对象的数据发生变化时通知他们的“观察者”。为了实现这一点,我有一些用@NotifiableChange注释修饰的方法,以及在这些方法调用完成后触发通知过程的方面。
难点在于我不想在对象构造期间触发通知,只有在构造后调用方法时才会触发通知。即从objectA的构造函数到objectA自己的方法的调用不应该包含在切入点中。但是,在objectA的构造函数的过程中对objectB的方法的调用应该包含在切入点中。
我完全把自己束缚在尝试各种内码,内部,cflow,这个和目标但不能创造正确的切入点的结。这就是我现在所拥有的:( DataChangeNotifier是相关类实现的接口)
pointcut callsWithinConstructors(DataChangeNotifier notifierObject):
// call to a notifiable method
call(@NotifiableChange * *(..))
//on this object
&& this(notifierObject)
//in the execution of a constructor
&& withincode(DataChangeNotifier+.new(..));
// cut any method with any parameters with this annotation
pointcut notifiable(DataChangeNotifier notifierObject):
call(@NotifiableChange * DataChangeNotifier+.*(..))
&& target(notifierObject)
//but not from the constructors (because there should be no notifications during construction)
&& !cflow(callsWithinConstructors(DataChangeNotifier+))
//and not from the method that gets called by notifiers - this method is handled below
&& !withincode(* DataChangeNotifier+.listenedDataHasChanged(..));
但似乎第一个切入点是排除在构造函数中发生的所有方法调用,而不仅仅是它自己的方法。
请帮忙 - 我疯了!
由于
答案 0 :(得分:0)
您是否尝试过poincut谓词:初始化(DataChangeNotifier +)?这会将连接点限制为DataChangeNotifier及其子类型的构造函数。
您可以通过添加 cflow()和执行来进一步限制连接点(DataChangeNotifier + .. (..))*
如何将切入点限制为从对象的构造函数到其自己的方法的调用
pointcut myOwnMethod(): execution(DataChangeNotifier+..*(..))
pointcut myConstructor() : initialization(DataChangeNotifier+)
pointcut executionOfMyOwnMethodsInMyConstructor(): cflow(myConstructor()) && myOwnMethod()
关于对当前实例方法的限制(而不是其他实例)确实非常棘手......我不确定是否可能,我明天会做一些测试< / p>