Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 1.56 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Does GIN support anything like child injectors?
  2. GIN Modules:
  3.   Core - shared
  4.   MetadataCache - one per sub-application
  5.   UserProvider - one per sub-application
  6.        
  7. public class Thing {
  8.     private static int thingCount = 0;
  9.     private int index;
  10.  
  11.     public Thing() {
  12.         index = thingCount++;
  13.     }
  14.  
  15.     public int getIndex() {
  16.         return index;
  17.     }
  18. }
  19.  
  20. public class SharedThing extends Thing {
  21. }
  22.  
  23. public class ThingOwner1 {
  24.     private Thing thing;
  25.     private SharedThing shared;
  26.  
  27.     @Inject
  28.     public ThingOwner1(Thing thing, SharedThing shared) {
  29.         this.thing = thing;
  30.         this.shared = shared;
  31.     }
  32.  
  33.     @Override
  34.     public String toString() {
  35.         return "" + this.thing.getIndex() + ":" + this.shared.getIndex();
  36.     }
  37. }
  38.  
  39. public class ThingOwner2 extends ThingOwner1 {
  40.     @Inject
  41.     public ThingOwner2(Thing thing, SharedThing shared) {
  42.         super(thing, shared);
  43.     }
  44. }
  45.        
  46. public class MyPrivateModule1 extends PrivateGinModule {
  47.   @Override
  48.   protected void configure() {
  49.     bind(Thing.class).in(Singleton.class);
  50.     bind(ThingOwner1.class).in(Singleton.class);
  51.   }
  52. }
  53.        
  54. public class MySharedModule extends AbstractGinModule {
  55.     @Override
  56.     protected void configure() {
  57.         bind(SharedThing.class).in(Singleton.class);
  58.     }
  59. }
  60.        
  61. @GinModules({MyPrivateModule1.class, MyPrivateModule2.class, MySharedModule.class})
  62. public interface MyGinjector extends Ginjector {
  63.     ThingOwner1 getOwner1();
  64.     ThingOwner2 getOwner2();
  65. }
  66.        
  67. System.out.println(injector.getOwner1().toString());
  68. System.out.println(injector.getOwner2().toString());