As this site says, Spring provides to register shutdown callbacks. These can be used for shutting down the application gracefully and effectively freeing up the used resources.