java - Google Guice - Use generic as parameter of injected field -


i want use generic parameter of class field injected, guice complains unbound key. possible inject field in test2?

example:

public static class test1<t1> {     }      public static class test2<t2> {         @inject         public test1<t2> test;     }      public static void main(string[] args) throws exception {         injector injector = jsr250.createinjector(stage.production, new testmodule());         test2<string> test = injector.getinstance(key.get(new typeliteral<test2<string>>(){}));     }      private static class testmodule extends abstractmodule {         @override         protected void configure() {             bind(new typeliteral<test1<string>>(){}).toinstance(new test1<string>());             bind(new typeliteral<test2<string>>(){}).toinstance(new test2<string>());         }     } 

well, overkill java generics. code not work without injection, cuz test1<t2> cannot match test1<string>. should consider different approach, try use annotation binding like:

public static class test2 {         @inject @named("t2")         public test1<somegenericinterface> test;     }  bind(new typeliteral<test1<somegenericinterface>>(){}).annotatedwith(names.named("t2")).toinstance(new test1<t2>()); //t2 implements somegenericinterface bind(new typeliteral<test1<somegenericinterface>>(){}).annotatedwith(names.named("t1")).toinstance(new test1<t1>()); //t1 implements somegenericinterface 

or implement specific provider provider bindings or use mapbinder


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -