【发布时间】:2019-02-11 06:21:07
【问题描述】:
我正在尝试在 Guice 中进行辅助注射。
这是我的实现。
public interface Dao<T> {
T get(String id);
}
public class DaoImpl<T> implements Dao<T> {
private final Class<T> clazz;
DaoImpl(@Assisted final Class<T> clazz) {
this.clazz = clazz;
}
@Override
public T get() {
//Some impl code
return T;
}
}
工厂界面。
public interface DaoFactory {
<T> Dao<T> getDao(Class<T> clazz);
}
Guice 模块:
public class DaoModule extends AbstractModule {
@Override
protected void configure() {
install(new FactoryModuleBuilder()
.implement(new TypeLiteral<Dao<? extends Entity>>() {},
new TypeLiteral<DaoImpl<? extends Entity>>() {})
.build(DaoFactory.class));
}
}
我收到错误:“DaoFactory 不能用作键;它没有完全指定”。
我应该如何配置 FactoryModuleBuilder?
我的目标是在运行时使用 DaoFactory 获得一个类型化的 Dao 实例
【问题讨论】: