Guice 配置错误:未绑定任何实现

2022-09-03 02:24:30

我试图让DI与Guice一起工作,做(在我看来)完全符合手册中的内容。

我无法解释这个问题,因为我并不真正理解它 - 一切似乎都非常合乎逻辑,应该有效。但事实并非如此。因此,我只能附加代码和堆栈跟踪:

public class Runner {

    public static void main(String[] args) {
        Injector injector = Guice.createInjector(new TestModule());
        //next line throws the exception
        JMeterComponent jMeterComponent = 
             injector.getInstance(JMeterComponent.class);
        ....
    }
}

如您所见,我正在尝试实例化类的对象。它的构造函数(正如您稍后将看到的)接受3个参数:所有这些参数也应该由IoC实例化并注入。JMeterComponent

以下是这3个参数的配置:TestModule

public class TestModule extends AbstractModule {

    @Override
    protected void configure() {
        bind(Callable.class).annotatedWith(Names.named("JMeter"))
                  .to(JMeterTask.class);      
        bind(Processor.class).annotatedWith(Names.named("JMeter"))
                  .to(JMeterResultsProcessor.class);
        bind(Renderer.class).annotatedWith(Names.named("JMeter"))
                  .to(JMeterResultsWikiRenderer.class);
    }
}

现在,让我们看一下那些具体的实现 - 和(为了简单起见,它们都有假体):JMeterTaskJMeterResultsProcessorJMeterResultsWikiRenderer

public class JMeterTask implements Callable<JMeterRawResults> {

    public JMeterRawResults call() throws Exception {
        return new JMeterRawResults();
    }
}

public class JMeterResultsProcessor implements 
                   Processor<JMeterRawResults, JMeterResults> {

    public JMeterResults process(JMeterRawResults raw) {
        return new JMeterResults();
    }
}

public class JMeterResultsWikiRenderer implements Renderer<JMeterResults> {

    public Map<String, String> render(JMeterResults jMeterResults) {
        Map<String, String> results = Maps.newHashMap();
        ...
        return results;
    }
}

现在让我们看一下类,哪个实例的构造是这里整个DI相关内容的目标:JMeterComponent

public class JMeterComponent extends AbstractComponent<String, String> {

    @Inject
    public JMeterComponent(@Named("JMeter") Callable<String> task, 
                           @Named("JMeter")Processor<String, String> processor, 
                           @Named("JMeter")Renderer<String> renderer) {
        super(task, processor, renderer);
    }
}

下面是堆栈跟踪:

Exception in thread "main" com.google.inject.ConfigurationException: Guice configuration errors:

1) No implementation for stat.domain.Processor<java.lang.String, java.lang.String> annotated with @com.google.inject.name.Named(value=JMeter) was bound.
  while locating stat.domain.Processor<java.lang.String, java.lang.String> annotated with @com.google.inject.name.Named(value=JMeter)
    for parameter 1 at stat.components.jmeter.JMeterComponent.<init>(JMeterComponent.java:18)
  while locating cstat.components.jmeter.JMeterComponent

2) No implementation for stat.domain.Renderer<java.lang.String> annotated with @com.google.inject.name.Named(value=JMeter) was bound.
  while locating stat.domain.Renderer<java.lang.String> annotated with @com.google.inject.name.Named(value=JMeter)
    for parameter 2 at stat.components.jmeter.JMeterComponent.<init>(JMeterComponent.java:18)
  while locating stat.components.jmeter.JMeterComponent

3) No implementation for java.util.concurrent.Callable<java.lang.String> annotated with @com.google.inject.name.Named(value=JMeter) was bound.
  while locating java.util.concurrent.Callable<java.lang.String> annotated with @com.google.inject.name.Named(value=JMeter)
    for parameter 0 at stat.components.jmeter.JMeterComponent.<init>(JMeterComponent.java:18)
  while locating stat.components.jmeter.JMeterComponent

一些额外的事实:

  1. 我使用 guice-2.0 (带标签)featured
  2. 在代码中的任何其他类中,不再有任何来自包的任何注释com.google.inject
  3. 接口并放置在一个模块中,它们的-实现(和其他)和类放置在另一个模块中。ProcessorRendererjmeterJMeterResultsProcessorJMeterComponent

这几乎是所有关于它要说的。

很抱歉这么长的帖子,并感谢您的耐心阅读到最后。

关于为什么会发生错误以及如何解决它的任何想法?


答案 1

我在这里看到几个问题。

首先,它们是不同的。如果你想在 Guice 中注入一个(或 等),你必须将某些东西绑定到 ,而不是 。CallableCallable<String>Callable<String>Processor<String, String>Callable<String>Callable

其次,您正在绑定到哪些实现,但您正在注入 的构造函数(和 的相同交易)。我将假设应该已经注入了等。CallableJMeterTaskCallable<JMeterRawResults>Callable<String>JMeterComponentProcessorRendererJMeterComponentCallable<JMeterRawResults>

无论如何,您需要做的是使用以下代码进行泛型绑定:TypeLiteral

bind(new TypeLiteral<Callable<JMeterRawResults>>(){})
    .annotatedWith(Names.named("JMeter"))
    .to(JMeterTask.class);

答案 2

推荐