Package com.mastfrog.giulius
Class Dependencies
java.lang.Object
com.mastfrog.giulius.Dependencies
- All Implemented Interfaces:
com.mastfrog.abstractions.instantiate.Instantiator
public final class Dependencies
extends Object
implements com.mastfrog.abstractions.instantiate.Instantiator
A wrapper around Guice's injector which enforces a few things such as how
configuration information is loaded, and binds @Named injections to
values from system properties, environment variables, any default values
specified using the @Defaults annotation, any
com/mastfrom/defaults.properties files on the classpath,
Typically you create a Dependencies once on startup, and then get whatever
bootsrap objects you need to start the application. It is possible to
completely isolate things by using multiple Dependencies, but this is usually
an indication of doing something wrong.
- Author:
- Tim Boudreau
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionDependencies(com.google.inject.Module... modules) Dependencies(Settings configuration, com.google.inject.Module... modules) Create a dependency using the passed Configuration, bypassing any loading from the classpath or elsewhere. -
Method Summary
Modifier and TypeMethodDescriptionfinal DependenciesalsoShutdown(Dependencies other) voidstatic DependenciesBuilderbuilder()static Dependenciescreate(com.google.inject.Module... modules) Create a Dependencies with the classpath contents used to look up properties files and named properties, and the default settings.protected com.google.inject.ModuleCreate a module which binds @Named properties appropriately.static com.google.inject.ModulecreateBindings(Settings config) For use with frameworks that insist on creating the injectorcom.google.inject.InjectorGet the injector, creating it if necessary.<T> TgetInstance(com.google.inject.Key<T> key) Get an instance of the passed key; throws an exception if nothing is bound.<T> TgetInstance(Class<T> type) Get an instance of the passed type; throws an exception if nothing is bound.getSettings(String namespace) com.google.inject.StagegetStage()Get the Guice stage, as determined by isProductionMode()voidinjectMembers(Object arg) Same as getInjector().injectMembers(arg)static booleanUsed by the tests.guice framework to bypass execution of long running tests if this system property is set.booleanwhether the productionMode system property (-DproductionMode=true) or settings (productionMode=true) is set.static booleanisProductionMode(Settings settings) Whether the productionMode system property (-DproductionMode=true) or settings (productionMode=true) is set.voidshutdown()Triggers running shutdown hooks; for use with unit tests, servlet unloading, etc.toString()
-
Field Details
-
SETTINGS_KEY_SHUTDOWN_HOOK_EXECUTOR_WAIT
- See Also:
-
SYSTEM_PROP_PRODUCTION_MODE
System property which determines Guice stage & result of isProductionMode(). System property overrides same value in the default namespace settings (string value is "productionMode").- See Also:
-
IDE_MODE_SYSTEM_PROPERTY
- See Also:
-
-
Constructor Details
-
Dependencies
- Throws:
IOException
-
Dependencies
Create a dependency using the passed Configuration, bypassing any loading from the classpath or elsewhere.- Parameters:
configuration- The configuration which is the source for Named injectionsmodules- A set of modules
-
-
Method Details
-
toString
-
create
Create a Dependencies with the classpath contents used to look up properties files and named properties, and the default settings.- Parameters:
modules-- Returns:
- A dependencies
- Throws:
Error- if an IOException occurred
-
builder
-
getInjector
public com.google.inject.Injector getInjector()Get the injector, creating it if necessary. This is the typical entry-point for starting an application, e.g.:Dependencies deps = new Dependencies(new Module1(), new Module2()); Server server = deps.getInjector().getInstance(Server.class); server.start();
- Returns:
-
getInstance
Get an instance of the passed type; throws an exception if nothing is bound.- Specified by:
getInstancein interfacecom.mastfrog.abstractions.instantiate.Instantiator- Type Parameters:
T- A type- Parameters:
type- The type- Returns:
- An instance of that class
-
getInstance
public <T> T getInstance(com.google.inject.Key<T> key) Get an instance of the passed key; throws an exception if nothing is bound.- Type Parameters:
T- A type- Parameters:
type- The type- Returns:
- An instance of that class
-
shutdown
public void shutdown()Triggers running shutdown hooks; for use with unit tests, servlet unloading, etc. -
createBindings
For use with frameworks that insist on creating the injector- Parameters:
config-- Returns:
- Throws:
IOException
-
createBindings
protected com.google.inject.Module createBindings()Create a module which binds @Named properties appropriately. This method is only necessary when using frameworks such as GuiceRestEasy, which insist on creating the injector.- Returns:
-
isProductionMode
public boolean isProductionMode()whether the productionMode system property (-DproductionMode=true) or settings (productionMode=true) is set. If it is, will be configured for production mode, meaning that singletons will be initialized eagerly, and that modules might configure themselves to configure e.g. for using a real mail server rather than a mock as you would probably do in development mode. SeeStage.PRODUCTION. If not provided, this class will be bootstrapped inStage.DEVELOPMENTmode.- Returns:
- true if the system should start up in production mode, false otherwise
-
isProductionMode
Whether the productionMode system property (-DproductionMode=true) or settings (productionMode=true) is set. If it is, will be configured for production mode, meaning that singletons will be initialized eagerly, and that modules might configure themselves to configure e.g. for using a real mail server rather than a mock as you would probably do in development mode. SeeStage.PRODUCTION. If not provided, this class will be bootstrapped inStage.DEVELOPMENTmode.- Parameters:
settings- settings (may contain productionMode)- Returns:
- true if the system should start up in production mode, false otherwise
-
getSettings
-
getSettings
-
getStage
public com.google.inject.Stage getStage()Get the Guice stage, as determined by isProductionMode()- Returns:
-
isIDEMode
public static boolean isIDEMode()Used by the tests.guice framework to bypass execution of long running tests if this system property is set. To use, configure your IDE to pass -Din.ide=true to Maven- Returns:
- True if the system property is set
-
autoShutdownRefresh
-
injectMembers
Same as getInjector().injectMembers(arg)- Parameters:
arg-
-
alsoShutdown
-
loadNamespaceListsFromClasspath
- Throws:
IOException
-