title: Java Dependency injection: XML or annotations
body: <p>Annotations becoming popular. Spring-3 supports them. CDI depends on them heavily (I <em>can not</em> use CDI with out of annotations, right?)</p>
  
  <p>My question is <em>why</em>?</p>
  
  <p>I heard several issues:</p>
  
  <ol>
  <li><p>"It helps get rid of XML". But what is bad about xml? Dependencies are declarative by nature, and XML is very good for declarations (and very bad for imperative programming).
  With good IDE (like idea) it is very easy to edit and validate xml, is not it?</p></li>
  <li><p>"In many cases there is only one implementation for each interface". That is not true!
  Almost all interfaces in my system has mock implementation for tests. </p></li>
  </ol>
  
  <p>Any other issues?</p>
  
  <p>And now my pluses for XML:</p>
  
  <ol>
  <li><p>You can inject anything anywhere (not only code that has annotations)</p></li>
  <li><p>What should I do if I have several implementations of one interface? Use qualifiers? But it forces my class to know what kind of injection it needs.
  It is not good for design.</p></li>
  </ol>
  
  <p>XML based DI makes my code clear: each class has no idea about injection, so I can configure it and unit-test it in any way.</p>
  
  <p>What do you think?</p>