public class ImmutableBeanWrapper<T> extends BaseImmutableBean<T>
This bean-wrapper allows you to build a general purpose bean (likely a
producer method), and register it for a narrowed type (or qualifiers).
You can easily create instances of this class with the
WrappingBeanBuilder
.
For example, you could create a producer method which uses an a String ID tolocated an object (the object can have any class):
@Produces // Use some synthetic scope to prevent this from interfering with other // resolutions @MyProducer Object produce(InjectionPoint ip) { String id = ip.getAnnotated().getAnnotation(Id.class).value(); // Lookup and return the object for the id }
The wrapped bean must return an object which can be cast to the
type T
, otherwise a ClassCastException
will be thrown at
runtime when the bean is created.
You can then register a narrowing bean for each type you need:
event.addBean(new NarrowingBeanBuilder<T>(delegateBean).readFromType(type) .create());
ImmutableBeanWrapper
will use the annotations on
definingType
to discover the qualifiers, types, scope,
stereotypes of the bean, as well as determine it's name (if any) and whether
it is an alternative.
The attributes are immutable, and collections are defensively copied on instantiation. It uses the defaults from the specification for properties if not specified.
WrappingBeanBuilder
Constructor and Description |
---|
ImmutableBeanWrapper(javax.enterprise.inject.spi.Bean<T> bean,
String name,
Set<java.lang.annotation.Annotation> qualifiers,
Class<? extends java.lang.annotation.Annotation> scope,
Set<Class<? extends java.lang.annotation.Annotation>> stereotypes,
Set<Type> types,
boolean alternative,
boolean nullable,
String toString)
Instantiate a new
ImmutableBeanWrapper . |
Modifier and Type | Method and Description |
---|---|
T |
create(javax.enterprise.context.spi.CreationalContext<T> creationalContext) |
void |
destroy(T instance,
javax.enterprise.context.spi.CreationalContext<T> creationalContext) |
getBeanClass, getInjectionPoints, getName, getQualifiers, getScope, getStereotypes, getTypes, isAlternative, isNullable, toString
public ImmutableBeanWrapper(javax.enterprise.inject.spi.Bean<T> bean, String name, Set<java.lang.annotation.Annotation> qualifiers, Class<? extends java.lang.annotation.Annotation> scope, Set<Class<? extends java.lang.annotation.Annotation>> stereotypes, Set<Type> types, boolean alternative, boolean nullable, String toString)
ImmutableBeanWrapper
.bean
- the bean to wrapped the lifecycle toname
- the name of the beanqualifiers
- the qualifiers of the beanscope
- the scope of the beanstereotypes
- the bean's stereotypestypes
- the types of the beanalternative
- whether the bean is an alternativenullable
- true if the bean is nullabletoString
- the string which should be returned by #BaseImmutableBean.toString()
Copyright © 2015 The Apache Software Foundation. All rights reserved.