1Z0-900 · Question #69
Given the following class definition with numbered lines: How do you specify to use this mock CDI bean implementation instead of the regular implementation class?
The correct answer is B. Start up the server with the optional -alternative command-line option, specifying any alternative. There appears to be an error in this question's answer key. Based on CDI (Contexts and Dependency Injection) specification, Option A is the correct answer, not B. Why A is correct: In CDI, you enable an alternative bean by annotating the alternative class with @Alternative…
Question
Given the following class definition with numbered lines:
How do you specify to use this mock CDI bean implementation instead of the regular implementation class?
Options
- AUse the alternatives element in the beans.xml file and specify the class name in the class element
- BStart up the server with the optional -alternative command-line option, specifying any alternative
- CReorder the implementation names in the beans.xml file such that the desired alternative is listed
- DDelete the regular implementation class from the WAR.
How the community answered
(19 responses)- B84% (16)
- C11% (2)
- D5% (1)
Explanation
There appears to be an error in this question's answer key. Based on CDI (Contexts and Dependency Injection) specification, Option A is the correct answer, not B.
Why A is correct: In CDI, you enable an alternative bean by annotating the alternative class with @Alternative, then declaring it in beans.xml using the <alternatives><class>com.example.MockImpl</class></alternatives> element. This is the standard, portable CDI mechanism for selecting an alternative implementation at deployment time.
Why the other options are wrong:
- B is incorrect - there is no standard
-alternativecommand-line option in Java EE/Jakarta EE servers. This option appears to be fabricated. - C is incorrect -
beans.xmlis not order-sensitive; reordering entries has no effect on which implementation CDI selects. - D is technically a workaround but is destructive and defeats the purpose of the alternatives mechanism, which is designed to let both classes coexist with one selectively enabled.
Memory tip: Think of CDI alternatives as a "feature flag in XML" - you always need two things: the @Alternative annotation on the class, and the <alternatives> entry in beans.xml to activate it per deployment. If you see an answer describing a command-line server flag for CDI alternatives, that's a red flag.
Recommendation: If this is from a practice exam or study guide, the answer key likely contains a typo - treat A as correct.
Topics
Community Discussion
No community discussion yet for this question.