Skip to content

Bootstrapping

Randgalt edited this page Aug 21, 2012 · 25 revisions

Governator creates two Guice injectors: an internal “bootstrapping” Injector and the main application Injector. The main Injector is created when you call LifecycleInjectorBuilder.createInjector(). The bootstrapping injector is created internally by Governator.

Why two Injectors?

Several Governator features introduce recursive dependencies. Asset loading and configuration mapping require AssetLoader and ConfigurationProvider instances. These instances are needed as the main Injector is being created (i.e. as Guice is creating instances).

Bootstrapping Steps

Bootstrapping is part of the classpath scanning step:

  • Classes annotated with @RequiredAsset are examined to determine the specified AssetLoader class. A singleton binding is added to the bootstrap Injector for this AssetLoader.
  • ConfigurationProvider and AssetLoader classes annotated with @AutoBindSingleton are detected and bound into the bootstrap Injector.
  • Application specific bootstrap binding is done via the specified bootstrap module (if any).

You can specify your own bootstrap bindings if needed by specifying a bootstrap module:

LifecycleInjector.builder().withBootstrapModule(yourBootstrapModule)

The bootstrap module is passed a special Guice Binder that has additional methods for binding AssetLoaders, AssetLoader parameters and ConfigurationProviders. Always use these special methods:

  /**
   * Begin binding a required asset name/value to a loader
   *
   * @param assetName asset name/value
   * @return binder
   */
  public LinkedBindingBuilder<AssetLoader> bindAssetLoader(String assetName);

  /**
   * Begin binding the default asset loader
   *
   * @return binder
   */
  public LinkedBindingBuilder<AssetLoader> bindDefaultAssetLoader();

  /**
   * Begin binding a required asset name/type to an asset parameter
   *
   * @param assetName the name/value of the asset
   * @param type the value type
   * @return binder
   */
  public<T> LinkedBindingBuilder<T> bindAssetParameter(String assetName, TypeLiteral<T> type);

  /**
   * Begin binding a required asset name/type to an asset parameter
   *
   * @param assetName the name/value of the asset
   * @param type the value type
   * @return binder
   */
  public<T> LinkedBindingBuilder<T> bindAssetParameter(String assetName, Class<T> type);

  /**
   * Use this to bind {@link ConfigurationProvider}s. Do NOT use standard Guice binding.
   *
   * @return configuration binding builder
   */
  public LinkedBindingBuilder<ConfigurationProvider> bindConfigurationProvider();

Completion

At the end of Bootstrapping, Governator creates the bootstrap injector and gets the LifecycleManager instance which manages the main injection phase. Any bound asset loaders or configuration providers are injected into the LifecycleManager.