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"/>



Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email