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

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.

主站蜘蛛池模板: 清丰县| 绵竹市| 静宁县| 于田县| 同心县| 邵武市| 革吉县| 科尔| 玉田县| 天长市| 昆山市| 香河县| 天镇县| 张掖市| 张掖市| 阿荣旗| 阿勒泰市| 广平县| 芷江| 永济市| 遵义县| 临朐县| 环江| 泸州市| 浦城县| 米易县| 东乡| 永仁县| 奇台县| 宁河县| 阳泉市| 肥东县| 霞浦县| 威海市| 大港区| 阜南县| 类乌齐县| 墨玉县| 中山市| 孟村| 鄢陵县|