gpt4 book ai didi

java - 按方面禁用方法的实际调用

转载 作者:行者123 更新时间:2023-12-01 09:12:50 24 4
gpt4 key购买 nike

是否可以禁用实际方法的调用。我想要实现的是创建一个方面,它将在我的方法之前调用,如果某些语句为真,则根本不调用主方法。

使用伪代码,它会是这样的

public class MyClass {

public void myMethod() {
//implementation
}
}

@Aspect
public class MyAspect {
@Before("execution(* MyClass.myMethod(..))")
public void doSth() {
//do something here but if some statement is true then don't call myMethod
}
}

有可能吗?或者也许可以用其他东西而不是方面?

最佳答案

使用@AroundProceedingJoinPoint你应该能够做到这一点。例如

  @Around("execution(* MyClass.myMethod())")
public void doSth(ProceedingJoinPoint joinpoint) throws Throwable {

boolean invokeMethod = false; //Should be result of some computation
if(invokeMethod)
{
joinpoint.proceed();
}
else
{
System.out.println("My method was not invoked");
}
}

我在这里将 invokeMethod boolean 值设置为 false,但它应该是一些计算的结果,您可以通过这些计算来确定是否要执行方法。 joinPoint.proceed 实际调用该方法。

关于java - 按方面禁用方法的实际调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40811109/

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