如何将Assisted Injected类绑定到接口? [英] How to bind Assisted Injected class to interface?

查看:81
本文介绍了如何将Assisted Injected类绑定到接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我遇到的问题:

Class SimpleCommand implements Executable{
private final ConfigManager config;
private String name;

@Inject    
public SimpleCommand(ConfigManager config, @Assisted String name){
  this.config = config;
  this.name = name;
  }
}

Class MyModule extends AbstractModule{
@Override
protected void configure() {
     bind(CommandFactory.class).toProvider(FactoryProvider.newFactory(CommandFactory.class, SimpleCommand.class));
     bind(Executable.class).to(SimpleCommand.class);
     }
}

当我尝试使用以下方法获取SimpleCommand实例时:

When I try to get instance of SimpleCommand using:

Guice.createInjector(new MyModule()).getInstance(CommandFactory.class).create("sample command");

我收到此错误:

1) No implementation for java.lang.String annotated with @com.google.inject.assistedinject.Assisted(value=) was bound.
  while locating java.lang.String annotated with @com.google.inject.assistedinject.Assisted(value=)
    for parameter 2 at model.Command.<init>(SimpleCommand.java:58)
  at module.MyModule.configure(MyModule.java:34)

所以我的问题是,当SimpleCommand具有辅助注入参数时,如何将SimpleCommand绑定到可执行文件?

So my problem is how can I bind SimpleCommand to Executable when SimpleCommand has Assisted Injected parameter?

这是CommandFactory及其实现:

Here is the CommandFactory and its implementation:

public interface CommandFactory{
  public Command create(String name);
}


public class GuiceCommandFactory implements CommandFactory{
  private Provider<ConfigManager> configManager ;

  @Inject
  public GuiceCommandFactory(Provider<ConfigManager> configManager){
    this.configManager = configManager;
  }

  public Command create(String cmd){
    return new Command(configManager.get(), cmd);
  }
}

推荐答案

似乎有两个问题.

一个与bind(Executable.class).to(SimpleCommand.class)一起使用.将Executable绑定到SimpleCommand意味着可以将SimpleCommand直接注入依赖于Executable的类.但是由于Guice本身无法创建SimpleCommand的实例(它需要提供给工厂的辅助参数),因此绑定无效.错误消息将@Assisted视为普通的绑定批注,并尝试在构造函数中解析与其绑定的String,但没有绑定.

One is with bind(Executable.class).to(SimpleCommand.class). Binding Executable to SimpleCommand implies that a SimpleCommand can be injected directly to classes with a dependency on Executable. But since Guice cannot create an instance of SimpleCommand itself (it needs the assisted parameter provided to the factory), the bind is invalid. The error message is treating @Assisted as a normal binding annotation, and trying to resolve a String bound to it in the constructor, but none is bound.

除此之外,似乎如果您正在使用Assisted Inject为Executable s创建工厂,则不能在没有使用某些绑定注释的情况下为其创建另一个绑定.由于您正在使用辅助注入,因此大概是想使用工厂创建多个Executable实例,因此您想要直接绑定的任何单个Executable在您的系统中都可能具有某些特定含义,对吧?

Beyond that, it seems like if you're using Assisted Inject to create a factory for Executables, you can't create another binding to it without using some binding annotation on it. Since you're using assisted inject, presumably you want to create multiple instances of Executable using your factory, so any individual Executable you wanted to bind directly would probably have some specific meaning in your system, right?

在这种情况下,您应该只使用诸如@Default之类的绑定注释和诸如此类的提供者方法:

In that case, you should just use some binding annotation like @Default and a provider method like so:

@Provides
@Default
protected Executable provideDefaultExecutable(ConfigManager configManager) {
   return new SimpleCommand(configManager, "My default command");
}

这篇关于如何将Assisted Injected类绑定到接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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