Is the Spring prototype scope the same as the CDI dependant scope.
Googling lead me to blog posts that claimed they are the same and others that claimed that they are similar but not quite the same without explaining the differences.
So what are the differences between the spring prototype scope and the cdi dependant scope?
According to the CDI documentation and javadoc
When a bean is declared to have @Dependent scope:
- No injected instance of the bean is ever shared between multiple injection points.
- ...
Similarly, the Spring documentation states
The non-singleton, prototype scope of bean deployment results in the creation of a new bean instance every time a request for that specific bean is made.
They are, behaviorally, the same.
The only difference I could find is on the lifecycle of the bean. In Spring
Thus, although initialization lifecycle callback methods are called on all objects regardless of scope, in the case of prototypes, configured destruction lifecycle callbacks are not called. The client code must clean up prototype-scoped objects and release expensive resources that the prototype bean(s) are holding.
In CDI however the container manages the full lifecycle of the bean, either directly when it is injected as a method invocation argument or indirectly when destroying the bean it was injected into. The conditions are all described in the documentation linked.
As Luiggi mentions in the comments, it is important to note the default scope of a bean declaration. In the Spring docs state
The singleton scope is the default scope [...]
while in CDI, the default scope is dependent
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With