Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8212017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:37:26+00:00 2026-06-07T10:37:26+00:00

I come from a .NET background, using dependency injection containers like Unity, Ninject, Castle

  • 0

I come from a .NET background, using dependency injection containers like Unity, Ninject, Castle Windsor, etc. Recently I have begun to learn to use Spring’s dependency injection capabilities for Java.

In learning Spring, I’ve seen that it’s possible to specify on a bean XML configuration the notion of an ‘init-method’ and ‘destroy-method’.

The purpose of specifying an ‘init-method’ appears to be to do any setup at bean creation time that you might want to do. Here’s where I get confused. Why would you need a separate method to perform setup, instead of just using the constructor to perform any setup needed by the object, like normal/good object oriented design would dictate?

In other words, if a dependency is required by a class, shouldn’t it be injected in the constructor, which you know has been called, whereas the object could exist in a state without having called it’s ‘init-method’ ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:37:28+00:00Added an answer on June 7, 2026 at 10:37 am

    There are few cases where separate init() method is needed:

    • legacy APIs where you just don’t have any choice

    • initialization having some side-effects, e.g. starting a Thread, connecting to som external resources

      This actually has even deeper implications: when using class-based proxies (via cglib) constructor of your base class is called twice (second time for the proxy that inhertis from your class) – init() method is called only once on final object.

    • you shouldn’t perform virtual calls in constructor (this should actually be forbidden by the compiler…)

    • sometimes you must use setter/field injection (although I love constructor injection), e.g. when aforementioned class-based proxies are used

    using the constructor to perform any setup needed by the object, like normal/good object oriented design would dictate?

    This isn’t actually the best practice to put all initialization code in constructor. Side-effects in constructor make testing and mocking much harder. Instead focus on creating objects in stable and known state. This way you can e.g. decouple creating an object managing the connection pool and physically connecting to that pool.

    BTW destroy() is a blessing in a language without destructors as it allows you to gratefully close external resources, interrupt threads, etc. Use it often.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am planning of using Specflow for .NET. I come from a RoR background,
I come from a C#/.NET 2.0 and WinForms background, but I'd really like to
I come from a .NET background and need to do a web project in
I come from a Java background and am getting more into .NET, what are
I'm just starting to dive into ASP.net MVC3. I come from a Django background.
I am building a Web Application using asp.net (C#). I come from windows forms
I come from a .NET background and will be going into the field of
I come from a .net background so the empty classes (models) that I'm seeing
I come from an ASP .Net background. I am now writing a Java program
I come from a .NET background and am completely new to Java and am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.