Class BindingsOverrideInjectorFactory

java.lang.Object
ru.vyarus.dropwizard.guice.test.binding.BindingsOverrideInjectorFactory
All Implemented Interfaces:
InjectorFactory

public class BindingsOverrideInjectorFactory extends Object implements InjectorFactory
Custom injector factory used to override already overridden bindings. Note that in most cases direct override support in GuiceBundle.Builder.modulesOverride(Module...) will be enough. Custom injector factory may be required to override binding, already overridden by modules override feature.

Usage:

  • Register custom factory in main bundle: bundle.injectorFactory(new BindingsOverrideInjectorFactory())
  • Register modules with overriding bindings using override(Module...).

Note that factory may be registered using GuiceyConfigurationHook (in spock or junit extension). Custom modules could be registered with hook too because hook provides a good integration point before application start.

It is assumed that modules will be registered at the same thread where application will initialize (thread local state used to support parallel testing).

Since:
24.06.2018
  • Constructor Details

    • BindingsOverrideInjectorFactory

      public BindingsOverrideInjectorFactory()
      Create a factory.
  • Method Details

    • createInjector

      public com.google.inject.Injector createInjector(com.google.inject.Stage stage, Iterable<? extends com.google.inject.Module> modules)
      Description copied from interface: InjectorFactory
      Creates an injector instance.

      NOTE: if overriding modules were used (GuiceBundle.Builder.modulesOverride(Module...)) then modules list will contain only one overridden module. Modules.override(Module...) can be used on this module too, if required.

      Specified by:
      createInjector in interface InjectorFactory
      Parameters:
      stage - target injector stage.
      modules - modules supplied to injector
      Returns:
      injector instance.
    • override

      public static void override(com.google.inject.Module... modules)
      Overriding bindings registration. Note that modules should contain only overriding bindings - all other will be used from main modules.
      Parameters:
      modules - modules containing overriding bindings
      Throws:
      IllegalStateException - if injector was already created to prevent bad usage