From the course: Java EE: Contexts and Dependency Injection

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Disposer method for producers

Disposer method for producers

From the course: Java EE: Contexts and Dependency Injection

Start my 1-month free trial

Disposer method for producers

- [Instructor] You know how to use a producer method to generate an object that can be injected into a class. Well, a disposer method is used to do the opposite. It'll remove a produced bean when it's work is completed. The disposer method is always matched to a producer method. So a typical use case for a disposer method is to remove resources such as database connections. To create a disposer method, you annotate the parameter that is the same type as the instance you want to destroy and in the method, you write code that does whatever is necessary to destroy the resource. The parameter annotated disposer receives the object produced by the producer method. This method is called automatically when the context ends. So in this case, at the end of the HTTP session the clear array method is called and the array is cleared.

Contents