javaspring

Is it recommended to use the Spring's predefined InitializingBean and DisposableBean callbacks or not?


Is it recommended to use the Spring's predefined InitializingBean and DisposableBean callbacks or own init-method and destroy-method for initialization and cleanup.

I just started learning Spring Framework and I confused with the follwing statements that,

  1. It is recommended that you do not use the InitializingBean or DisposableBean callbacks, because XML configuration gives much flexibility in terms of naming your method. (From a Tutorial Blog)

  2. It is recommended to use Spring's predefined InitializingBean or DisposableBean callbacks. (From my Tutor)

Are both valid based on the scenario?


Solution

  • The first one doesn't force your class to implement a Spring-proprietary interface, but recommends using XML to configure your beans. XML is, quite frankly, awful, and Spring has provided Java configurations or simple annotations for a long time, that allows removing the need for verbose, cumbersome, unsafe XML configuration.

    I would simply use standard PostConstruct and PreDestroy annotations for this.