2011-01-21 82 views
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

我尝试这样做,但它不会被调用

@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 { 
     ... 

回答

2

描述您可以使用虫洞设计模式为@RequestMapping等标注必须是接口,而不是实现类上这个。我正在说明如何使用基于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() 
} 
0

一)它不能正常工作,您要匹配两个不同的东西:

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

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

B),因为你明显是想提醒一个控制器,有几个注意事项相符:

  • 如果您使用proxy-target-class=true一切都应该按原样工作,只要确保您没有任何最终方法
  • ,如果你不这样做,你所有的控制器方法必须通过一个接口的支持,并为this section of the Spring MVC reference docs
+0

那我可以在实际的呼叫添加注释吗? – 2011-01-21 15:10:12

+0

@Mick你不能注释一个方法调用,但你可以注释processAutoHandling()方法。 – 2011-01-21 15:19:16