我正在尝试使用Guice工作制作DI,对我来说(在我看来)正是the manual中的内容。
我无法解释这个问题,因为我并不真正理解它 - 一切看起来都非常符合逻辑并应该有效......但事实并非如此。所以,我只能附加代码和堆栈跟踪:
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);
....
}
}
如您所见,我正在尝试实例化JMeterComponent
类的对象。它的构造函数(稍后会看到)接受3个参数:它们都应该由IoC实例化并注入。
这是TestModule
配置这3个参数:
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);
}
}
现在,让我们看看那些具体的实现 - JMeterTask
,JMeterResultsProcessor
和JMeterResultsWikiRenderer
(为简单起见,所有这些实体都有假身体):
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;
}
}
现在让我们看一下JMeterComponent
类,这个实例的构造是整个DI相关内容的目标:
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
其他一些事实:
featured
标签)com.google.inject
包中都没有任何注释Processor
和Renderer
放置在一个模块中,其jmeter
- 实现(JMeterResultsProcessor
和其他)和JMeterComponent
类放在另一个模块中模块。这几乎是关于它的所有内容。
很抱歉这么长的帖子,感谢您的耐心阅读到最后。
关于错误发生原因以及解决方法的任何想法?
答案 0 :(得分:14)
我在这里看到了几个问题。
首先,Callable
和Callable<String>
不同。如果您想在Guice中注入Callable<String>
(或Processor<String, String>
等),则必须将某些内容绑定到Callable<String>
,而不是Callable
。
其次,你将Callable
绑定到实现JMeterTask
的{{1}},但是你在Callable<JMeterRawResults>
的构造函数中注入Callable<String>
(同样的交易) JMeterComponent
和Processor
)。我将假设Renderer
应注入JMeterComponent
等。
无论如何,您需要做的是使用Callable<JMeterRawResults>
泛型绑定:
TypeLiteral