[Guice 4.0]
我希望有一个泛型类的接口,并在使用Guice的依赖注入中使用它。 对于下面列出的代码,我收到以下错误:
public class Class1<T1 extends Number, T2 extends Number>
implements InterfClass1<T1, T2> {
public static final String ANNOT1 = "ANNOT1";
public static final String ANNOT2 = "ANNOT2";
private T1 t1;
private T2 t2;
// for the factory
@AssistedInject
public Class1(
@Assisted(Class1.ANNOT1) T1 t1,
@Assisted(Class1.ANNOT2) T2 t2
) {
this.t1 = t1;
this.t2 = t2;
}
public T1 getT1() {
return t1;
}
public T2 getT2() {
return t2;
}
}
public class Module extends AbstractModule {
@Override
protected void configure() {
bind(new TypeLiteral<InterfClass1<Integer, Integer>>(){})
.to(new TypeLiteral<Class1<Integer, Integer>>(){});
}
public static void main(String[] args) {
Injector inj = Guice.createInjector(new Module());
}
}
-
public interface Class1Factory<T1 extends Number, T2 extends Number> {
public Class1<T1, T2> createClass1(
@Assisted(Class1.ANNOT1) T1 t1,
@Assisted(Class1.ANNOT2) T2 t2
);
}
public class Module extends AbstractModule {
@Override
protected void configure() {
install(new FactoryModuleBuilder()
.build(new TypeLiteral<Class1Factory<Integer, Double>>(){}));
}
public static void main(String[] args) {
Injector inj = Guice.createInjector(new Module());
Class1Factory f = inj.getInstance(Key.get(new TypeLiteral<Class1Factory<Integer, Double>>(){}));
f.createClass1(10, 11.0);
}
}
导致此错误的原因是什么?
让我们添加工厂并修改模块(从模块中删除InterfClass1接口):
public class Module extends AbstractModule {
@Override
protected void configure() {
bind(new TypeLiteral<InterfClass1<Integer, Double>>(){})
.to(new TypeLiteral<Class1<Integer, Double>>(){});
install(new FactoryModuleBuilder()
.build(new TypeLiteral<Class1Factory<Integer, Double>>(){}));
}
public static void main(String[] args) {
Injector inj = Guice.createInjector(new Module());
Class1Factory f = inj.getInstance(Key.get(new TypeLiteral<Class1Factory<Integer, Double>>(){}));
f.createClass1(10, 11.0);
}
}
一切正常!
现在让我们加入界面:
1) Could not find a suitable constructor in com.ulmon.fsqtransit.guicetest.Class1. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
at com.ulmon.fsqtransit.guicetest.Class1.class(Class1.java:15)
at com.ulmon.fsqtransit.guicetest.Module.configure(Module.java:14)
我们得到:
public interface Class1Factory<T1 extends Number, T2 extends Number> {
public InterfClass1<T1, T2> createClass1(
@Assisted(Class1.ANNOT1) T1 t1,
@Assisted(Class1.ANNOT2) T2 t2
);
}
好的,不管错误如何,让我们改变工厂:
1) Could not find a suitable constructor in com.ulmon.fsqtransit.guicetest.Class1. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
at com.ulmon.fsqtransit.guicetest.Class1.class(Class1.java:15)
at com.ulmon.fsqtransit.guicetest.Module.configure(Module.java:14)
2) com.ulmon.fsqtransit.guicetest.InterfClass1<java.lang.Integer, java.lang.Double> is an interface, not a concrete class. Unable to create AssistedInject factory.
while locating com.ulmon.fsqtransit.guicetest.InterfClass1<java.lang.Integer, java.lang.Double>
at com.ulmon.fsqtransit.guicetest.Class1Factory.createClass1(Class1Factory.java:1)
我们得到:
public class Module extends AbstractModule {
@Override
protected void configure() {
install(new FactoryModuleBuilder()
.implement(new TypeLiteral<InterfClass1<Integer, Double>>(){}
, new TypeLiteral<Class1<Integer, Double>>(){})
.build(new TypeLiteral<Class1Factory<Integer, Double>>(){}));
}
public static void main(String[] args) {
Injector inj = Guice.createInjector(new Module());
Class1Factory f = inj.getInstance(Key.get(new TypeLiteral<Class1Factory<Integer, Double>>(){}));
f.createClass1(10, 11.0);
}
}
但是,如果我删除模块中接口和类之间的关系并将此关系添加到工厂的规范中,我就不会收到错误:
{{>yield "aside"}}
一切正常!
为什么Guice有这种奇怪的行为?为什么我不能将接口与实现类联系起来而不考虑工厂?
答案 0 :(得分:1)
我无法确切地知道你想要做什么,但它看起来像你实际上并没有尝试创建一个AssistedInject工厂,因为你还没有为工厂创建接口。在这种情况下,您根本不需要使用@Assisted
参数。你的构造函数应该是这样的(没有对类进行其他更改):
@Inject
public Class1(
@Named(Class1.ANNOT1) T1 t1,
@Named(Class1.ANNOT2) T2 t2
) {
然后,在模块中指定绑定。你必须明确告诉Guice要为参数注入什么,以及你指定的注释:
@Override
protected void configure() {
bind(new TypeLiteral<InterfClass1<Integer, Integer>>(){})
.to(new TypeLiteral<Class1<Integer, Integer>>(){});
bind(Integer.class)
.annotatedWith(Names.named(Class1.ANNOT1))
.toInstance(5);
bind(Integer.class)
.annotatedWith(Names.named(Class1.ANNOT2))
.toInstance(15);
}
然后,我们可以像这样运行它:
public static void main(String[] args) {
Injector inj = Guice.createInjector(new Module());
InterfClass1<Integer, Integer> interf =
inj.getInstance(Key.get(new TypeLiteral<InterfClass1<Integer, Integer>>(){}));
System.out.println(interf.getClass());
System.out.println("T1: " + interf.getT1() + " T2: " + interf.getT2());
}
正如您在运行它时所看到的,它将注入Class
实现,并指定了两个参数:
class guice.Class1
T1: 5 T2: 15
如果您 尝试使用AssistedInject构建Class1
个对象的工厂,由于您指定了generit类型参数的方式,这要困难得多。不幸的是,你不能inject a generic factory using AssistedInject in the normal way,因为Class1
实施需要T1 extends Number
,所以这会变得更加困难,但您的界面可以是您想要的任何T1
。这意味着您的工厂必须将其收到的输入参数的类型限制为Number
的子类;换句话说,你的工厂不能只创建它想要的任何InterfClass1
,它只能用正确的输入参数创建实例。
希望答案的前半部分是你要做的。如果不是,我们需要有关您的用例的更多信息才能回答这个问题。