Annotations have been more successful than XML

Annotations came to JDK with JDK5 (2004) since then they evaluated and are accepted so widely and even became more powerful and useful than XML in just 4 years.

While you are developing a project with java at any scale you’ll need a very large number of XML files defined in very different API’s and DTD/XSDs. Which makes configuration very complex.

eg:struts.xml,tiles.xml,validation.xml,hibernate.cfg.xml,spring-context.xml,web.xml,and so on

Although it is easy to read and modify there are lots files :(

Now with annotations:

Yes you still have lots of things/APIs to configure and you should still use XML, but you can now get rid of some of them and decrease the number of files in your repository.And bind multiple configuration files in just one file

Look at the sample:

Here, in a single file, it is possible to configure both JPA, hibernate and JAXB easily and in a human readable way.


import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlID;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Column;

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "IdentifiedType")
public abstract class IdentifiedType {
@Id
@Column(name="id")
@XmlAttribute
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlID
protected String id;

Of source do not forget other points:

  • since annotations are embedded configuration they are really fast
  • on the other hand they are one time configuration cannot be change at any time (beware)
  • annotations are not replacement of XML but complementary of them
  • Trackback are closed
  • Comments (2)
  1. I am a fan of annotations too, but I think your last point is key: annotations and XML are complements. Sometimes the advantages to distributing code outweigh the advantages to centralizing it and vice versa. Annotations give us a nice mechanism for choosing the former approach in those cases where it makes sense. I agree that good cases can be made for JPA, Hibernate, JAXB, and even other areas such as Spring transactions. Javadoc is another example where the distribution makes a lot of sense.

    But there are also examples where centralization makes sense. Consider properties files for message externalization (e.g., messages.properties and that sort of thing). People want to do this so they can easily internationalize their apps, or so they can easily review the messages for quality/usability, etc. The skill sets involved are more typically associated with a functional specialist than with a general developer. And so here it’s more sensible to have a single file where the functional specialist can go in and review the messages in one place.

  2. yes annotations are great because it bring/binds your java code and related api configuration in a single file

    Easy to maintain:easy to read (self documenting)

    but it DO NOT MEAN ‘XML sucks’ ,in fact it means use XML where it is necessary. eg: for interoperability

Comment are closed.