gpt4 book ai didi

java - 是否可以围绕类级别变量创建 AspectJ 切入点?

转载 作者:行者123 更新时间:2023-12-02 03:28:11 26 4
gpt4 key购买 nike

我正在寻找一种围绕类级别变量指定切入点的方法。像这样的东西:

@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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com