Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disposing objects in CDI

Tags:

jakarta-ee

cdi

I was wondering if CDI had some way to dispose/close objects other than @Disposes methods?

For simple cases, I would like to do something like:

class MyObject {

    @Inject MyObject(MyDependencies...) {...}

    @Disposes void close() {...}
}

rather than having to create a @Disposes method elsewhere just to call close() like this:

class MyObjectManager {

    void closeMyObject(@Disposes MyObject myObject) {
        myObject.close();
    }
}
like image 673
Xavier Dury Avatar asked Apr 26 '26 23:04

Xavier Dury


1 Answers

I think what you're looking for is @PreDestroy. This method gets called when whatever context the object is tied to is closed. Annotate a method with this and that method will be invoked whenever the object is meant to be destroyed.

like image 200
John Ament Avatar answered Apr 30 '26 01:04

John Ament



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!