Results 1 to 5 of 5

Thread: Diff between constructor and spring init-method

  1. #1
    Join Date
    Jul 2012
    Posts
    2

    Default Diff between constructor and spring init-method

    Hi,
    I am newer to spring. I read Spring init-method is used to do some intial operation while the bean is initialized and destroy-method used to do some clean up operations before the bean object is about to destroy. This is same operations can be done in constructor and destructor of Bean's class. Then how the Spring's init-method and destroy-method differes from constructor and destructor?

    Thanks in Advance,
    Vinodh

  2. #2
    Join Date
    Mar 2007
    Posts
    561

    Default

    init is called after the constructor.
    There are no destructors in Java.

  3. #3
    Join Date
    Jul 2012
    Posts
    2

    Default Diff between constructor and spring init-method

    Hi,
    My question is, even though init is called after constructor, why can't we put the operations into constructor which ever we have in init.I know there is no destructor in java, but we have finalize method instead of that. How the destroy method of spring differs from finalize method. Can any one explain how these differs and in what scenario these would be useful.

    Thanks,
    Vinod

    Quote Originally Posted by brainvinod View Post
    Hi,
    I am newer to spring. I read Spring init-method is used to do some intial operation while the bean is initialized and destroy-method used to do some clean up operations before the bean object is about to destroy. This is same operations can be done in constructor and destructor of Bean's class. Then how the Spring's init-method and destroy-method differes from constructor and destructor?

    Thanks in Advance,
    Vinodh

  4. #4
    Join Date
    Mar 2007
    Posts
    561

    Default

    You can use the ctor or init, as you like.
    Finalizer are not guaranteed to be called. Do not use it, use destroy.

  5. #5

    Default

    Agreed. Finalizers are not guaranteed to be called by Java. But the "Spring Container" is guaranteed to call the destroy method.

    Jeff

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •