Spring Bean Init and Destroy method Configuration Example


In Spring, you can use init-method and destroy-method as attribute in bean configuration file for bean to perform certain actions upon initialization and destruction 

Beans can be initilized by the factory before its first use

public class ExampleBean {
      public void init () {
           // do some initialization work
       }
  }

<bean id="exampleBean" class="ExampleBean" init-method="init"/>


Beans can be cleaned up when not used any more

public class ExampleBean {
      public void cleanup () {
           // do some destruction work
       }
  }

<bean id="exampleBean" class="ExampleBean" destroy-method="cleanup"/>

Search:

Recent Posts