首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么Guice不能绑定到中间依赖项呢?

问为什么Guice不能绑定到中间依赖项呢?
EN

Stack Overflow用户
提问于 2015-09-18 19:47:24
回答 1查看 49关注 0票数 3

这是我的密码:

代码语言:javascript
复制
// 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模块:

代码语言:javascript
复制
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);
    }
}

我的例外是:

代码语言:javascript
复制
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构造函数。

你知道我在哪里出问题了吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-18 22:28:41

为了让Guice创建任何对象,它必须知道要使用哪个构造函数。这在整个对象图中都是正确的。

考虑以下代码:

代码语言:javascript
复制
public interface Something { }
代码语言:javascript
复制
public class SomethingImpl implements Something {
  private final String data;

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

  public SomethingImpl(Integer data) {
    this.data = data.toString();
  }
}
代码语言:javascript
复制
public class AnotherClass {
  private final Something something;

  @Inject
  public AnotherClass(Something something) {
    this.something = something;
  }
}
代码语言:javascript
复制
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不知道该做什么。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32659933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档