2016-11-02 41 views
2

我需要攔截org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#處理方法,我使用spring aop。它是spring-webmvc.jar的spring類。它在org.springframework.web.servlet.DispatcherServlet中的請求過程中用於我的rest mvc服務。如何使用彈簧aop從外部jar的類

AOP配置:

<bean id="contextAspect" class="com.eightbitlab.spring3rest.config.ContextAspect"/> 

    <aop:aspectj-autoproxy proxy-target-class="true"/> 
    <aop:config> 
     <aop:aspect ref="contextAspect"> 
      <aop:pointcut id="contextPointcut" expression="execution (* org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handle(..))"/> 
      <aop:around method="around" pointcut-ref="contextPointcut"/> 
     </aop:aspect> 
    </aop:config> 

看點豆:

@Aspect 
public class ContextAspect { 
    public void around(ProceedingJoinPoint joinPoint) throws Throwable { 
     Logger.getAnonymousLogger().info("start aspect"); 
     Object proceeded = joinPoint.proceed(); 
     Logger.getAnonymousLogger().info("end aspect"); 
    } 
} 

如果我用自己的類執行方面的方法這方面。我認爲這個問題是一個來自外部jar的類,但我找不到解決方案...

回答

0

如果你看handle方法的簽名,它會被標記爲final

由於Spring AOP不能勸final方法(見下面摘錄春天文檔here)考慮攔截/建議,而不是你的控制器。

無法當您使用Spring MVC的最後方法添加的建議。例如,您不能將建議添加到AbstractController.setSynchronizeOnSession()方法中。請參閱Section 10.6.1,「瞭解AOP代理」以獲取有關AOP代理的更多信息,以及爲什麼不能將建議添加到最終方法。

P.S.:另外看看here以及進一步瞭解AOP代理。

+0

感謝您的信息。但是從接口「處理」的方法,我認爲如果我會使用動態代理而不是CGI它會工作,但是當我添加 - 沒有改變( – miroshnik

+0

在切入點你已明確提到建議'RequestMappingHandlerAdapter.handle'的執行已將其標記爲'final',所以不管代理類型如何,它都不起作用 –

+0

即使切入點更新爲「target」(org.springframework.web.servlet .HandlerAdapter),Spring運行時將會選擇'RequestMappingHandlerAdapter',因爲它是通過'@ RequestMapping'配置的映射的查詢處理程序。除非你有特別的理由堅持建議'handle'方法IMO嘗試通知控制器。 –