官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 凭祥市| 泸定县| 乌兰县| 平乐县| 双流县| 长岛县| 永定县| 吉水县| 奉贤区| 墨玉县| 土默特左旗| 中山市| 扎囊县| 宁陵县| 青海省| 偏关县| 垫江县| 辉南县| 福清市| 平原县| 云和县| 衡南县| 滦南县| 石首市| 昌吉市| 阳原县| 资溪县| 青川县| 中超| 天全县| 英吉沙县| 乳源| 高清| 涿州市| 大冶市| 隆林| 金昌市| 安康市| 塘沽区| 化德县| 高雄县|