2011-12-04 49 views
0

我想用guice實現方法攔截。 我想能夠註釋方法並攔截它們,並且在嘗試調用bindInterceptor時遇到錯誤。Java - 使用guice的攔截方法?

的錯誤是: 方法bindInterceptor(匹配器,匹配器,MyInterceptor)是未定義的類型MyModule的

難道我做錯了什麼?

public class MyInterceptor implements MethodInterceptor { 

    @Override 
    public Object invoke(MethodInvocation arg0) throws Throwable { 
     return arg0.proceed(); 
    } 
} 

public class MyModule extends AbstractModule { 

    @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) 
    @interface MyAnnotation {} 

    @Override 
    protected void configure() { 
      // I get an error on this line 
     bindInterceptor(Matchers.any(), Matchers.annotatedWith(MyAnnotation.class), 
       new MyInterceptor()); 
    } 
} 
+0

我沒有問題得到這個編譯和運行使用guice 3.0攔截器工作。你的進口報表是什麼樣的? –

回答

3

這個錯誤通常是獲得了錯誤的importMatchers一個,AbstractModule,或MethodInterceptor的結果。

這三條進口線是什麼?你應該有:

import com.google.inject.AbstractModule; 
import com.google.inject.matcher.Matchers; 
import org.aopalliance.intercept.MethodInterceptor;