Package org.jboss.weld.inject
Interface WeldInstance<T>
- Type Parameters:
T- the required bean type
- All Superinterfaces:
jakarta.enterprise.inject.Instance<T>,Iterable<T>,jakarta.inject.Provider<T>
public interface WeldInstance<T>
extends jakarta.enterprise.inject.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:
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from interface jakarta.enterprise.inject.Instance
jakarta.enterprise.inject.Instance.Handle<T extends Object> -
Method Summary
Modifier and TypeMethodDescriptionComparator<jakarta.enterprise.inject.Instance.Handle<?>>The returned comparator sorts handles by priority in descending order.Deprecated.Deprecated.handlers()Deprecated.default Stream<? extends WeldInstance.Handler<T>>Deprecated.<U extends T>
WeldInstance<U>select(jakarta.enterprise.util.TypeLiteral<U> subtype, Annotation... qualifiers) select(Annotation... qualifiers) <U extends T>
WeldInstance<U>select(Class<U> subtype, Annotation... qualifiers) <X> WeldInstance<X>select(Type subtype, Annotation... qualifiers) Obtains a childInstancefor the given required type and additional required qualifiers.Methods inherited from interface jakarta.enterprise.inject.Instance
destroy, getHandle, handles, handlesStream, isAmbiguous, isResolvable, isUnsatisfied, streamMethods inherited from interface java.lang.Iterable
forEach, iterator, spliteratorMethods inherited from interface jakarta.inject.Provider
get
-
Method Details
-
getHandler
Deprecated.This method is deprecated as a similar functioning method exists in CDI 4.0 and newer. Users should instead useInstance.getHandle(). 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:
jakarta.enterprise.inject.UnsatisfiedResolutionException- if there is no bean with given type and qualifiersjakarta.enterprise.inject.AmbiguousResolutionException- if there is more than one bean given type and qualifiers
-
handlers
Deprecated.This method is deprecated as a similar functioning method exists in CDI 4.0 and newer. Users should instead useInstance.handles(). 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
Iterableis stateless and so eachIterable.iterator()produces a new set of handlers.- Returns:
- a new iterable
-
handlersStream
Deprecated.This method is deprecated as a similar functioning method exists in CDI 4.0 and newer. Users should instead useInstance.handlesStream().- Returns:
- a new stream of contextual reference handlers
-
getPriorityComparator
Deprecated.This method is deprecated in favor ofgetHandlePriorityComparator()which operates on a non-deprecatedInstance.Handleinterface. The returned comparator sorts handlers by priority in descending order.- A class-based bean whose annotated type has
jakarta.annotation.Priorityhas the priority of valuejakarta.annotation.Priority#value() - A custom bean which implements
Prioritizedhas the priority of valuePrioritized.getPriority() - Any other bean has the priority of value 0
- Returns:
- a comparator instance
- A class-based bean whose annotated type has
-
getHandlePriorityComparator
Comparator<jakarta.enterprise.inject.Instance.Handle<?>> getHandlePriorityComparator()The returned comparator sorts handles by priority in descending order.- A class-based bean whose annotated type has
jakarta.annotation.Priorityhas the priority of valuejakarta.annotation.Priority#value() - A custom bean which implements
Prioritizedhas the priority of valuePrioritized.getPriority() - Any other bean has the priority of value 0
- Returns:
- a comparator instance
- A class-based bean whose annotated type has
-
select
- Specified by:
selectin interfacejakarta.enterprise.inject.Instance<T>
-
select
- Specified by:
selectin interfacejakarta.enterprise.inject.Instance<T>
-
select
<U extends T> WeldInstance<U> select(jakarta.enterprise.util.TypeLiteral<U> subtype, Annotation... qualifiers) - Specified by:
selectin interfacejakarta.enterprise.inject.Instance<T>
-
select
Obtains a child
Instancefor the given required type and additional required qualifiers. Must be invoked onInstance<T>where T isObject.- Type Parameters:
X- the required type- Parameters:
subtype- aTyperepresenting the required typequalifiers- 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 typeIllegalStateException- if the container is already shutdownIllegalStateException- if invoked onInstance<T>where T is of any other type thanObject
-