本文介绍了对多个具有相同类型的参数(@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
}
}

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



感谢您的帮助!

解决方案

看看(以前):


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):

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

08-07 02:33