Home » design

Annotations vs. XML

19 November 2006 4 Comments

Since Java adopted annotations, I’ve heard a lot of discussion around whether to prefer annotations over external configuration files (often XML) and vice versa.

I think the decision boils down to two criteria:

1) Can annotations simplify the metadata?
2) Can changes to the metadata break behavior in your application?

If annotations do not reduce the amount of metadata that you have to provide (in most cases they do), then you shouldn’t use annotation.

I think the more important thing to considered is whether a configuration change could break behavior or not. If not, then you can feel comfortable applying the change while the system is running in production. External config files are the best place for the metadata in this case because you don’t want to have to recompile your code to make the change.

However, if a metadata change could break behavior then you’ll want to test the change by going through a full release cycle (change code, build, test, deploy). If you’re going to go through a full release cycle there is practically no benefit to not recompiling besides the 5-10 seconds you save in compile time. The more important thing to consider is which method of metadata definition is simpler.

For example, Hibernate mappings are often in XML, but annotations often provide the ability to specify the same mappings with significantly less metadata. Any changes you make to your mappings (whether in XML or annotations) could potentially be behavior breaking. You’re not going to change your mappings dynamically at runtime, are you? So, annotations seem like a much better choice.

[Note: I might consider using XML metadata for Hibernate when mapping to a legacy database because the annotations can often become bulky. Also, you are forced to use XML if you want to map the classes against two different databases with different schemas. I haven’t heard of a way to specify two mutually exclusive sets of Hibernate annotations on my classes. Even if this did exist it would be complex, which violates my first criterion for selecting annotations over XML.]


  • John said:

    Working on a Hibernate/Spring project right now. Got spring cfg in XML and Hib in Annotations.
    Recently started becoming concerned with the fact that my Pojo’s now know something about their persist store (tables/cols etc) and the imports now directly reference hibernate classes for the annotations – which is kinda ugly when in some environments I’m using them as pojo’s and in our unit test env where we mock there is no Hibernate.

    This leads me towards an all XML approach but I don’t find the proliferation of non-java artefacts very appealing either.

  • Mariusz said:


    consider another thing, those are not POJO’s in it’s strict sense, those are Entities!

  • Nuru said:

    >>I haven’t heard of a way to specify two mutually exclusive sets of Hibernate annotations on my classes.

    Read about Hibernate annotations. Not JPA

  • Vlad said:

    >Mariusz said:

    >consider another thing, those are not POJO’s in it’s strict sense, >those are Entities!

    Only if you want your application be tied to a database.

    E.g., imagine your POJOs could be persisted in your app’s database, and at some point your app got integrated with another system via web services, and could pull POJOs of the same type from that system.