【问题标题】:Turning off matching in Google Guice在 Google Guice 中关闭匹配
【发布时间】:2023-04-04 11:35:01
【问题描述】:

我目前有一个使用以下绑定的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 有误?

提前致谢!

【问题讨论】:

    标签: java dependency-injection aop guice


    【解决方案1】:

    这不是更简单吗?:

    public void configure(Binder binder) {
    
        if(widgetInterceptionEnabled){
            binder.bindInterceptor(Matchers.any(), Matchers.any(),
                new WidgetInterceptor());
        }
    
    }
    

    【讨论】:

    • 是的!有一个全新的视角总是很高兴!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-13
    • 2011-08-18
    • 2014-04-21
    • 2019-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多