nerdexam
Oracle

1Z0-900 · Question #34

Given the code sample: And: And: And the next fragment from beans.xml: And this injection point: @Inject GenericMessenger messageHandler; Which type would be injected at run time and referenced by…

The correct answer is D. GenericMessenger. Option D is correct because in CDI (Contexts and Dependency Injection), @Alternative beans must be explicitly activated in beans.xml to participate in injection. Based on the code fragments, SmsHandler and MsgHandler are annotated with @Alternative, but the beans.xml shown does…

Use CDI Beans

Question

Given the code sample:

And:

And:

And the next fragment from beans.xml:

And this injection point:

@Inject GenericMessenger messageHandler; Which type would be injected at run time and referenced by the messageHandler variable?

Options

  • ASmsHandler
  • BMsgHandler
  • CNone of the above. An exception would be thrown due to ambiguous bean references.
  • DGenericMessenger

How the community answered

(45 responses)
  • A
    4% (2)
  • B
    2% (1)
  • C
    7% (3)
  • D
    87% (39)

Explanation

Option D is correct because in CDI (Contexts and Dependency Injection), @Alternative beans must be explicitly activated in beans.xml to participate in injection. Based on the code fragments, SmsHandler and MsgHandler are annotated with @Alternative, but the beans.xml shown does not enable either of them under <alternatives>, leaving GenericMessenger itself as the sole eligible bean for the injection point.

Option A (SmsHandler) and Option B (MsgHandler) are wrong for the same reason: they are @Alternative beans that are declared but not activated, so CDI excludes them from consideration entirely. Option C is wrong because there is no ambiguity - alternatives that are not enabled in beans.xml are invisible to the CDI container, so only one candidate (GenericMessenger) remains.

Memory tip: Think of CDI alternatives as "opt-in substitutes" - declaring @Alternative on a class is like writing your name on a substitute list, but you only get called in if beans.xml activates you. If no one activates the substitutes, the original teacher (GenericMessenger) still runs the class.

Topics

#CDI injection#bean resolution#type-based selection#dependency injection

Community Discussion

No community discussion yet for this question.

Full 1Z0-900 Practice