JBoss Seam is using lot of annotations and very few XML for reasons that i will explain here.
First i thought of calling this blog entry "annotations vs XML" but it isn't fair. It's not about who is the winner but when it is more appropriate plus it is often an endless question like "Linux vs Windows" or "XBox 360 vs PS3" (it is good to have both).
I love XML
Back in 2001 my first real job at NIST was helping with defining, then implementing a RI and finally start building a test framework for the registry and repository part of ebXML. ebXML is supposed to be adapted to work for any kind of business and we worked on two different demonstrations one for motherboard manufacturers repositories and one for e-Learning courses repositories. It made lot of sense to use XML as metadata for those things, XML is really cool, i can read and write it with any editor, i can parse it really easily with those libraries available out there. Most important i can add metadata about images and other binary documents since i am using an external file just referencing the embedded binary or filename.
For all those reasons XML was totally appropriate
Now i want to add metadata on a java source file.
Back to the old <not so> good time
Back in the old not so simple EJB*2* time, i had to write my java file, then go tweak a file which i never remembered the name and the syntax, even worse i had to switch file everytime i was writing a method and in Eclipse it's not as simple to switch file as in emacs ! I often messed up with method names, typos became nightmares, but here came XDoclet ! I don't have to write my classname and method names twice, i don't have to learn the XML syntax, i just have to go on the XDoclet website and look through the cheatsheets, thank you God Xdoclet, i don't have to maintain parallel files anymore and i save *a lot of time* thanks to you.
The drawback is that i had to learn that again to explain to JBoss students how to do the traditional way, when i explained XDoclet method afterward, i was about to get killed for not explaining that at first.
Now that Java imported the Xdoclet basic idea right into JSE 5 by using annotations (aka JSR 175) it is even easier.
There are now lot of things that i want to declare on my POJOs, not only what kind of beans they are but also their Seam name, i probably want to use Hibernate annotations to specify some validation and so on. I will loose time if i have to switch to an XML file specify the classname, the method name and the validation furthermore i will probably do typos and realize it on deployment time and finally if i change my method name, my IDE refactoring super-feature will or will not change it in the XML file.
It makes lot of sense to have an email validator on my email field, why would i declare this outside of my POJO. There is just no reason for that.
The only good point i see in this case for outside metadata is when you want to keep the same POJO for different clients and they have different requirements (and i know they all have) in terms of password and username length for example.
Conclusion, I love annotations even more
XML forces people to be aware of the connection between a class file and it's file descriptor and to keep them synchronized, while it looks like a little work it appears to be a big time consumer for most of us. In my opinion in a 1-1 relationship between a java class element and it's metadata, annotation is a much nicer way to achieve the goal of marking a method or a field.Thomas Heute - JBoss Seam project leader