注入仿制药与Guice [英] Injecting generics with Guice
问题描述
import com.google.inject.Inject;
public class Console< T> {
private final StringOutput< T>出;
@Inject
public Console(StringOutput< T> out){
this.out = out;
}
public void print(T t){
System.out.println(out.converter(t));
}
}
public class StringOutput< T> {
public String converter(T t){
return t.toString();
}
}
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.TypeLiteral;
public class MyModule extends AbstractModule {
@Override
protected void configure(){
bind(StringOutput.class);
bind(Console.class);
}
public static void main(String [] args){
注入器注入= Guice.createInjector(new MyModule());
StringOutput<整数> out = injector.getInstance(StringOutput.class);
System.out.println(out.converter(12));
控制台< Double> cons = injector.getInstance(Console.class);
cons.print(123.0);
}
}
当我运行这个例子,所有我得到的是:
线程中的异常maincom.google.inject.CreationException:Guice创建错误:
1)playground.StringOutput< T>不能用作钥匙;没有完全指定。
在playground.MyModule.configure(MyModule.java:15)
1错误
在com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:354)
com.google.inject.InjectorBuilder.initializeStatically(InjectorBuilder.java:152)
com.google.inject.InjectorBuilder.build(InjectorBuilder.java:105)
在com.google .inject.Guice.createInjector(Guice.java:92)
我尝试寻找错误消息,但没有找到任何有用的提示。进一步在Guice FAQ上,我绊倒了一个关于如何注射仿制药的问题。我试图在 configure
方法中添加以下绑定:
bind (new TypeLiteral< StringOutput< Double>>(){})toInstance(new StringOutput< Double>());
但没有成功(同样的错误信息)。
有人可以向我解释错误信息,并提供一些提示?谢谢。
我认为你看到的具体问题可能是因为 bind .class)
语句。它也应该使用 TypeLiteral
。或者,你可以绑定这两个,JIT绑定将会为你照顾,因为这里涉及的所有类型都是具体的类。
此外,你应该检索控制台
与:
控制台< Double> cons =
inject.getInstance(Key.get(new TypeLiteral< Console< Double>>(){}));
编辑:你不需要绑定到一个实例,只因为您正在使用 TypeLiteral
。你可以这样做:
bind(new TypeLiteral< Console< Double>>(){});
当然,就像上面所说的,你可以跳过这个例子,然后检索<$ c基于 TypeLiteral
和绑定的键
从注入器中获取$ c>控制台将是隐含的。
I am trying to migrate a small project, replacing some factories with Guice (it is my first Guice trial). However, I am stuck when trying to inject generics. I managed to extract a small toy example with two classes and a module:
import com.google.inject.Inject;
public class Console<T> {
private final StringOutput<T> out;
@Inject
public Console(StringOutput<T> out) {
this.out = out;
}
public void print(T t) {
System.out.println(out.converter(t));
}
}
public class StringOutput<T> {
public String converter(T t) {
return t.toString();
}
}
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.TypeLiteral;
public class MyModule extends AbstractModule {
@Override
protected void configure() {
bind(StringOutput.class);
bind(Console.class);
}
public static void main(String[] args) {
Injector injector = Guice.createInjector( new MyModule() );
StringOutput<Integer> out = injector.getInstance(StringOutput.class);
System.out.println( out.converter(12) );
Console<Double> cons = injector.getInstance(Console.class);
cons.print(123.0);
}
}
When I run this example, all I got is:
Exception in thread "main" com.google.inject.CreationException: Guice creation errors:
1) playground.StringOutput<T> cannot be used as a key; It is not fully specified.
at playground.MyModule.configure(MyModule.java:15)
1 error
at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:354)
at com.google.inject.InjectorBuilder.initializeStatically(InjectorBuilder.java:152)
at com.google.inject.InjectorBuilder.build(InjectorBuilder.java:105)
at com.google.inject.Guice.createInjector(Guice.java:92)
I tried looking for the error message, but without finding any useful hints. Further on the Guice FAQ I stumble upon a question about how to inject generics. I tried to add the following binding in the configure
method:
bind(new TypeLiteral<StringOutput<Double>>() {}).toInstance(new StringOutput<Double>());
But without success (same error message).
Can someone explain me the error message and provide me some tips ? Thanks.
I think the specific issue you're seeing is probably because of the bind(Console.class)
statement. It should use a TypeLiteral
as well. Or, you could just bind neither of those and JIT bindings will take care of it for you since both of the types involved here are concrete classes.
Additionally, you should retrieve the Console
with:
Console<Double> cons =
injector.getInstance(Key.get(new TypeLiteral<Console<Double>>(){}));
Edit: You don't need to bind to an instance just because you're using a TypeLiteral
. You can just do:
bind(new TypeLiteral<Console<Double>>(){});
Of course, like I said above you could just skip that in this case and retrieve the Console
from the injector using a Key
based on the TypeLiteral
and the binding would be implicit.
这篇关于注入仿制药与Guice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!