Class GuiceBundle

  • All Implemented Interfaces:
    io.dropwizard.core.ConfiguredBundle<io.dropwizard.core.Configuration>

    public final class GuiceBundle
    extends java.lang.Object
    implements io.dropwizard.core.ConfiguredBundle<io.dropwizard.core.Configuration>
    Bundle enables guice integration for dropwizard. Guice context is configured in initialization phase, but actual injector is created on run phase, This approach provides greater configuration options, because during initialization configuration and environment objects are not available. Bootstrap, Environment and Configuration objects will be available in guice context. But if you need them in module (for example to get configuration parameters), implement one of *AwareModule interfaces (e.g. ConfigurationAwareModule).

    You can use auto scan to automatically install features. To enable auto scan you must configure package (or packages) to search in. To know all supported features look FeatureInstaller implementations. Installers are extendable mechanism: they are resolved by scanning classpath, so you can add new installers in your code and classpath scanning will find them and activate. Also, installers could be disabled (for example, if you want to replace existing installer, you will disable it in builder and implement your own - auto scan will find it and activate).

    Any class may be hidden from auto scanning with @InvisibleForScanner annotation.

    Commands may use injection too, but only fields injection. You can register command manually and their fields will be injected or you can activate auto scan for commands in builder (disabled by default). If auto scan for commands enabled, they will be instantiated with default no-arg constructor.

    Resources are registered using jersey integration module. GuiceFilter is registered for both contexts to provide request and session scopes support.

    Lifecycle:

    • Bundle configured
    • Bundle initialization started
    • If commands scan enabled, commands resolved from classpath and registered in Bootstrap
    • Bundle run started
    • If auto scan enabled, scan classpath for feature installers and perform one more scan with registered installers to find extensions
    • Perform GuiceyBundle lookup with registered GuiceyBundleLookup
    • Guice injector created
    • Register all extensions found by installers
    • Perform injections for all registered environment commands (because only environment commands runs bundles)

    Project was originally inspired by dropwizard-guice project. And because of this, project name was changed to dropwizard-guicey.

    Since:
    31.08.2014
    See Also:
    for configuratio diagnostic
    • Method Detail

      • initialize

        public void initialize​(io.dropwizard.core.setup.Bootstrap bootstrap)
        Specified by:
        initialize in interface io.dropwizard.core.ConfiguredBundle<io.dropwizard.core.Configuration>
      • run

        public void run​(io.dropwizard.core.Configuration configuration,
                        io.dropwizard.core.setup.Environment environment)
                 throws java.lang.Exception
        Specified by:
        run in interface io.dropwizard.core.ConfiguredBundle<io.dropwizard.core.Configuration>
        Throws:
        java.lang.Exception
      • getInjector

        public com.google.inject.Injector getInjector()
        Note that injector could be accessed statically anywhere with InjectorLookup.getInjector(io.dropwizard.core.Application).
        Returns:
        created injector instance or fail if injector not yet created
        Throws:
        java.lang.IllegalStateException - if injector is not yet created
      • builder

        public static GuiceBundle.Builder builder()
        Returns:
        builder instance to construct bundle