为什么Guice不能绑定中间依赖?

sme*_*eeb 3 java dependency-injection guice

这是我的代码:

// Groovy
interface MyMapper {
    Buzz toBuzz(Fizz fizz);
}

class MyMapperImpl implements MyMapper {
    @Named("SIMPLE_FOOBAR")
    Foobar foobar;

    MyMapperImpl(Foobar foobar) {
        super();
        this.foobar = foobar;
    }

    @Override
    Buzz toBuzz(Fizz fizz) {
        // ...etc.
    }
}

class Whistlefeather {
    MyMapper mapper;

    Whistlefeather(MyMapper mapper) {
        super();

        this.mapper = mapper;
    }

    void doSomething(Fink fink) {
        Fizz fizz = getSomehow(fink);
        Buzz buzz = mapper.toBuzz(fizz);

        // Do something with 'buzz'...
    }
}

class ApplicationMain {
    Whistlefeather whistlefeather;

    @Inject
    ApplicationMain(Whistlefeather whistlefeather) {
        super();

        this.whistlefeather = whistlefeather;
    }

    static void main(String[] args) {
        Injector injector = Guice.createInjector(new ApplicationModule());
        ApplicationMain appMain = injector.getInstance(ApplicationMain);
        appMain.run();
    }

    void run() {
        whistlefeather.doSomething(new Fink());
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的Guice模块:

class ApplicationModule extends AbstractModule {
    @Override
    protected void configure() {
        // I have to name the Foobars because in reality there will be
        // *many* of them, each configured slightly different.
        bind(Foobar.class).annotatedWith(Names.named("SIMPLE_FOOBAR"))
            .toInstance(new Foobar(true, true, false, 103, "yee haw"));

        bind(MyMapper.class).to(MyMapperImpl);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的例外:

Could not find a suitable constructor in com.me.myapp.MyMapperImpl.
Classes must have either one (and only one) constructor annotated
with @Inject or a zero-argument constructor that is not private.
Run Code Online (Sandbox Code Playgroud)

我的理解是,@Inject如果我通过该Injector#getInstance(...)方法直接调用它,我只需要注释构造函数.由于我这样做ApplicationMain,包含一个引用Whistlefeather,其中包含一个引用MyMapper,我不认为我将不得不注释MyMapperImpl构造函数.

关于我在哪里出错的任何想法?

dur*_*597 5

为了让Guice创建任何对象,它必须知道要使用哪个构造函数.在Object Graph中一直如此.

请考虑以下代码:

public interface Something { }
Run Code Online (Sandbox Code Playgroud)
public class SomethingImpl implements Something {
  private final String data;

  public SomethingImpl(String data) {
    this.data = data;
  }

  public SomethingImpl(Integer data) {
    this.data = data.toString();
  }
}
Run Code Online (Sandbox Code Playgroud)
public class AnotherClass {
  private final Something something;

  @Inject
  public AnotherClass(Something something) {
    this.something = something;
  }
}
Run Code Online (Sandbox Code Playgroud)
public class MyModule extends AbstractModule {
  @Override
  protected void configure() { 
    bind(Something.class).to(SomethingImpl.class);
    bind(String.class).toInstance("Hello!");
    bind(Integer.class).toInstance(50);
  }
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,Guice如何知道要使用哪个构造函数SomethingImpl如果你是Guice的作者,你会怎么写呢?

显然,无法回答,因为这是不可能的.必须有某种机制告诉Guice使用哪个构造函数,无论它是否被调用Injector.getInstance(); 这就是为什么你必须注释至少一个构造函数.如果指定了一个,Guice将默认使用无参数构造函数,但如果没有,则Guice不知道该怎么做.