Interface WeldInstance<T>

  • Type Parameters:
    T - the required bean type
    All Superinterfaces:
    Instance<T>, Iterable<T>, Provider<T>
    All Known Implementing Classes:
    AbstractCDI, ForwardingWeldInstance, InstanceImpl, SimpleCDI, WeldContainer

    public interface WeldInstance<T>
    extends Instance<T>
    Represents an enhanced version of Instance.

    In the following example we filter out beans which are not Dependent then sort the beans by priority and use the handler whose bean has the highest priority (according to getPriorityComparator()) to obtain the hello string. Note that contextual references for beans with lower priority are not created at all.

     @ApplicationScoped
     class Hello {
    
         @Inject
         WeldInstance<HelloProvider> instance;
    
         String hello() {
             HelloProvider helloProvider = instance.handlersStream().filter(h -> h.getBean().getScope().equals(Dependent.class))
                     .sorted(instance.getPriorityComparator()).findFirst().map(Handler::get).orElse(null);
             if (helloProvider != null)
                 return helloProvider.getHello();
             return "No hello provider found!";
         }
     }
     
    Author:
    Martin Kouba
    See Also:
    WELD-2204
    • Method Detail

      • getHandler

        WeldInstance.Handler<T> getHandler()
        Obtains an initialized contextual reference handler for the bean that has the required type and required qualifiers and is eligible for injection.

        The contextual reference is obtained lazily, i.e. when first needed.

        Returns:
        a new handler
        Throws:
        UnsatisfiedResolutionException - if there is no bean with given type and qualifiers
        AmbiguousResolutionException - if there is more than one bean given type and qualifiers
      • handlers

        Iterable<WeldInstance.Handler<T>> handlers()
        Allows to iterate over contextual reference handlers for all the beans that have the required type and required qualifiers and are eligible for injection.

        Note that the returned Iterable is stateless and so each Iterable.iterator() produces a new set of handlers.

        Returns:
        a new iterable
      • getPriorityComparator

        Comparator<WeldInstance.Handler<?>> getPriorityComparator()
        The returned comparator sorts handlers by priority in descending order.
        • A class-based bean whose annotated type has jakarta.annotation.Priority has the priority of value jakarta.annotation.Priority#value()
        • A custom bean which implements Prioritized has the priority of value Prioritized.getPriority()
        • Any other bean has the priority of value 0
        Returns:
        a comparator instance
      • select

        WeldInstance<T> select​(Annotation... qualifiers)
        Description copied from interface: Instance

        Obtains a child Instance for the given additional required qualifiers.

        Specified by:
        select in interface Instance<T>
        Parameters:
        qualifiers - the additional required qualifiers
        Returns:
        the child Instance
      • select

        <U extends TWeldInstance<U> select​(Class<U> subtype,
                                             Annotation... qualifiers)
        Description copied from interface: Instance

        Obtains a child Instance for the given required type and additional required qualifiers.

        Specified by:
        select in interface Instance<T>
        Type Parameters:
        U - the required type
        Parameters:
        subtype - a Class representing the required type
        qualifiers - the additional required qualifiers
        Returns:
        the child Instance
      • select

        <U extends TWeldInstance<U> select​(TypeLiteral<U> subtype,
                                             Annotation... qualifiers)
        Description copied from interface: Instance

        Obtains a child Instance for the given required type and additional required qualifiers.

        Specified by:
        select in interface Instance<T>
        Type Parameters:
        U - the required type
        Parameters:
        subtype - a TypeLiteral representing the required type
        qualifiers - the additional required qualifiers
        Returns:
        the child Instance
      • select

        <X> WeldInstance<X> select​(Type subtype,
                                   Annotation... qualifiers)

        Obtains a child Instance for the given required type and additional required qualifiers. Must be invoked on Instance<T> where T is Object.

        Type Parameters:
        X - the required type
        Parameters:
        subtype - a Type representing the required type
        qualifiers - the additional required qualifiers
        Returns:
        the child Instance
        Throws:
        IllegalArgumentException - if passed two instances of the same non repeating qualifier type, or an instance of an annotation that is not a qualifier type
        IllegalStateException - if the container is already shutdown
        IllegalStateException - if invoked on Instance<T> where T is of any other type than Object