title: What are the benefits of dependency injection containers?
body: <p>I understand benefits of dependency injection itself. Let's take Spring for instance. I also understand benefits of other Spring featureslike AOP, helpers of different kinds, etc. I'm just wondering, what are the benefits of XML configuration such as:</p>
  
  <pre><code>&lt;bean id="Mary" class="foo.bar.Female"&gt;
    &lt;property name="age" value="23"/&gt;
  &lt;/bean&gt;
  &lt;bean id="John" class="foo.bar.Male"&gt;
    &lt;property name="girlfriend" ref="Mary"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>compared to plain old java code such as:</p>
  
  <pre><code>Female mary = new Female();
  mary.setAge(23);
  Male john = new Male();
  john.setGirlfriend(mary);
  </code></pre>
  
  <p>which is easier debugged, compile time checked and can be understood by anyone who knows only java.
  So what is the main purpose of a dependency injection framework? (or a piece of code that shows its benefits.)</p>
  
  <hr>
  
  <p><strong>UPDATE:</strong><br/>
  In case of</p>
  
  <pre><code>IService myService;// ...
  public void doSomething() {  
    myService.fetchData();
  }
  </code></pre>
  
  <p>How can IoC framework guess which implementation of myService I want to be injected if there is more than one? If there is only one implementation of given interface, and I let IoC container automatically decide to use it, it will be broken after a second implementation appears. And if there is intentionally only one possible implementation of an interface then you do not need to inject it.</p>
  
  <p>It would be really interesting to see small piece of configuration for IoC which shows it's benefits. I've been using Spring for a while and I can not provide such example. And I can show single lines which demonstrate benefits of hibernate, dwr, and other frameworks which I use.</p>
  
  <hr>
  
  <p><strong>UPDATE 2:</strong><br/>
  I realize that IoC configuration can be changed without recompiling. Is it really such a good idea? I can understand when someone wants to change DB credentials without recompiling - he may be not developer. In your practice, how often someone else other than developer changes IoC configuration? I think that for developers there is no effort to recompile that particular class instead of changing configuration. And for non-developer you would probably want to make his life easier and provide some simpler configuration file.</p>
  
  <hr>
  
  <p><strong>UPDATE 3:</strong><br/></p>
  
  <blockquote>
    <p>External configuration of mapping between interfaces and their concrete implementations </p>
  </blockquote>
  
  <p>What is so good in making it extenal? You don't make all your code external, while you definitely can - just place it in ClassName.java.txt file, read and compile manually on the fly - wow, you avoided recompiling. Why should compiling be avoided?!</p>
  
  <blockquote>
    <p>You save coding time because you provide mappings declaratively, not in a procedural code </p>
  </blockquote>
  
  <p>I understand that sometimes declarative approach saves time. For example, I declare only once a mapping between a bean property and a DB column and hibernate uses this mapping while loading, saving, building SQL based on HSQL, etc. This is where the declarative approach works. In case of Spring (in my example), declaration had more lines and had the same expressiveness as corresponding code. If there is an example when such declaration is shorter than code - I would like to see it.</p>
  
  <blockquote>
    <p>Inversion of Control principle allows for easy unit testing because you can replace real implementations with fake ones (like replacing SQL database with an in-memory one)</p>
  </blockquote>
  
  <p>I do understand inversion of control benefits (I prefer to call the design pattern discussed here as Dependency Injection, because IoC is more general - there are many kinds of control, and we are inverting only one of them - control of initialization). I was asking why someone ever needs something other than a programming language for it. I definitely can replace real implementations with fake ones using code. And this code will express same thing as configuration - it will just initialize fields with fake values.</p>
  
  <pre><code>mary = new FakeFemale();
  </code></pre>
  
  <p>I do understand benefits of DI. I do not understand what benefits are added by external XML configuration compared to configuring code that does the same. I do not think that compiling should be avoided - I compile every day and I'm still alive. I think configuration of DI is bad example of declarative approach. Declaration can be useful if is declared once AND is used many times in different ways - like hibernate cfg, where mapping between bean property and DB column is used for saving, loading, building search queries, etc. Spring DI configuration can be easily translated to configuring code, like in the beginning of this question, can it not? And it is used only for bean initialization, isn't it? Which means a declarative approach does not add anything here, does it?</p>
  
  <p>When I declare hibernate mapping, I just give hibernate some information, and it works based on it - I do not tell it what to do. In case of spring, my declaration tells spring exactly wht to do - so why declare it, why not just do it?</p>
  
  <hr>
  
  <p><strong>LAST UPDATE:</strong><br/>
  Guys, a lot of answers are telling me about dependency injection, which I KNOW IS GOOD.
  The question is about purpose of DI configuration instead of initializing code - I tend to think that initializing code is shorter and clearer.
  The only answer I got so far to my question, is that it avoids recompiling, when the configuration changes. I guess I should post another question, because it is a big secret for me, why compiling should be avoided in this case.</p>