Skip to content

Instantly share code, notes, and snippets.

@mmadson
Last active December 16, 2015 17:09
Show Gist options
  • Save mmadson/5468416 to your computer and use it in GitHub Desktop.
Save mmadson/5468416 to your computer and use it in GitHub Desktop.
package app;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class Main
{
public static void main(String[] args)
{
final Injector injector = Guice.createInjector(new MyAppModule());
injector.getInstance(MyService.class).printInternals();
System.out.println("----");
injector.getInstance(MyOtherService.class).printInternals();
}
}
MyService Executor: java.util.concurrent.Executors$FinalizableDelegatedExecutorService@19632847
Red Executor: java.util.concurrent.Executors$FinalizableDelegatedExecutorService@3ce95a56
Blue Executor: java.util.concurrent.Executors$FinalizableDelegatedExecutorService@7c29e357
----
MyOtherService Executor: java.util.concurrent.Executors$FinalizableDelegatedExecutorService@19632847
Red Executor: java.util.concurrent.Executors$FinalizableDelegatedExecutorService@3ce95a56
Blue Executor: java.util.concurrent.Executors$FinalizableDelegatedExecutorService@6bffc686
package app;
import com.google.inject.Module;
import com.google.inject.Provider;
import red.RedModule;
import blue.BlueModule;
import com.google.inject.PrivateModule;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MyAppModule
extends PrivateModule
{
private static final Module appGlobalDependencies = new MyAppGlobalDependenciesModule();
private static final Provider<ExecutorService> redExecutorServiceProvider = new Provider<ExecutorService>()
{
@Override
public ExecutorService get()
{
return Executors.newSingleThreadExecutor();
}
};
private static final Provider<ExecutorService> blueExecutorServiceProvider = new Provider<ExecutorService>()
{
@Override
public ExecutorService get()
{
return Executors.newSingleThreadExecutor();
}
};
@Override
protected void configure()
{
// no unannotated / named bindings allowed here, only private module installations
// If RedModule or BlueModule expose() and bind to any Types that we want to use in our appGlobalDependencies
// we will need to wrap the module in yet another private module, and only expose the red or blue services
// that we want to use in our application
install(new RedModule(redExecutorServiceProvider));
install(new BlueModule(blueExecutorServiceProvider));
install(new MyServiceModule(appGlobalDependencies));
install(new MyOtherServiceModule(appGlobalDependencies));
expose(MyService.class);
expose(MyOtherService.class);
}
}
package app;
import com.google.inject.PrivateModule;
import com.google.inject.Provider;
import java.util.concurrent.ExecutorService;
public class MyAppGlobalDependenciesModule
extends PrivateModule
{
static final Provider<ExecutorService> myAppExecutorServiceProvider = new MyAppExecutorServiceProvider();
@Override
protected void configure()
{
// here is the one gotcha, although we are binding as an eager singleton; since we are installing
// this module in each of the app's services separately (and each service module is itself a private module)
// we need to enforce the singleton scope in the provider itself (see: MyAppExecutorServiceProvider)
bind(ExecutorService.class).toProvider(myAppExecutorServiceProvider).asEagerSingleton();
expose(ExecutorService.class);
}
}
package app;
import com.google.inject.Provider;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MyAppExecutorServiceProvider
implements Provider<ExecutorService>
{
// This is how we enforce the singleton-ness of our app's global ExecutorService, classic singelton style
// We could have also used an enum
private static final ExecutorService singletonExecutorService = Executors.newSingleThreadExecutor();
@Override
public ExecutorService get()
{
return singletonExecutorService;
}
}
package blue;
import java.util.concurrent.ExecutorService;
import com.google.inject.PrivateModule;
import com.google.inject.Provider;
public class BlueModule
extends PrivateModule
{
private final Provider<ExecutorService> executorServiceProvider;
public BlueModule(
final Provider<ExecutorService> executorServiceProvider)
{
this.executorServiceProvider = executorServiceProvider;
}
@Override
protected void configure()
{
bind(ExecutorService.class).toProvider(executorServiceProvider);
bind(BlueService.class).to(BlueServiceImpl.class);
expose(BlueService.class);
}
}
package blue;
public interface BlueService
{
void printInternals();
}
package blue;
import java.util.concurrent.ExecutorService;
import com.google.inject.Inject;
class BlueServiceImpl
implements BlueService
{
private final ExecutorService executor;
@Inject
BlueServiceImpl(final ExecutorService executor)
{
this.executor = executor;
}
@Override
public void printInternals()
{
System.out.println("Blue Executor: " + executor);
}
}
package red;
import java.util.concurrent.ExecutorService;
import com.google.inject.PrivateModule;
import com.google.inject.Provider;
public class RedModule
extends PrivateModule
{
private final Provider<ExecutorService> executorServiceProvider;
public RedModule(
final Provider<ExecutorService> executorServiceProvider)
{
this.executorServiceProvider = executorServiceProvider;
}
@Override
protected void configure()
{
bind(ExecutorService.class).toProvider(executorServiceProvider).asEagerSingleton();
bind(RedService.class).to(RedServiceImpl.class);
expose(RedService.class);
}
}
package red;
public interface RedService
{
void printInternals();
}
package red;
import com.google.inject.Inject;
import java.util.concurrent.ExecutorService;
class RedServiceImpl
implements RedService
{
private final ExecutorService executor;
@Inject
RedServiceImpl(final ExecutorService executor)
{
this.executor = executor;
}
@Override
public void printInternals()
{
System.out.println("Red Executor: " + executor);
}
}
package app;
import com.google.inject.Module;
import com.google.inject.PrivateModule;
public class MyServiceModule
extends PrivateModule
{
private final Module appGlobalDependencies;
public MyServiceModule(final Module appGlobalDependencies)
{
this.appGlobalDependencies = appGlobalDependencies;
}
@Override
protected void configure()
{
install(appGlobalDependencies);
bind(MyService.class).to(MyServiceImpl.class);
expose(MyService.class);
}
}
package app;
public interface MyService
{
void printInternals();
}
package app;
import blue.BlueService;
import com.google.inject.Inject;
import red.RedService;
import java.util.concurrent.ExecutorService;
class MyServiceImpl
implements MyService
{
private final ExecutorService executor;
private final RedService red;
private final BlueService blue;
@Inject
MyServiceImpl(final ExecutorService executor, final RedService red,
final BlueService blue)
{
this.executor = executor;
this.red = red;
this.blue = blue;
}
@Override
public void printInternals()
{
System.out.println("MyService Executor: " + executor);
red.printInternals();
blue.printInternals();
}
}
package app;
import com.google.inject.Module;
import com.google.inject.PrivateModule;
public class MyOtherServiceModule
extends PrivateModule
{
private final Module appGlobalDependencies;
public MyOtherServiceModule(final Module appGlobalDependencies)
{
this.appGlobalDependencies = appGlobalDependencies;
}
@Override
protected void configure()
{
install(appGlobalDependencies);
bind(MyOtherService.class).to(MyOtherServiceImpl.class);
expose(MyOtherService.class);
}
}
package app;
public interface MyOtherService
{
void printInternals();
}
package app;
import blue.BlueService;
import com.google.inject.Inject;
import red.RedService;
import java.util.concurrent.ExecutorService;
class MyOtherServiceImpl
implements MyOtherService
{
private final ExecutorService executor;
private final RedService red;
private final BlueService blue;
@Inject
MyOtherServiceImpl(final ExecutorService executor, final RedService red,
final BlueService blue)
{
this.executor = executor;
this.red = red;
this.blue = blue;
}
@Override
public void printInternals()
{
System.out.println("MyOtherService Executor: " + executor);
red.printInternals();
blue.printInternals();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment