GWT-GIN Multiple Implementations?

I have the following code

public class AppGinModule extends AbstractGinModule{ @Override protected void configure() { bind(ContactListView.class).to(ContactListViewImpl.class); bind(ContactDetailView.class).to(ContactDetailViewImpl.class); } } @GinModules(AppGinModule.class) public interface AppInjector extends Ginjector{ ContactDetailView getContactDetailView(); ContactListView getContactListView(); } 

At my entry point

 AppInjector appInjector = GWT.create(AppGinModule.class); appInjector.getContactDetailsView(); 

Here ContactDetailView always associated with ContactsDetailViewImpl . But I want this to be related to ContactDetailViewImplX under some conditions.

How can i do this? Help me.

+3
source share
1 answer

You cannot declaratively tell Jin to sometimes introduce one implementation and another at another time. You can do this using the Provider or @Provides method .

Provider Example:

 public class MyProvider implements Provider<MyThing> { private final UserInfo userInfo; private final ThingFactory thingFactory; @Inject public MyProvider(UserInfo userInfo, ThingFactory thingFactory) { this.userInfo = userInfo; this.thingFactory = thingFactory; } public MyThing get() { //Return a different implementation for different users return thingFactory.getThingFor(userInfo); } } public class MyModule extends AbstractGinModule { @Override protected void configure() { //other bindings here... bind(MyThing.class).toProvider(MyProvider.class); } } 

@Provides Example:

 public class MyModule extends AbstractGinModule { @Override protected void configure() { //other bindings here... } @Provides MyThing getMyThing(UserInfo userInfo, ThingFactory thingFactory) { //Return a different implementation for different users return thingFactory.getThingFor(userInfo); } } 
+7
source

All Articles