Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

While learning Spring framework, I found that Spring framework provides init-method and destroy-methods which can be used to initialize some properties or to do housekeeping stuff.

We can do all those things in the bean's constructor so why do Spring provides these things and in the real world what is the actual use of these two things?

share|improve this question
up vote 4 down vote accepted

When you use setter-based dependency injection, the init-method makes it possible to perform initialization: the specified method will be called after all the setters have been called.

As per destroy-method, it seems that implementing finalize in Java classes is not a good practice, instead, and many libraries define a close method which may or may not be called from finalize. See this discussion:

EDIT (to clarify things even further)

In setter-based injection methods will be called in the following order:

  1. default constructor
  2. the setters corresponding to each <property> of the bean
  3. the init-method, if specified

In constructor-based injection methods will be called in the following order:

  1. the matching constructor
  2. the init-method, if specified
share|improve this answer
shoot.... I forgot about setter based injection. In this case 'init-method' is really helpful. Thanks a lot for your answer. – Shekhar May 23 '12 at 2:25
In case of constructor dependency injection, is init-method performs the initialization or constructor itself does? – Satish Pandey Aug 31 '12 at 5:44
The constructor will be invoked first, and then the init-method (if specified). See my updated answer for more details. – janos Aug 31 '12 at 7:39

The argument is one that continually happens in spring/java circles. Here's a few blog posts which illuminate most of the important points:

Primary arguments for constructor injection:

  1. Constructor injection ensures all mandatory properties have been satisfied, and it is simply not possible to instantiate an object in an invalid state
  2. Enforces the order of initialization and prevents circular dependencies
share|improve this answer
Circular dependencies is a key benefit of setter injection. From the humble back-pointer and beyond. – MebAlone Aug 31 '12 at 8:41

For setter injection of XML configured beans, the init-method gives you the opportunity to check the state of your object before any methods get called. Remember there is no compile-time check that an argument is passed to initialize each field of the object. So even though a setter may be defined in the java class, the bean definition in the XML could be missing a property tag.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.