【问题标题】:Using @Assisted inject with multiple params of same Type (@Named params)使用具有多个相同类型参数的@Assisted 注入(@Named 参数)
【发布时间】:2014-06-26 13:25:19
【问题描述】:

我的问题归结为将@Assisted 与工厂的两个字符串参数一起使用。问题是因为Guice把type作为参数的识别机制,所以两个参数是一样的,我配置错误。

一些代码:

public class FilePathSolicitingDialog {

    //... some fields

    public static interface Factory {
        public FilePathSolicitingDialog make(Path existingPath,
                                             String allowedFileExtension,
                                             String dialogTitle);
    }

    @Inject
    public FilePathSolicitingDialog(EventBus eventBus,
                                    SelectPathAndSetTextListener.Factory listenerFactory,
                                    FilePathDialogView view,
                                    @Assisted Path existingPath,
                                    @Assisted String allowedFileExtension,
                                    @Assisted String dialogTitle) {
        //... typical ctor, this.thing = thing
    }

    // ... methods
}

问题在于双字符串参数。

我尝试使用单独的 @Named("as proper") 注释标记每个字符串,但这只会导致更多配置错误。从那些错误的声音来看,他们不想在工厂类上绑定注释,所以我没有尝试自定义绑定注释。

简单而嘈杂的解决方案是创建一个简单的参数类来包含这三个辅助值,然后简单地注入:

    public static class Config{
        private final Path existingPath;
        private final String allowedFileExtension;
        private final String dialogTitle;

        public Config(Path existingPath, String allowedFileExtension, String dialogTitle){
            this.existingPath = existingPath;
            this.allowedFileExtension = allowedFileExtension;
            this.dialogTitle = dialogTitle;
        }
    }

    public static interface Factory {
        public FilePathSolicitingDialogController make(Config config);
    }

    @Inject
    public FilePathSolicitingDialogController(EventBus eventBus,
                                              SelectPathAndSetTextListener.Factory listenerFactory,
                                              FilePathDialogView view,
                                              @Assisted Config config) {
        //reasonably standard ctor, some this.thing = thing
        // other this.thing = config.thing
    }
}

这很有效,而且很可能没有错误,但很吵。摆脱嵌套静态类的某种方法会很好。

感谢您的帮助!

【问题讨论】:

    标签: java dependency-injection guice assisted-inject


    【解决方案1】:

    看看this documentation(以前是here):

    区分参数类型

    工厂方法的参数类型必须不同。使用 多个相同类型的参数,使用一个命名的@Assisted注解 消除参数的歧义。名称必须应用于 工厂方法的参数:

    public interface PaymentFactory {
       Payment create(
           @Assisted("startDate") Date startDate,
           @Assisted("dueDate") Date dueDate,
           Money amount);
     }
    

    ...以及具体类型的构造函数参数:

    public class RealPayment implements Payment {
       @Inject
       public RealPayment(
          CreditService creditService,
          AuthService authService,
          @Assisted("startDate") Date startDate,
          @Assisted("dueDate") Date dueDate,
          @Assisted Money amount) {
         ...
       }
     }
    

    【讨论】:

    • 谢谢。在 GoogleGuice v4 上成功运行。 (此答案自 2018 年 4 月 24 日起有效)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-14
    • 2016-08-10
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多