【问题标题】:Aspect using pointcut=@annotation for @AfterThrowing is running twice使用切入点=@annotation for @AfterThrowing 的方面运行了两次
【发布时间】:2015-05-15 00:52:44
【问题描述】:

我在使用 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;
    }

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

【问题讨论】:

    标签: java spring aspectj


    【解决方案1】:

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

    您的切入点将如下所示:

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

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

    【讨论】:

    • 非常感谢您指出这一点,您对我帮助很大。 +1
    • 没问题。继续使用 AspectJ 进行货运,这是值得的。 :)
    猜你喜欢
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-23
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 2019-12-07
    相关资源
    最近更新 更多