Package play.inject

Class BindingKey<T>


  • public final class BindingKey<T>
    extends Object
    A binding key.

    A binding key consists of a class and zero or more JSR-330 qualifiers.

    See the Module class for information on how to provide bindings.

    • Constructor Detail

      • BindingKey

        public BindingKey​(Class<T> clazz,
                          Optional<QualifierAnnotation> qualifier)
        A binding key.

        A binding key consists of a class and zero or more JSR-330 qualifiers.

        See the Module class for information on how to provide bindings.

        Parameters:
        clazz - The class to bind.
        qualifier - An optional qualifier.
      • BindingKey

        public BindingKey​(play.api.inject.BindingKey<T> underlying)
      • BindingKey

        public BindingKey​(Class<T> clazz)
    • Method Detail

      • getClazz

        public Class<T> getClazz()
      • qualifiedWith

        public <A extends AnnotationBindingKey<T> qualifiedWith​(A instance)
        Qualify this binding key with the given instance of an annotation.

        This can be used to specify bindings with annotations that have particular values.

      • qualifiedWith

        public <A extends AnnotationBindingKey<T> qualifiedWith​(Class<A> annotation)
        Qualify this binding key with the given annotation.

        For example, you may have both a cached implementation, and a direct implementation of a service. To differentiate between them, you may define a Cached annotation:

        
         bindClass(Foo.class).qualifiedWith(Cached.class).to(FooCached.class),
         bindClass(Foo.class).to(FooImpl.class)
        
         ...
        
         class MyController {
           {@literal @}Inject
           MyController({@literal @}Cached Foo foo) {
             ...
           }
           ...
         }
         
        In the above example, the controller will get the cached Foo service.
      • qualifiedWith

        public BindingKey<T> qualifiedWith​(String name)
        Qualify this binding key with the given name.

        For example, you may have both a cached implementation, and a direct implementation of a service. To differentiate between them, you may decide to name the cached one:

        
         bindClass(Foo.class).qualifiedWith("cached").to(FooCached.class),
         bindClass(Foo.class).to(FooImpl.class)
        
         ...
        
         class MyController {
           {@literal @}Inject
           MyController({@literal @}Named("cached") Foo foo) {
             ...
           }
           ...
         }
         
        In the above example, the controller will get the cached `Foo` service.
      • to

        public Binding<T> to​(Class<? extends T> implementation)
        Bind this binding key to the given implementation class.

        This class will be instantiated and injected by the injection framework.

      • to

        public Binding<T> to​(javax.inject.Provider<? extends T> provider)
        Bind this binding key to the given provider instance.

        This provider instance will be invoked to obtain the implementation for the key.

      • to

        public <A extends TBinding<T> to​(Supplier<A> instance)
        Bind this binding key to the given instance.
      • to

        public Binding<T> to​(BindingKey<? extends T> key)
        Bind this binding key to another binding key.
      • toProvider

        public <P extends javax.inject.Provider<? extends T>> Binding<T> toProvider​(Class<P> provider)
        Bind this binding key to the given provider class.

        The dependency injection framework will instantiate and inject this provider, and then invoke its `get` method whenever an instance of the class is needed.

      • toInstance

        public Binding<T> toInstance​(T instance)
        Bind this binding key to the given instance.
      • toSelf

        public Binding<T> toSelf()
        Bind this binding key to itself.
      • asScala

        public play.api.inject.BindingKey<T> asScala()