Created
October 4, 2017 09:51
-
-
Save adrienlauer/636220b61db509b38f7be0975994dd1e to your computer and use it in GitHub Desktop.
Retrieve instances from SeedStack in any external Guice module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Install | |
public class BridgeModule extends AbstractModule { | |
public void configure() { | |
requestStaticInjection(SeedStackProvider.class); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ExternalModule extends AbstractModule { | |
public void configure() { | |
Key<Repository<Customer, CustomerId>> myRepoKey = Key.get( | |
new TypeLiteral<Repository<Customer, CustomerId>>() {}, | |
Names.named("someName") | |
); | |
bind(myRepoKey).toProvider(new SeedStackProvider<>(myRepoKey)); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SeedStackProvider<T> implements Provider<T> { | |
@Inject | |
private static Injector injector; | |
private final Key<T> key; | |
public SeedStackProvider(Key<T> key) { | |
this.key = key; | |
} | |
public T get() { | |
return injector.getInstance(key); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment