Does spring have a shutdown process to put cleanup code?

When my spring web app shuts down, is there an event I can wireup to somehow that I can perform some cleanup code to empty out some pools etc.


You Could use the following

  1. destroy-method as @amir75 recommends

  2. @PreDestroy annotation

  3. Implement DisposableBean and override destroy method.

All the deatails about these can be found at Disposable Callbacks.


Spring beans have a 'destroy-method' attribute, which will be invoked when you 'close' your context.

<bean id="bean1" 
    destroy-method="stop"
    class="com.example.Bean" />

In order to close it, you'd call the close() method:

http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/context/support/AbstractApplicationContext.html#close%28%29

(or just shut down the container if appropriate)

Hope that helps..


Based on the JSR-250 specification the best practice to use in modern spring application is the @PreDestroy annotation since using this approach will decouple your beans from Spring.