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 Details

    • SETTINGS_KEY_SHUTDOWN_HOOK_EXECUTOR_WAIT

      public static final String SETTINGS_KEY_SHUTDOWN_HOOK_EXECUTOR_WAIT
      See Also:
    • SYSTEM_PROP_PRODUCTION_MODE

      public static final String 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

      public static final String IDE_MODE_SYSTEM_PROPERTY
      See Also:
  • Constructor Details

    • Dependencies

      public Dependencies(com.google.inject.Module... modules) throws IOException
      Throws:
      IOException
    • Dependencies

      public Dependencies(Settings configuration, com.google.inject.Module... modules)
      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 injections
      modules - A set of modules
  • Method Details

    • toString

      public String toString()
      Overrides:
      toString in class Object
    • create

      public static Dependencies create(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.
      Parameters:
      modules -
      Returns:
      A dependencies
      Throws:
      Error - if an IOException occurred
    • builder

      public static DependenciesBuilder 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

      public <T> T getInstance(Class<T> type)
      Get an instance of the passed type; throws an exception if nothing is bound.
      Specified by:
      getInstance in interface com.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

      public static com.google.inject.Module createBindings(Settings config) throws IOException
      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. See Stage.PRODUCTION. If not provided, this class will be bootstrapped in Stage.DEVELOPMENT mode.
      Returns:
      true if the system should start up in production mode, false otherwise
    • isProductionMode

      public static boolean isProductionMode(Settings settings)
      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. See Stage.PRODUCTION. If not provided, this class will be bootstrapped in Stage.DEVELOPMENT mode.
      Parameters:
      settings - settings (may contain productionMode)
      Returns:
      true if the system should start up in production mode, false otherwise
    • getSettings

      public Settings getSettings()
    • getSettings

      public Settings getSettings(String namespace)
    • 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

      public void autoShutdownRefresh(SettingsBuilder sb)
    • injectMembers

      public void injectMembers(Object arg)
      Same as getInjector().injectMembers(arg)
      Parameters:
      arg -
    • alsoShutdown

      public final Dependencies alsoShutdown(Dependencies other)
    • loadNamespaceListsFromClasspath

      public static Set<String> loadNamespaceListsFromClasspath() throws IOException
      Throws:
      IOException