需要帮助在方法内部创建特定切入点

发布于 2024-10-13 09:07:32 字数 1478 浏览 1 评论 0原文

我从一个原始问题开始 需要帮助创建使用方法注释中的值的特定切入点

我决定问另一个问题来改变我正在采取的方法。 我有一个方法(导航),该方法在该方法内部调用另一个方法,我希望获得 @Around 建议。

@RequestMapping(method = RequestMethod.GET)
public String navigation(ModelMap model) {
    ...        
            // Call Auto Handling
            logger.info("Call AutoHandling");
            this.processAutoHandling(callSession, FunctionalArea.PRE_MAIN_MENU);
        }
        ...

    return forward(returnView);
}

这是否可能,因为如果该方法位于同一个类中,我似乎无法使其工作。

如果它不在对象本身上,则此工作有效:

@Around("execution(* *.processAutoHandling(..)) &&" +
        "args(callSession, functionalArea) && " +
        "args(functionalArea) && " +
        "target(bean)"
)
public Object processAutoHandlingCall2(ProceedingJoinPoint jp,
                                      CallSession callSession,
                                      FunctionalArea functionalArea,
                                      Object bean)
        throws Throwable {
    logger.debug("processAutoHandleCall");
    return jp.proceed();
}

在我的控制器中使用此调用:

autoHandlingComponent.processAutoHandling(callSession, FunctionalArea.PRE_MAIN_MENU);

而不是

this.processAutoHandling(callSession, FunctionalArea.PRE_MAIN_MENU);

I started with an original question on
Need help creating a specific pointcut that utilizes a value from a method annotation

I decided I wanted to ask another question to change the approach I was taking.
I have a method (navigation), that has a call inside of that method to another method which I would like to have @Around advice.

@RequestMapping(method = RequestMethod.GET)
public String navigation(ModelMap model) {
    ...        
            // Call Auto Handling
            logger.info("Call AutoHandling");
            this.processAutoHandling(callSession, FunctionalArea.PRE_MAIN_MENU);
        }
        ...

    return forward(returnView);
}

Is this possible as I cannot seem to get this to work if the method is inside of the same class.

This work if it was not on the object itself:

@Around("execution(* *.processAutoHandling(..)) &&" +
        "args(callSession, functionalArea) && " +
        "args(functionalArea) && " +
        "target(bean)"
)
public Object processAutoHandlingCall2(ProceedingJoinPoint jp,
                                      CallSession callSession,
                                      FunctionalArea functionalArea,
                                      Object bean)
        throws Throwable {
    logger.debug("processAutoHandleCall");
    return jp.proceed();
}

With this call in my controller:

autoHandlingComponent.processAutoHandling(callSession, FunctionalArea.PRE_MAIN_MENU);

instead of

this.processAutoHandling(callSession, FunctionalArea.PRE_MAIN_MENU);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙女 2024-10-20 09:07:32

看来您正在使用 Spring 的基于代理的 AOP。如果是这样,这是一个已知的限制。请参阅 了解 Spring 文档中的 AOP 代理以获取更多详细信息。您有两种方法可以解决此问题:

  1. 使用文档中概述的 AopContext.currentProxy() 方法。我不鼓励这种方法,因为您的代码现在将非常明确地与 Spring AOP 绑定。
  2. 使用 AspectJ 的字节码编织。由于它不涉及代理,因此您不会遇到“this”指向原始对象的问题,并且代理透明地仅对外部对象可用。

It seems that you are using Spring's proxy-based AOP. If so, this is a known limitation. See Understanding AOP Proxies from Spring documentation for more details. You have two ways to solve this issue:

  1. Use the AopContext.currentProxy() approach outlined in the documentation. I will discourage this approach, since your code will now be tied to Spring AOP quite explicitly.
  2. Use AspectJ's byte-code weaving. Since there is no proxy involved with it, you won't have the issue with 'this' pointing to original object and proxy is transparently available only to external objects.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文