我有一个界面
public interface ConfFileLoader {
public Map<String, Object> getResultMap() throws SecurityException, IOException;
}
以及将其实现为的类
public class ConfYamlLoader implements ConfFileLoader{
@Override
public Map<String, Object> getResultMap() throws SecurityException, IOException {
}
}
并且有一个注入器类
public class AppInjector extends AbstractModule {
@Override
public void configure() {
bind(ConfFileLoader.class).to(ConfYamlLoader.class);
}
}
现在的问题是我有一个类
public class TI {
@Inject
private ConfFileLoader confFileLoader;
public void test(){
System.out.println("yeah tesrintY>>>>>>>>>>>>>>>>>>>"+confFileLoader);
}
}
它给出了例外
Exception in thread "main" com.google.inject.ConfigurationException: Guice configuration errors:
1) No implementation for com.exzeo.conf.ConfFileLoader was bound.
while locating com.exzeo.conf.ConfFileLoader
for field at com.exzeo.automate.HCPCI.TI.confFileLoader(TI.java:10)
while locating com.exzeo.automate.HCPCI.TI
但是当我做@ConfYamlLoader 时,它可以正常工作
public class TI {
@Inject
private ConfYamlLoader confFileLoader;
public void test(){
System.out.println("yeah tesrintY>>>>>>>>>>>>>>>>>>>"+confFileLoader);
}
}
上面的这个类工作正常
因此,经过调查,我发现当我在 ConfFileLoader 上添加 @implemetedby 时,它可以正常工作
@ImplementedBy(ConfYamlLoader.class)
public interface ConfFileLoader {
public Map<String, Object> getResultMap() throws SecurityException, IOException;
}
如果我已经在绑定模块中绑定它,我无法理解为什么我需要做 @ImplementedBy 的这种行为。据我了解,@implementedby 等同于 bind()。
让我知道我是否遗漏了什么并且我使用的是 guice 版本 3