我目前有一个使用以下绑定的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错了吗?
提前致谢!
答案 0 :(得分:2)
这不会更简单吗?:
public void configure(Binder binder) {
if(widgetInterceptionEnabled){
binder.bindInterceptor(Matchers.any(), Matchers.any(),
new WidgetInterceptor());
}
}