gpt4 book ai didi

java - 使用 pointcut=@annotation 进行 @AfterThrowing 的方面运行了两次

转载 作者:行者123 更新时间:2023-12-02 15:38:06 24 4
gpt4 key购买 nike

我在使用 AspectJ 使用自定义注释进行切入点时遇到了奇怪的行为。

我使用的切入点是:

@AfterThrowing(pointcut="@annotation(com.core.meta.NotifyOnFailure)", throwing="ex")

我遇到的问题是我的方面执行了两次,但是如果我将切入点修改为:

@AfterThrowing(pointcut="execution(* sendAndReceive(..))", throwing="ex")

它按预期运行一次。

我唯一能提高方面的方法是:

    @NotifyOnFailure // I want to use this annotation to raise the aspect once
public String sendAndReceive(String serviceUrl)
{
String responseXml = "...";
try
{
throw new Exception("test...");
}
catch(Exception x)
{
ExternalExecutionException ex = new ExternalApiExecutionException("Service failed");
throw ex;
}
finally
{
...
}

return responseXml;
}

知道为什么我的方面在使用自定义注释时执行两次而在使用执行切入点时仅执行一次吗?

最佳答案

确保您还将切入点限制为执行。否则,它将仅限于注释,因此可用于调用执行(如果 AspectJ 可以建议调用您的方法,那么它可以,因为它位于你自己的代码)。两者之间的一个很好的比较在这里:https://stackoverflow.com/a/18149106/2191746

你的切入点将如下所示:

@AfterThrowing(pointcut="execution(* *(..)) && @annotation(com.core.meta.NotifyOnFailure)", throwing="ex")

无法保证语法,因为我手头没有aspectJ编译器。

关于java - 使用 pointcut=@annotation 进行 @AfterThrowing 的方面运行了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29017011/

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