What is @Dependent Scope?

In CDI, classes annotated with @Dependent are "pseudo-scoped", what means:

"No injected instance of the bean is ever shared between multiple injection points"
Multiple instances can exist at the same time
"Any instance of the bean injected into an object that is being created by the container is bound to the lifecycle of the newly created object."
A "POJO" declared with @Dependent annotation and injected into a e.g. @Stateless or @RequestScoped bean has the same lifecycle as the corresponding bean.
"Any instance of the bean that receives a producer method, producer field, disposer method or observer method invocation exists to service that invocation only."
A @Dependent only lives as long, as the e.g. @Produces invocation takes
"Any instance of the bean injected into method parameters of a disposer method or observer method exists to service the method invocation only."
An as e.g. parameter injected @Dependent bean lives as long as the method invocation takes.

Because there is 1:1 relation between the @Dependent bean and the host instance, proxies are not needed in the standard case. The @Dependent instance can be directly injected by the container.

[See CDI spec]

Good news: the dependent scope is the default scope, if no scope is defined (if a class is not annotated). Following the "Convention over Configuration" principle, you don't have to declare the @Dependent annotation. All not annotated classes, or "POJOs", are dependent-scoped.

In an ECB/BCE application, the boundary is annotated with @Stateless and all other control instances come as vanilla POJOs without any annotations. They are dependent-objects.

See you at Java EE Workshops at MUC Airport, particularly at the Java EE Architectures workshop

Cloudy Jakarta EE and MicroProfile: Microservices, Clouds and Beyond Jakarta EE / MicroProfile airhacks workshops at MUC airport, Winter Edition

airhacks.fm the podcast:

Stay in touch: airhacks.news.


Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments