关闭Google Guice中的匹配

时间:2012-01-31 12:30:59

标签: java dependency-injection aop guice

我目前有一个使用以下绑定的Module impl:

binder.bindInterceptor(Matchers.any(), Matchers.any(),
    new WidgetInterceptor());

我希望能够以编程方式打开/关闭此功能,这就是我所做的:

private boolean widgetInterceptionEnabled = true;

public void configure(Binder binder) {
    Matcher<Object> matcher = null;
    if(widgetInterceptionEnabled)
        matcher = Matchers.any();
    else
        matcher = Matchers.not(Matchers.any());

    binder.bindInterceptor(Matchers.any(), matcher,
        new WidgetInterceptor());
}

这是告诉Guice 匹配任何内容的正确方法吗?或者我使用API​​错了吗?

提前致谢!

1 个答案:

答案 0 :(得分:2)

这不会更简单吗?:

public void configure(Binder binder) {

    if(widgetInterceptionEnabled){
        binder.bindInterceptor(Matchers.any(), Matchers.any(),
            new WidgetInterceptor());
    }

}
相关问题