gpt4 book ai didi

java - 如何在不使用构造函数的情况下初始化 AspectJ 切面?

转载 作者:行者123 更新时间:2023-11-29 05:45:38 25 4
gpt4 key购买 nike

我的 AspectJ 方面看起来像这样:

@Aspect
public class MyAspect {
private Child child;
public MyAspect() {
this.child = new Child();
}
@Around("... skipped ...")
public Object wrap(ProceedingJoinPoint point) throws Throwable {
// some custom functionality
return point.proceed();
}
}

这是行不通的,因为在 Child 的构造过程中调用了一个 wrap() 切入点并导致运行时异常,因为 的实例MyAspect 尚未就绪。

是否可以告诉 AspectJ 在实例化后立即调用 MyAspect 的某些方法?

最佳答案

新答案:

这是一个示例驱动程序类 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 的公共(public)方法和构造函数执行,但不包括切面构造函数:

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())

这应该排除您不想拦截的内容。

关于java - 如何在不使用构造函数的情况下初始化 AspectJ 切面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15954607/

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