对多个具有相同类型的参数(@Named参数)使用@Assisted注入 [英] Using @Assisted inject with multiple params of same Type (@Named params)

查看:347
本文介绍了对多个具有相同类型的参数(@Named参数)使用@Assisted注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题归结为对工厂使用两个字符串参数的@Assisted。问题在于,因为Guice将类型视为参数的识别机制,所以两个参数相同,并且出现配置错误。



某些代码:

 公共类FilePathSolicitingDialog {

// ...某些字段

公共静态接口工厂{
public FilePathSolicitingDialog make(路径现有路径,
允许的字符串字符串FileExtension,
的对话框字符串标题);
}

@Inject
public FilePathSolicitingDialog(EventBus eventBus,
SelectPathAndSetTextListener.Factory listenerFactory,
FilePathDialogView视图,
@Assisted Path existingPath,
@Assisted String allowedFileExtension,
@Assisted String dialogTitle){
// ...典型的ctor,this.thing =事物
}

/ / ...方法
}

问题出在双字符串参数中。 / p>

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



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

  public static class Config {
private最终路径existingPath;
私人最终String allowedFileExtension;
私人最终字串对话框标题;

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

公共静态接口Factory {
public FilePathSolicitingDialogController make(Config config);
}

@Inject
public FilePathSolicitingDialogController(EventBus eventBus,
SelectPathAndSetTextListener.Factory listenerFactory,
FilePathDialogView视图,
@Assisted Config配置) {
//合理的标准ctor,一些this.thing = something
//其他this.thing = config.thing
}
}

此方法有效,可能几乎没有错误,但是很吵。摆脱嵌套的静态类的某种方法会很好。



感谢您的帮助!

解决方案

看看本文档(以前此处):


使参数类型不同



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

 公共接口PaymentFactory {
Payment create(
@Assisted( startDate)日期startDate,
@Assisted( dueDate)日期DueDate,
金额);
}

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

 公共类RealPayment实现付款{
@Inject
public RealPayment(
CreditService creditService,
AuthService authService,
@Assisted( startDate)日期startDate,
@Assisted( dueDate)日期DueDate,
@ Assisted Money金额){
...
}
}



my problem boils down to using @Assisted with two string arguments to the factory. The problem is that because Guice treats type as the identification mechanism for parameters, both parameters are the same, and I get a configuration error.

Some Code:

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
}

The problem lies in the double-strings parameters.

I've tried tagging each string with separate @Named("as appropriate") annotations, but that just leads to more configuration errors. From the sound of those errors they don't want binding annotations on the factory class, so I have not tried custom binding annotations.

The simple and noisy solution is to create a simple argument class to contain those three assisted values, and simply inject that:

    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
    }
}

This works and is likely to be fairly bug-free but is noisy. Some way to get rid of that nested static class would be nice.

Thanks for any help!

解决方案

Have a look at this documentation (previously here):

Making parameter types distinct

The types of the factory method's parameters must be distinct. To use multiple parameters of the same type, use a named @Assisted annotation to disambiguate the parameters. The names must be applied to the factory method's parameters:

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

...and to the concrete type's constructor parameters:

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

这篇关于对多个具有相同类型的参数(@Named参数)使用@Assisted注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆