Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we need to destroy a CDI bean obtained programmatically?

I have obtained a CDI bean which was obtained programatically using the following code:

MyBean bean = CDI.current().select(MyBean.class, qualifier).get();

Once I am done, do I need to destroy this bean using

CDI.current().destroy (bean);

Or does the bean inherit the scope from my class?

like image 614
sudhir shakya Avatar asked Dec 09 '25 13:12

sudhir shakya


1 Answers

Or does the bean inherit the scope from my class?

Definitely no. It will have whatever scope you gave to MyBean.

do I need to destroy this bean

If your bean is normal scoped you don't need to do that. If, however, it is so called pseudo-scope, you might need to destroy it. For the record, normal scoped are all basic CDI scopes except for @Dependent.

The reason is that @Dependent lifecycle (so destroy as well) is bound to a bean where you inject it. But you didn't really inject it, instead you did programmatic lookup. Therefore, it isn't bound to any other bean and you should destroy it.

like image 190
Siliarus Avatar answered Dec 11 '25 03:12

Siliarus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!