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

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.

主站蜘蛛池模板: 黑河市| 琼结县| 静安区| 商洛市| 普兰县| 元谋县| 长乐市| 二连浩特市| 南丰县| 彰武县| 香格里拉县| 滕州市| 柞水县| 仙居县| 衡阳县| 和平区| 安乡县| 库尔勒市| 上杭县| 镇远县| 太谷县| 寻乌县| 宜川县| 德惠市| 泰安市| 合作市| 庄河市| 大丰市| 青州市| 古交市| 长汀县| 灵丘县| 玉龙| 高邑县| 顺义区| 西乌| 镶黄旗| 山丹县| 万州区| 讷河市| 普陀区|