Class PolyBind


  • public class PolyBind
    extends Object
    Provides the ability to create "polymorphic" bindings where the polymorphism is actually just making a decision based on a value in Properties.

    The workflow is that you first create a choice by calling createChoice(). Then you create options using the binder returned by the optionBinder() method. Multiple different modules can call optionBinder() and all options will be reflected at injection time as long as equivalent interface Key objects are passed into the various methods.

    • Constructor Detail

      • PolyBind

        public PolyBind()
    • Method Detail

      • createChoice

        public static <T> com.google.inject.binder.ScopedBindingBuilder createChoice​(com.google.inject.Binder binder,
                                                                                     String property,
                                                                                     com.google.inject.Key<T> interfaceKey,
                                                                                     @Nullable
                                                                                     com.google.inject.Key<? extends T> defaultKey)
        Sets up a "choice" for the injector to resolve at injection time.
        Type Parameters:
        T - interface type
        Parameters:
        binder - the binder for the injector that is being configured
        property - the property that will be checked to determine the implementation choice
        interfaceKey - the interface that will be injected using this choice
        defaultKey - the default instance to be injected if the property doesn't match a choice. Can be null
        Returns:
        A ScopedBindingBuilder so that scopes can be added to the binding, if required.
      • createChoiceWithDefault

        @Deprecated
        public static <T> com.google.inject.binder.ScopedBindingBuilder createChoiceWithDefault​(com.google.inject.Binder binder,
                                                                                                String property,
                                                                                                com.google.inject.Key<T> interfaceKey,
                                                                                                com.google.inject.Key<? extends T> defaultKey,
                                                                                                String defaultPropertyValue)
        Deprecated.
        use createChoiceWithDefault(Binder, String, Key, String) instead. defaultKey argument is ignored.
      • createChoiceWithDefault

        public static <T> com.google.inject.binder.ScopedBindingBuilder createChoiceWithDefault​(com.google.inject.Binder binder,
                                                                                                String property,
                                                                                                com.google.inject.Key<T> interfaceKey,
                                                                                                String defaultPropertyValue)
        Sets up a "choice" for the injector to resolve at injection time.
        Type Parameters:
        T - interface type
        Parameters:
        binder - the binder for the injector that is being configured
        property - the property that will be checked to determine the implementation choice
        interfaceKey - the interface that will be injected using this choice
        defaultPropertyValue - the default property value to use if the property is not set.
        Returns:
        A ScopedBindingBuilder so that scopes can be added to the binding, if required.
      • optionBinder

        public static <T> com.google.inject.multibindings.MapBinder<String,​T> optionBinder​(com.google.inject.Binder binder,
                                                                                                 com.google.inject.Key<T> interfaceKey)
        Binds an option for a specific choice. The choice must already be registered on the injector for this to work.
        Type Parameters:
        T - interface type
        Parameters:
        binder - the binder for the injector that is being configured
        interfaceKey - the interface that will have an option added to it. This must equal the Key provided to createChoice
        Returns:
        A MapBinder that can be used to create the actual option bindings.