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

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.

主站蜘蛛池模板: 溧水县| 余姚市| 辰溪县| 武义县| 松阳县| 漳浦县| 苏尼特左旗| 乳山市| 诸暨市| 基隆市| 哈密市| 望城县| 获嘉县| 南昌市| 龙门县| 永仁县| 巴东县| 沙河市| 交城县| 黔西| 大兴区| 大安市| 山东| 剑川县| 裕民县| 西城区| 尼玛县| 桂林市| 巫山县| 北碚区| 贵州省| 恩平市| 抚远县| 屏东市| 兴业县| 五原县| 丽江市| 保康县| 木兰县| 长顺县| 泰宁县|