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

时间:2015-09-18 19:47:25

标签: 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());
    }
}

这是我的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);
    }
}

这是我的例外:

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.

我的理解是我只需要使用@Inject注释构造函数,如果我将通过Injector#getInstance(...)方法直接调用它们。由于我使用ApplicationMain执行此操作,其中包含对Whistlefeather的引用,其中包含对MyMapper的引用,因此我认为不必对MyMapperImpl构造函数进行注释

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

1 个答案:

答案 0 :(得分:5)

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

请考虑以下代码:

public interface Something { }
public class SomethingImpl implements Something {
  private final String data;

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

  public SomethingImpl(Integer data) {
    this.data = data.toString();
  }
}
public class AnotherClass {
  private final Something something;

  @Inject
  public AnotherClass(Something something) {
    this.something = something;
  }
}
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);
  }
}

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

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