i am new to java here, I was reading about annotations and xml, personally I find out xml has lot of advantages like it can be put outside application, changes can be made without recompiling class files. If i use annotations if I need to make changes need to go to source code and recompiling should be done. If this is the case why should we use annotations
-
Sounds like one of those questions where worlds, or rather opinions might collide. – Scorpio Jun 13 '13 at 11:59
3 Answers
Advantages of the annotation:
1) All the information is in a single file (no need to open two files to configure a given behavior)
2) When the class changes, no need to modify the xml file
Advantages of xml file:
1) Clear separation between the POJO and its behavior
2) When you do not know which POJO is responsible for the behavior, it is easier to find that POJO (searching in a subset of files rather than all the source code)

- 3,077
- 1
- 30
- 47
-
1
-
@Scorpio : You are right, i am going to edit my answer to reflect a subset of files rather than all files – VirtualTroll Jun 13 '13 at 12:17
First of all we use annotations for many more things, than just configuration.
Now: Some advantages of using annotations for configuration
Readability. For example in JPA configuration its much more cleaner to declare new entities by Annotations instead of hbm.xml files. These things change only in development stage so there is no problem with recompiling code. When You use xml files You have to often open both- entity and hbm file to make changes.. That can cause some errors.
Flexibility. In XML files you have to write all configs in "only one proper way". It is advantage and disadvantage at the same time.
Length. XML-based configs are often very long (like pom's, or hbm's). Annotations are much simpler to use.

- 10,528
- 6
- 54
- 53

- 236
- 1
- 10
The question is actually a difficult one to answer in a short answer.
Basically, there are pros and cons to all forms of configuration. Annotations, xml or Java Based Configuration. All the pros and cons are 100% valid. The main goal them becomes consistency. Making sure everyone on your project follows the same rules.
It is also NOT a question of either or, which one over the other. Because you can use any combination of configuration options in your application. One or all three combined. You just have to make your rules and stick to them
So my personal opinion is. Remember this is all my opinion and not fact.
1) Annotations over all other because I can configure and code much faster
2) Java Based configuration for those beans I can't annotate (They aren't my classes so I don't have the source to add an Annotation)
3) xml with what is left, or I need it complete externalized outside the package of my classes and I don't want to recompile and repackage. (Extremely rare, like it has never happened to me yet that I needed this)

- 816
- 4
- 11
- 26