Wednesday, March 10, 2010

Annotations have been more successful than XML

Tagged with: , , ,
Monday, March 10, 2008, 1:25
This news item was posted in Notes category and has 2 Comments so far.

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
Share and Enjoy:
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • Wists
  • LinkedIn
  • Slashdot
  • Google Bookmarks
  • Reddit
  • StumbleUpon
  • email
  • Twitter

Related posts:

  1. too much annotation is also bad
  2. Clean database schema with inheritance
  3. hibernate native Id generator tip
  4. hibernate cache injection: missing…
  5. hibernate tip: smart id generator

You can leave a response, or trackback from your own site.

2 Responses to “Annotations have been more successful than XML”

  1. Willie Wheeler said on Thursday, March 13, 2008, 13:53

    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. altuure said on Friday, March 14, 2008, 6:51

    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

Leave a Reply