2011-01-21 30 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