Java Dependency injection: XML or annotations

I can only speak from experience with Guice, but here’s my take. The short of it is that annotation-based configuration greatly reduces the amount you have to write to wire an application together and makes it easier to change what depends on what… often without even having to touch the configuration files themselves. It does this by making the most common cases absolutely trivial at the expense of making certain relatively rare cases slightly more difficult to handle.

I think it’s a problem to be too dogmatic about having classes have “no idea about injection”. There should be no reference to the injection container in the code of a class. I absolutely agree with that. However, we must be clear on one point: annotations are not code. By themselves, they change nothing about how a class behaves… you can still create an instance of a class with annotations as if they were not there at all. So you can stop using a DI container completely and leave the annotations there and there will be no problem whatsoever.

When you choose not to provide metadata hints about injection within a class (i.e. annotations), you are throwing away a valuable source of information on what dependencies that class requires. You are forced to either repeat that information elsewhere (in XML, say) or to rely on unreliable magic like autowiring which can lead to unexpected issues.

To address some of your specific questions:

It helps get rid of XML

Many things are bad about XML configuration.

  • It’s terribly verbose.
  • It isn’t type-safe without special tools.
  • It mandates the use of string identifiers. Again, not safe without special tool support.
  • Doesn’t take any advantage of the features of the language, requiring all kinds of ugly constructs to do what could be done with a simple method in code.

That said, I know a lot of people have been using XML for long enough that they are convinced that it is just fine and I don’t really expect to change their minds.

In many cases there is only one implementation for each interface

There is often only one implementation of each interface for a single configuration of an application (e.g. production). The point is that when starting up your application, you typically only need to bind an interface to a single implementation. It may then be used in many other components. With XML configuration, you have to tell every component that uses that interface to use this one particular binding of that interface (or “bean” if you like). With annotation-based configuration, you just declare the binding once and everything else is taken care of automatically. This is very significant, and dramatically reduces the amount of configuration you have to write. It also means that when you add a new dependency to a component, you often don’t have to change anything about your configuration at all!

That you have mock implementations of some interface is irrelevant. In unit tests you typically just create the mock and pass it in yourself… it’s unrelated to configuration. If you set up a full system for integration tests with certain interfaces using mocks instead… that doesn’t change anything. For the integration test run of the system, you’re still only using 1 implementation and you only have to configure that once.

XML: You can inject anything anywhere

You can do this easily in Guice and I imagine you can in CDI too. So it’s not like you’re absolutely prevented from doing this by using an annotation-based configuration system. That said, I’d venture to say that the majority of injected classes in the majority of applications are classes that you can add an @Inject to yourself if it isn’t already there. The existence of a lightweight standard Java library for annotations (JSR-330) makes it even easier for more libraries and frameworks to provide components with an @Inject annotated constructor in the future, too.

More than one implementation of an interface

Qualifiers are one solution to this, and in most cases should be just fine. However, in some cases you do want to do something where using a qualifier on a parameter in a particular injected class would not work… often because you want to have multiple instances of that class, each using a different interface implementation or instance. Guice solves this with something called PrivateModules. I don’t know what CDI offers in this regard. But again, this is a case that is in the minority and it’s not worth making the rest of your configuration suffer for it as long as you can handle it.

Leave a Comment