I understand that spring’s lifecycle callback init-method is useful when you need to do initializations based on the dependencies injected by the IoC framework that can’t be done in the regular contructor method. But what is the advantage of doing clean ups in the lifecyle callback destroy-method over regular finalize method?
I understand that spring’s lifecycle callback init-method is useful when you need to do
Share
Others have provided good answers, but I thought I’d add a piece of explanation on the
finalize()method.As a good practice, you should not put application critical code in the
finalize()method, as there’s no guarantee when it will be called (or if it will be called). You can put some last-ditch effort code in there to make sure resources used by a class are freed (files, or communication ports for instance), but you should never rely on this method to do any required cleanup.Whenever you have a mechanism in place to destroy objects (such as the destroy callback in the Spring framework), you should definitely use it to free resources.