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.
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.