Top Description Methods
javax.enterprise.inject

public Interface Instance<T>

extends Iterable<T>, Provider<T>
Known Direct Subinterfaces
javax.enterprise.inject.se.SeContainer
Known Direct Implementers
javax.enterprise.inject.spi.CDI
Type Parameters
<T>
the required bean type
Imports
java.lang.annotation.Annotation, java.util.stream.Stream, .StreamSupport, javax.enterprise.util.TypeLiteral, javax.inject.Provider

Allows the application to dynamically obtain instances of beans with a specified combination of required type and qualifiers.

In certain situations, injection is not the most convenient way to obtain a contextual reference. For example, it may not be used when:

In these situations, an instance of the Instance may be injected:

@Inject
Instance<PaymentProcessor> paymentProcessor;

Any combination of qualifiers may be specified at the injection point:

@Inject
@PayBy(CHEQUE)
Instance<PaymentProcessor> chequePaymentProcessor;

Or, the @Any qualifier may be used, allowing the application to specify qualifiers dynamically:

@Inject
@Any
Instance<PaymentProcessor> anyPaymentProcessor;

Finally, the @New qualifier may be used, allowing the application to obtain a @New qualified bean:

@Inject
@New(ChequePaymentProcessor.class)
Instance<PaymentProcessor> chequePaymentProcessor;

For an injected Instance:

The inherited javax.inject.Provider#get() method returns a contextual references for the unique bean that matches the required type and required qualifiers and is eligible for injection into the class into which the parent Instance was injected, or throws an javax.enterprise.inject.UnsatisfiedResolutionException or javax.enterprise.inject.AmbiguousResolutionException.

PaymentProcessor pp = chequePaymentProcessor.get();

The inherited java.lang.Iterable#iterator() method returns an iterator over contextual references for beans that match the required type and required qualifiers and are eligible for injection into the class into which the parent Instance was injected.

for (PaymentProcessor pp : anyPaymentProcessor)
    pp.test();
Authors
Gavin King, John Ament, Martin Kouba
See Also
javax.inject.Provider#get(), java.lang.Iterable#iterator(), javax.enterprise.util.AnnotationLiteral, javax.enterprise.util.TypeLiteral

Method Summary

Modifier and TypeMethod and Description
public void
destroy(T
the instance to destroy
instance
)

When called, the container destroys the instance if the active context object for the scope type of the bean supports destroying bean instances.

public boolean

Returns:

true if there is more than one bean that matches the required type and qualifiers and is eligible for injection into the class into which the parent Instance was injected, or false otherwise.
isAmbiguous
()

Determines if there is more than one bean that matches the required type and qualifiers and is eligible for injection into the class into which the parent Instance was injected.

public default boolean

Returns:

true if there is exactly one bean that matches the required type and qualifiers and is eligible for injection into the class into which the parent Instance was injected, or false otherwise.
isResolvable
()

Determines if there is exactly one bean that matches the required type and qualifiers and is eligible for injection into the class into which the parent Instance was injected.

public boolean

Returns:

true if there is no bean that matches the required type and qualifiers and is eligible for injection into the class into which the parent Instance was injected, or false otherwise.
isUnsatisfied
()

Determines if there is no bean that matches the required type and qualifiers and is eligible for injection into the class into which the parent Instance was injected.

public Instance<T>

Returns:

the child Instance
select
(Annotation...
the additional required qualifiers
qualifiers
)

Obtains a child Instance for the given additional required qualifiers.

public <
the required type
U extends T
>
Instance<U>

Returns:

the child Instance
select
(Class<U>
a java.lang.Class representing the required type
subtype
,
Annotation...
the additional required qualifiers
qualifiers
)

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

public <
the required type
U extends T
>
Instance<U>

Returns:

the child Instance
select
(TypeLiteral<U>
a javax.enterprise.util.TypeLiteral representing the required type
subtype
,
Annotation...
the additional required qualifiers
qualifiers
)

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

public default Stream<T>

Returns:

a Stream representing the beans associated with this Instance object
stream
()

When called, provides back a Stream of the beans available in this Instance.

Inherited from java.lang.Iterable:
forEachiteratorspliterator
Inherited from javax.inject.Provider:
get

Method Detail

destroyback to summary
public void destroy(T instance)

When called, the container destroys the instance if the active context object for the scope type of the bean supports destroying bean instances. All normal scoped built-in contexts support destroying bean instances.

The instance passed should either be a dependent scoped bean instance obtained from the same Instance object, or the client proxy for a normal scoped bean instance.

Parameters
instance:T

the instance to destroy

Exceptions
UnsupportedOperationException:
if the active context object for the scope type of the bean does not support destroying bean instances
Since
1.1
isAmbiguousback to summary
public boolean isAmbiguous()

Determines if there is more than one bean that matches the required type and qualifiers and is eligible for injection into the class into which the parent Instance was injected.

Returns:boolean

true if there is more than one bean that matches the required type and qualifiers and is eligible for injection into the class into which the parent Instance was injected, or false otherwise.

isResolvableback to summary
public default boolean isResolvable()

Determines if there is exactly one bean that matches the required type and qualifiers and is eligible for injection into the class into which the parent Instance was injected.

Returns:boolean

true if there is exactly one bean that matches the required type and qualifiers and is eligible for injection into the class into which the parent Instance was injected, or false otherwise.

Since
2.0
isUnsatisfiedback to summary
public boolean isUnsatisfied()

Determines if there is no bean that matches the required type and qualifiers and is eligible for injection into the class into which the parent Instance was injected.

Returns:boolean

true if there is no bean that matches the required type and qualifiers and is eligible for injection into the class into which the parent Instance was injected, or false otherwise.

selectback to summary
public Instance<T> select(Annotation... qualifiers)

Obtains a child Instance for the given additional required qualifiers.

Parameters
qualifiers:Annotation[]

the additional required qualifiers

Returns:Instance<T>

the child Instance

Exceptions
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
selectback to summary
public <U extends T> Instance<U> select(Class<U> subtype, Annotation... qualifiers)

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

Parameters
<U>
the required type
subtype:Class<U>

a java.lang.Class representing the required type

qualifiers:Annotation[]

the additional required qualifiers

Returns:Instance<U>

the child Instance

Exceptions
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
selectback to summary
public <U extends T> Instance<U> select(TypeLiteral<U> subtype, Annotation... qualifiers)

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

Parameters
<U>
the required type
subtype:TypeLiteral<U>

a javax.enterprise.util.TypeLiteral representing the required type

qualifiers:Annotation[]

the additional required qualifiers

Returns:Instance<U>

the child Instance

Exceptions
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
streamback to summary
public default Stream<T> stream()

When called, provides back a Stream of the beans available in this Instance. If no beans are found, it returns an empty stream.

Returns:Stream<T>

a Stream representing the beans associated with this Instance object

Since
2.0