Skip to content

Instantly share code, notes, and snippets.

@martint
Created September 9, 2014 22:46
Show Gist options
  • Save martint/c4e328a5cc9e7f67db72 to your computer and use it in GitHub Desktop.
Save martint/c4e328a5cc9e7f67db72 to your computer and use it in GitHub Desktop.
import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Scopes;
import com.google.inject.multibindings.MapBinder;
import javax.inject.Inject;
import java.util.Map;
public class Main
{
public static void main(String[] args)
{
Injector injector = Guice.createInjector(new Module()
{
@Override
public void configure(Binder binder)
{
binder.bind(Stats1.class).in(Scopes.SINGLETON);
binder.bind(Stats2.class).in(Scopes.SINGLETON);
binder.bind(Stats3.class).in(Scopes.SINGLETON);
binder.bind(Updater1.class).in(Scopes.SINGLETON);
binder.bind(Updater2.class).in(Scopes.SINGLETON);
binder.bind(Exporter.class).in(Scopes.SINGLETON);
MapBinder<String, Stats> mapBinder = MapBinder.newMapBinder(binder, String.class, Stats.class);
mapBinder.addBinding("stats1").to(Stats1.class);
mapBinder.addBinding("stats2").to(Stats2.class);
mapBinder.addBinding("stats3").to(Stats3.class);
}
});
Exporter exporter = injector.getInstance(Exporter.class);
}
public static interface Stats
{
}
public static class Stats1 implements Stats {}
public static class Stats2 implements Stats {}
public static class Stats3 implements Stats {}
public static class Updater1
{
@Inject
public Updater1(Stats1 stats) {}
}
public static class Updater2
{
@Inject
public Updater2(Stats2 stats2, Stats3 stats3) {}
}
public static class Exporter
{
@Inject
public Exporter(Map<String, Stats> stats)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment