- 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.
推薦閱讀
- Getting Started with React
- Android Studio Essentials
- C和C++安全編碼(原書第2版)
- PhoneGap Mobile Application Development Cookbook
- 軟件工程
- JavaScript 程序設(shè)計(jì)案例教程
- Linux:Embedded Development
- Flutter跨平臺(tái)開發(fā)入門與實(shí)戰(zhàn)
- Protocol-Oriented Programming with Swift
- 細(xì)說Python編程:從入門到科學(xué)計(jì)算
- Java程序設(shè)計(jì)入門(第2版)
- SQL Server 2008實(shí)用教程(第3版)
- 基于MATLAB的控制系統(tǒng)仿真及應(yīng)用
- 深入大型數(shù)據(jù)集:并行與分布化Python代碼
- Three.js Essentials