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();
}
}
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.
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