需要帮助创建利用方法注释中的值的特定切入点

发布于 2024-10-12 22:05:18 字数 1309 浏览 3 评论 0原文

我有以下方法

    @AutoHandling(slot = FunctionalArea.PRE_MAIN_MENU)
    @RequestMapping(method = RequestMethod.GET)
    public String navigation(ModelMap model) {
        logger.debug("navigation");
        ...

            //First time to the Main Menu and ID-Level is ID-1 or greater
            if (!callSession.getCallFlowData().isMainMenuPlayed()
                    && callSession.getCallFlowData().getIdLevel() >= 1) {
                // Call Auto Handling                    
                logger.info("Call AutoHandling");
                autoHandlingComponent.processAutoHandling();
            }
        ...

        return forward(returnView);
    }

基本上我想做的是在 processAutoHandling() 上有一个切入点 但是在@After中,我需要使用@AutoHandling的slot()

我尝试过这个,但它没有被调用

@Pointcut("execution(* *.processAutoHandling())")
public void processAutoHandleCall() {
    logger.debug("processAutoHandleCall");
}

@Around("processAutoHandleCall() &&" +
        "@annotation(autoHandling) &&" +
        "target(bean) "
)
public Object processAutoHandlingCall(ProceedingJoinPoint jp,
                                      AutoHandling autoHandling,
                                      Object bean)
        throws Throwable {
         ...

I have the following method

    @AutoHandling(slot = FunctionalArea.PRE_MAIN_MENU)
    @RequestMapping(method = RequestMethod.GET)
    public String navigation(ModelMap model) {
        logger.debug("navigation");
        ...

            //First time to the Main Menu and ID-Level is ID-1 or greater
            if (!callSession.getCallFlowData().isMainMenuPlayed()
                    && callSession.getCallFlowData().getIdLevel() >= 1) {
                // Call Auto Handling                    
                logger.info("Call AutoHandling");
                autoHandlingComponent.processAutoHandling();
            }
        ...

        return forward(returnView);
    }

Basically what I want to do, is have a pointcut on processAutoHandling()
But in the @After, I need to use the slot() for @AutoHandling

I tried this, but it does not get called

@Pointcut("execution(* *.processAutoHandling())")
public void processAutoHandleCall() {
    logger.debug("processAutoHandleCall");
}

@Around("processAutoHandleCall() &&" +
        "@annotation(autoHandling) &&" +
        "target(bean) "
)
public Object processAutoHandlingCall(ProceedingJoinPoint jp,
                                      AutoHandling autoHandling,
                                      Object bean)
        throws Throwable {
         ...

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

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

发布评论

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

评论(2

倾听心声的旋律 2024-10-19 22:05:18

您可以为此使用虫洞设计模式。我正在说明使用基于 AspectJ 字节码的方法和语法,但如果您使用 Spring 的基于代理的 AOP,您应该能够使用显式 ThreadLocal 获得相同的效果。

pointcut navigation(AutoHandling handling) : execution(* navigation(..)) 
                                             && @annotation(handling);

// Collect whatever other context you need
pointcut processAutoHandleCall() : execution(* *.processAutoHandling());

pointcut wormhole(AutoHandling handling) : processAutoHandleCall() 
                                           && cflow(navigation(handling));

after(AutoHandling handling) : wormhole(hanlding) {
   ... you advice code
   ... access the slot using handling.slot()
}

You can use the wormhole design pattern for this. I am illustrating using AspectJ byte-code based approach and syntax, but you should be able to get the same effect using an explicit ThreadLocal if you are using Spring's proxy-based AOP.

pointcut navigation(AutoHandling handling) : execution(* navigation(..)) 
                                             && @annotation(handling);

// Collect whatever other context you need
pointcut processAutoHandleCall() : execution(* *.processAutoHandling());

pointcut wormhole(AutoHandling handling) : processAutoHandleCall() 
                                           && cflow(navigation(handling));

after(AutoHandling handling) : wormhole(hanlding) {
   ... you advice code
   ... access the slot using handling.slot()
}
2024-10-19 22:05:18

a)它不起作用,您试图匹配两个不同的东西:

@Around("processAutoHandleCall() &&" +
        "@annotation(autoHandling) &&" +
        "target(bean) "
)

processHandleCall() 匹配内部方法执行 autoHandlingComponent.processAutoHandling()@annotation (autoHandling) 与外部方法执行 navigation(ModelMap model)

b) 由于您显然是在尝试向控制器提供建议,因此有一些注意事项:

  • 如果您使用 proxy- target-class=true 一切都应该按原样工作,只需确保您没有任何最终方法(
  • 如果没有),所有控制器方法都必须由接口和 @RequestMapping< /code> 等注释必须位于接口上,而不是 Spring MVC 参考文档的这一部分

a) It can't work, you are trying to match two different things:

@Around("processAutoHandleCall() &&" +
        "@annotation(autoHandling) &&" +
        "target(bean) "
)

processHandleCall() matches the inner method execution autoHandlingComponent.processAutoHandling() while @annotation(autoHandling) matches the outer method execution navigation(ModelMap model)

b) since you are obviously trying to advise a Controller, there are a few caveats:

  • if you use proxy-target-class=true everything should work as is, just make sure you don't have any final methods
  • if you don't, all your controller methods must be backed by an interface and the @RequestMapping etc annotations must be on the interface, not the implementing class as described in this section of the Spring MVC reference docs
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文