Java Reference
In-Depth Information
To make a bean available for lookup, injection, or EL resolution using this mechanism,
give it a
javax.enterprise.inject.Alternative
annotation and then use the
alternative
element to specify it in the
beans.xml
file.
For example, you might want to create a full version of a bean and also a simpler version
TestCoderImpl
. The test bean is annotated as follows:
@Alternative
public class TestCoderImpl implements Coder { ... }
The full version is not annotated:
public class CoderImpl implements Coder { ... }
The managed bean injects an instance of the
Coder
interface:
@Inject
Coder coder;
The alternative version of the bean is used by the application only if that version is de-
clared as follows in the
beans.xml
file:
<beans ... >
<alternatives>
<class>encoder.TestCoderImpl</class>
</alternatives>
</beans>
If the
alternatives
element is commented out in the
beans.xml
file, the
Coder-
Impl
class is used.
You can also have several beans that implement the same interface, all annotated
@Al-
ternative
. In this case, you must specify in the
beans.xml
file which of these al-
ternative beans you want to use. If
CoderImpl
were also annotated
@Alternative
,
one of the two beans would always have to be specified in the
beans.xml
file.