我正在寻找的是一种在类级别变量周围指定切入点的方法。就像是:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.FIELD)
@interface MyPointCut
{
}
public class Foo
{
@MyPointCut
BarAPI api = new BarAPI();
}
@Aspect
public class MyAspect
{
@Around(value="execution(* *(..)) && @annotation(MyPointCut)")
public Object doSomethingBeforeAndAfterEachMethodCall()
{
...
}
}
然后,我希望有一个方面可以在api字段的每次方法调用之前和之后执行一些工作。
这可行吗?您能否指出一些文档,以便我阅读如何做?
最佳答案
这有点像简单地将执行建议放在BarAPI类型的所有方法上,但是您的区别在于,您只关心BarAPI的特定实例,而不是所有实例。
// Execution of any BarAPI method running in the control flow of a Foo method
Object around(): execution(* BarAPI.*(..)) && cflow(within(Foo)) {...}
cflow为此有点“繁重”,我们可以做些更轻松的事情:
// Call to any BarAPI method from the type Foo
Object around(): call(* BarAPI.*(..)) && within(Foo) { ... }
那类似但更普遍适用的东西呢?
// Assume Foo has an annotation on it so it is more general than type Foo.
@HasInterestingBarAPIField
public class Foo { ... }
Object around(): call(* BarAPI.*(..)) && @within(HasInterestingBarAPIField) { ... }
关于java - 是否可以围绕类级别变量创建AspectJ切入点?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38499366/