nerdexam
Oracle

1Z0-900 · Question #58

Given the code fragment: Assuming this bean is used only in the code fragment above, how long will the injected Bean instance be available?

The correct answer is D. for the lifetime of the Service object. Option D is correct because when a CDI bean is injected without an explicit scope annotation (defaulting to @Dependent), its lifecycle is tied to the object that contains it - here, the Service object. The injected bean is created when the Service is instantiated and destroyed…

Use CDI Beans

Question

Given the code fragment:

Assuming this bean is used only in the code fragment above, how long will the injected Bean instance be available?

Options

  • Afor the lifetime of the enterprise application
  • Bfor the lifetime of the request
  • Cfor the lifetime of the session
  • Dfor the lifetime of the Service object

How the community answered

(44 responses)
  • A
    14% (6)
  • B
    5% (2)
  • C
    2% (1)
  • D
    80% (35)

Explanation

Option D is correct because when a CDI bean is injected without an explicit scope annotation (defaulting to @Dependent), its lifecycle is tied to the object that contains it - here, the Service object. The injected bean is created when the Service is instantiated and destroyed when the Service is destroyed.

Option A (@ApplicationScoped) would require the bean to be shared across the entire application with a single instance - that would need an explicit @ApplicationScoped annotation. Option B (@RequestScoped) ties a bean to a single HTTP request, which also requires explicit @RequestScoped annotation and isn't the default. Option C (@SessionScoped) binds the bean to a user's HTTP session, again requiring explicit annotation.

Memory tip: Think of @Dependent (the default CDI scope) as "I belong to whoever created me." No scope annotation = dependent on the injecting object's lifetime - so the injected bean lives and dies with its owner (Service).

Topics

#CDI scopes#dependency injection#bean lifecycle#scope annotations

Community Discussion

No community discussion yet for this question.

Full 1Z0-900 Practice