Interface BeanContainer
- All Known Subinterfaces:
BeanManager
BeanContainer is a superclass of BeanManager containing capabilities that are portable across
all CDI environments.
Provides operations for obtaining contextual references for beans, along with many other operations of use to CDI applications.
Any bean may obtain an instance of BeanContainer by injecting it:
@Inject BeanContainer container;
- Since:
- 4.0
- Author:
- Matej Novotny
-
Method Summary
Modifier and TypeMethodDescription<T> CreationalContext<T>createCreationalContext(Contextual<T> contextual) Obtain an instance of aCreationalContextfor the given contextual type, or for a non-contextual object.Obtains anInstanceobject to access to beans instances.getBeans(Type beanType, Annotation... qualifiers) Return the set of beans which have the given required type and qualifiers and are available for injection in the module or library containing the class into which theBeanManager/BeanContainerwas injected or, in the Jakarta EE environment, the Jakarta EE component from whose JNDI environment namespace theBeanManager/BeanContainerwas obtained, according to the rules of typesafe resolution.Return the set of beans which have the given EL name and are available for injection in the module or library containing the class into which theBeanManager/BeanContainerwas injected or, in the Jakarta EE environment, the Jakarta EE component from whose JNDI environment namespace theBeanManager/BeanContainerwas obtained, according to the rules of EL name resolution.getContext(Class<? extends Annotation> scopeType) Obtains an active context object for the given scope .getContexts(Class<? extends Annotation> scopeType) Obtains all context objects, active and inactive, for the given scope.getEvent()Returns an instance of Event with specified typejava.lang.Objectand specified qualifier@DefaultIt allows typesafe synchronous or asynchronous event firing without injection ofEventbuilt-in bean requirement.getReference(Bean<?> bean, Type beanType, CreationalContext<?> ctx) Obtains a contextual reference for a certain bean and a certain bean type of the bean.booleanisInterceptorBinding(Class<? extends Annotation> annotationType) Test the given annotation type to determine if it is an interceptor binding type .booleanisMatchingBean(Set<Type> beanTypes, Set<Annotation> beanQualifiers, Type requiredType, Set<Annotation> requiredQualifiers) Returnstrueif a bean with given bean types and qualifiers would be assignable to an injection point with given required type and required qualifiers,falseotherwise.booleanisMatchingEvent(Type specifiedType, Set<Annotation> specifiedQualifiers, Type observedEventType, Set<Annotation> observedEventQualifiers) Returnstrueif an event with given specified type and specified qualifiers would match an observer method with given observed event type and observed event qualifiers,falseotherwise.booleanisNormalScope(Class<? extends Annotation> annotationType) Test the given annotation type to determine if it is a normal scope type.booleanisQualifier(Class<? extends Annotation> annotationType) Test the given annotation type to determine if it is a qualifier type.booleanisScope(Class<? extends Annotation> annotationType) Test the given annotation type to determine if it is a scope type.booleanisStereotype(Class<? extends Annotation> annotationType) Test the given annotation type to determine if it is a stereotype.<X> Bean<? extends X>Apply the ambiguous dependency resolution rules to a set of beans.List<Interceptor<?>>resolveInterceptors(InterceptionType type, Annotation... interceptorBindings) Return an ordered list of enabled interceptors for a set of interceptor bindings and a type of interception and which are enabled in the module or library containing the class into which theBeanManager/BeanContainerwas injected or, in the Jakarta EE environment, the Jakarta EE component from whose JNDI environment namespace theBeanManager/BeanContainerwas obtained.<T> Set<ObserverMethod<? super T>>resolveObserverMethods(T event, Annotation... qualifiers) Return an ordered set of observer methods for an event.
-
Method Details
-
getReference
Obtains a contextual reference for a certain bean and a certain bean type of the bean.
- Parameters:
bean- theBeanobject representing the beanbeanType- a bean type that must be implemented by any client proxy that is returnedctx- aCreationalContextthat may be used to destroy any object with scopeDependentthat is created- Returns:
- a contextual reference representing the bean
- Throws:
IllegalArgumentException- if the given type is not a bean type of the given beanIllegalStateException- if called during application initialization, before theAfterDeploymentValidationevent is fired.
-
createCreationalContext
Obtain an instance of aCreationalContextfor the given contextual type, or for a non-contextual object.- Type Parameters:
T- type of the instance- Parameters:
contextual- theContextual, or a null value in the case of a non-contextual object- Returns:
- the new
CreationalContext
-
getBeans
Return the set of beans which have the given required type and qualifiers and are available for injection in the module or library containing the class into which theBeanManager/BeanContainerwas injected or, in the Jakarta EE environment, the Jakarta EE component from whose JNDI environment namespace theBeanManager/BeanContainerwas obtained, according to the rules of typesafe resolution. If no qualifiers are given, the default qualifier is assumed.Note that when called during invocation of an
AfterBeanDiscoveryevent observer, this method will only return beans discovered by the container before theAfterBeanDiscoveryevent is fired.- Parameters:
beanType- the required bean typequalifiers- the required qualifiers- Returns:
- the resulting set of beans
- Throws:
IllegalArgumentException- if the given type represents a type variableIllegalArgumentException- if two instances of the same non repeating qualifier type are givenIllegalArgumentException- if an instance of an annotation that is not a qualifier type is givenIllegalStateException- if called during application initialization, before theAfterBeanDiscoveryevent is fired.
-
getBeans
Return the set of beans which have the given EL name and are available for injection in the module or library containing the class into which theBeanManager/BeanContainerwas injected or, in the Jakarta EE environment, the Jakarta EE component from whose JNDI environment namespace theBeanManager/BeanContainerwas obtained, according to the rules of EL name resolution.Note that when called during invocation of an
AfterBeanDiscoveryevent observer, this method will only return beans discovered by the container before theAfterBeanDiscoveryevent is fired.- Parameters:
name- the EL name- Returns:
- the resulting set of beans
- Throws:
IllegalStateException- if called during application initialization, before theAfterBeanDiscoveryevent is fired.
-
resolve
Apply the ambiguous dependency resolution rules to a set of beans.Note that when called during invocation of an
AfterBeanDiscoveryevent observer, this method will only return beans discovered by the container before theAfterBeanDiscoveryevent is fired.- Type Parameters:
X- a common type of the beans- Parameters:
beans- a set of beans of the given type- Returns:
- the resolved bean, or null if null or an empty set is passed
- Throws:
AmbiguousResolutionException- if the ambiguous dependency resolution rules failIllegalStateException- if called during application initialization, before theAfterBeanDiscoveryevent is fired.
-
resolveObserverMethods
Return an ordered set of observer methods for an event.Note that when called during invocation of an
AfterBeanDiscoveryevent observer, this method will only return observers discovered by the container before theAfterBeanDiscoveryevent is fired.- Type Parameters:
T- the type of the event- Parameters:
event- the event objectqualifiers- the event qualifiers- Returns:
- the resulting set of observer methods
- Throws:
IllegalArgumentException- if the runtime type of the event object contains a type variableIllegalArgumentException- if two instances of the same non repeating qualifier type are givenIllegalArgumentException- if an instance of an annotation that is not a qualifier type is givenIllegalStateException- if called during application initialization, before theAfterBeanDiscoveryevent is fired.
-
resolveInterceptors
Return an ordered list of enabled interceptors for a set of interceptor bindings and a type of interception and which are enabled in the module or library containing the class into which theBeanManager/BeanContainerwas injected or, in the Jakarta EE environment, the Jakarta EE component from whose JNDI environment namespace theBeanManager/BeanContainerwas obtained.Note that when called during invocation of an
AfterBeanDiscoveryevent observer, this method will only return interceptors discovered by the container before theAfterBeanDiscoveryevent is fired.- Parameters:
type- the type of the interceptioninterceptorBindings- the interceptor bindings- Returns:
- the resulting set of interceptors
- Throws:
IllegalArgumentException- if no interceptor binding type is givenIllegalArgumentException- if two instances of the same interceptor binding type are givenIllegalArgumentException- if an instance of an annotation that is not an interceptor binding type is givenIllegalStateException- if called during application initialization, before theAfterBeanDiscoveryevent is fired.
-
isScope
Test the given annotation type to determine if it is a scope type.- Parameters:
annotationType- the annotation type- Returns:
- true if the annotation type is a scope type
-
isNormalScope
Test the given annotation type to determine if it is a normal scope type.- Parameters:
annotationType- the annotation type- Returns:
trueif the annotation type is a normal scope type
-
isQualifier
Test the given annotation type to determine if it is a qualifier type.- Parameters:
annotationType- the annotation type- Returns:
trueif the annotation type is a qualifier type
-
isStereotype
Test the given annotation type to determine if it is a stereotype.- Parameters:
annotationType- the annotation type- Returns:
trueif the annotation type is a stereotype
-
isInterceptorBinding
Test the given annotation type to determine if it is an interceptor binding type .- Parameters:
annotationType- the annotation to test- Returns:
trueif the annotation type is a interceptor binding type
-
getContext
Obtains an active context object for the given scope .- Parameters:
scopeType- the scope- Returns:
- the context object
- Throws:
ContextNotActiveException- if there is no active context object for the given scopeIllegalArgumentException- if there is more than one active context object for the given scope
-
getContexts
Obtains all context objects, active and inactive, for the given scope.- Parameters:
scopeType- the scope; must not benull- Returns:
- immutable collection of context objects; never
null, but may be empty
-
getEvent
Returns an instance of Event with specified typejava.lang.Objectand specified qualifier@DefaultIt allows typesafe synchronous or asynchronous event firing without injection ofEventbuilt-in bean requirement.- Returns:
- a new
Eventobject whose event type isObjectand qualifier@Default - Since:
- 2.0
-
createInstance
Obtains anInstanceobject to access to beans instances.The returned
Instanceobject can only access instances of beans that are available for injection in the module or library containing the class into which theBeanManager/BeanContainerwas injected or, in the Jakarta EE environment, the Jakarta EE component from whose JNDI environment namespace theBeanContainerwas obtained, according to the rules of typesafe resolution.Instances of dependent scoped beans obtained with this
Instancemust be explicitly destroyed by callingInstance.destroy(Object)If no qualifier is passed to
Instance.select(java.lang.annotation.Annotation...)method, the@Defaultqualifier is assumed.- Returns:
- an
Instanceobject to request beans instances - Throws:
IllegalStateException- if called during application initialization, before theAfterDeploymentValidationevent is fired.- Since:
- 2.0
-
isMatchingBean
boolean isMatchingBean(Set<Type> beanTypes, Set<Annotation> beanQualifiers, Type requiredType, Set<Annotation> requiredQualifiers) Returnstrueif a bean with given bean types and qualifiers would be assignable to an injection point with given required type and required qualifiers,falseotherwise.In line with the specification for beans and typesafe resolution, the set of
beanTypesis considered to always includejava.lang.Object. Types inbeanTypesthat are not legal bean types are ignored. The set ofbeanQualifiersis considered to always include@Anyand also include@Defaultwhen it contains no other qualifier but@Anyand@Named. The set ofrequiredQualifiersis considered to include@Defaultwhen it is empty.Throws
IllegalArgumentExceptionif any of the arguments isnullor if any of thebeanQualifiersorrequiredQualifiersis not a qualifier annotation.- Parameters:
beanTypes- bean types of a bean; must not benullbeanQualifiers- qualifiers of a bean; must not benullrequiredType- required type of an injection point; must not benullrequiredQualifiers- required qualifiers of an injection point; must not benull- Returns:
trueif a bean with given bean types and qualifiers would be assignable to an injection point with given required type and required qualifiers,falseotherwise
-
isMatchingEvent
boolean isMatchingEvent(Type specifiedType, Set<Annotation> specifiedQualifiers, Type observedEventType, Set<Annotation> observedEventQualifiers) Returnstrueif an event with given specified type and specified qualifiers would match an observer method with given observed event type and observed event qualifiers,falseotherwise.For the purpose of observer resolution, the
specifiedTypeis used as the event type directly (because there is no event object in this API) andspecifiedQualifiersare used as event qualifiers. In line with the specification for events and observer resolution, the set of event qualifiers is considered to always include@Any. Further, an empty set ofspecifiedQualifiersis considered to match the set ofobservedEventQualifierswhich contains@Default.Throws
IllegalArgumentExceptionif any of the arguments isnull, if thespecifiedTypecontains a type variable, or if any of thespecifiedQualifiersorobservedEventQualifiersis not a qualifier annotation.- Parameters:
specifiedType- specified type of an event; must not benullspecifiedQualifiers- specified qualifiers of an event; must not benullobservedEventType- observed event type of an observer method; must not benullobservedEventQualifiers- observed event qualifiers on an observer method; must not benull- Returns:
trueif an event object with given type and qualifiers would result in notifying an observer method with given observed event type and observed event qualifiers,falseotherwise
-