2015-10-18 88 views
2

在Guice中,有沒有一種方法可以調用我的MethodInterceptor::invoke執行攔截的方法被執行(而不是之前)?Guice post執行方法攔截

我已經添加了當前的代碼我AbstractModule

bindInterceptor(Matchers.subclassesOf(InterceptedClass.class), Matchers.annotatedWith(MyMethodAnnotation.class), new MyMethodInterceptor()); 

回答

3

要在攔截器的方法調用後執行代碼(這不僅適用於吉斯),你必須使用一個try /最後組合:

public Object invoke(MethodInvocation invocation) throws Throwable { 
    try { 
     // code run before execution 

     return invocation.proceed(); 
    } finally { 
     // code run after execution 
    } 
} 
+0

謝謝Jan,它的工作原理! – Shirile