- JBoss Weld CDI for Java Platform
- Ken Finnegan
- 133字
- 2021-08-13 16:49:55
Alternatives
Alternatives are explicitly declared within beans.xml
for them to be considered by the container for typesafe resolution, as they are disabled by default. One of the common uses of alternatives is for different deployment scenarios, such as for test deployments.
To create an alternative, a bean only requires the @Alternative
annotation to be present on its type:
@Alternative @Admin @User public class MockAccount implements Account { ... }
Activation of the alternative, for beans within the same archive, would need a beans.xml
with:
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> <alternatives> <class>org.cdibook.chapter2.alternatives.MockAccount</class> </alternatives> </beans>
Note
If an injection point has ambiguous dependencies during container startup, the container will look for any bean that is an alternative amongst the list of eligible beans. If there is only one alternative bean that is eligible, it will be injected instead of causing a deployment exception.
推薦閱讀
- FuelPHP Application Development Blueprints
- Visual Basic 6.0程序設計計算機組裝與維修
- 編程珠璣(續)
- HTML5+CSS3基礎開發教程(第2版)
- C語言程序設計學習指導與習題解答
- 表哥的Access入門:以Excel視角快速學習數據庫開發(第2版)
- RealSenseTM互動開發實戰
- Advanced Express Web Application Development
- 小程序,巧應用:微信小程序開發實戰(第2版)
- Mastering ArcGIS Enterprise Administration
- Hands-On GUI Programming with C++ and Qt5
- Canvas Cookbook
- Hands-On JavaScript for Python Developers
- Anaconda數據科學實戰
- 從零開始學UI:概念解析、實戰提高、突破規則