views:

467

answers:

3

Hello Everyone

When I look at Java frameworks like Hibernate, JPA, or Spring, I usually have the possibility to make my configuration via an xml-file or put annotations directly in my classes.

I am cosistently asking myself what way should I go.

When I use annotations, I have the class and its configuration together but with an xml I get a bigger picture of my system because I can see all class-configurations.

Annotations also get compiled in some way I guess and it should be quicker than parsing the xml, but on the other hand, if I want to change my configuration I have to recompile it instead of just changing the xml file (which might become even more handy for production environments on customer side).

So, when looking at my points, I tend to go the xml-way. But when looking at forums or tutorials usually annotations are used.

What are your pros and cons?

+12  A: 

A good rule of thumb: anything you can see yourself wanting to change without a full redeploy (e.g. tweaking performance parameters) should really go in something "soft-configurable" such as an XML file. Anything which is never realistically going to change - or only in the sort of situation where you'll be having to change the code anyway - can reasonably be in an annotation.

Ignore any ideas of performance differences between annotations and XML - unless your configuration is absolutely massive the difference will be insignificant.

Concentrate on flexibility and readability.

Jon Skeet
Thanks. I havent thought of using both at the same time.
lostiniceland
Annotations have the advantage of being attached to the code. So it's less likely that they will get out of sync than a separate file. Be sure whatever options are in a separate file are thoroughly covered with unit tests. That said, Your best bet is to head the advice above.
Chris Nava
Values that are different depending on the environment that is deployed to (QA vs. production) or that need to be supplied by the person doing the deployment (e.g. a database password) are also good candidates for an external configuration.
NamshubWriter
Also, with AOP, annotations act more like lisp-y macros. e.g. you see them in the code, they aren't hidden in an unrelated file.
KitsuneYMG
+3  A: 

If you're writing an API, then a word of warning: Annotations can leak out into your public interface which can be unbelievably infuriating.

I'm currently working with APIs where the API vendor has peppered his implementation with Spring MBean annotations, which suddenly means I have a dependency upon the Spring libraries, despite the possibility I might not need to use Spring at all :(

(Of course, if your API was an extension to Spring itself, this might be a valid assumption.)

butterchicken
Annotations do not cause a runtime dependency. See [this comment](http://stackoverflow.com/questions/2468390/dependency-on-springs-annotations/2473776#2473776) about it.
abhin4v
A: 

XML is more loosely coupled than java annotation

saikat