Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Annotations vs XML, advantages and disadvantages [duplicate]

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

like image 998
user2455129 Avatar asked Jun 13 '13 11:06

user2455129


People also ask

Is XML better than annotation?

The biggest disadvantage of XML is indeed its size. Multiple lines are needed for things that you can do in one simple annotation. They are far away from the part in the code where it applies, whereas annotations are just on top of them. So annotations seem a lot more logical and simpler.

Why we use annotations if we have already XML approach?

XML configurations can take a lot of work when there are a lot of beans, while annotations minimize the XML configurations. Annotation injection is performed before XML injection. Thus, XML configurations will override that of the annotations.

What are the advantages of annotations in Java?

The benefits of type annotations and example use cases For instance, they can produce informational messages for the developer at compile time, detecting errors or suppressing warnings. In addition, annotations can be processed to generate Java source files or resources that can be used to modify annotated code.

Which is better XML or Java configuration?

what is easy to understand? Obviously, the Java-based config is easy to understand. people who write codes and others can easily understand java code than XML. and you do not need to know about XML if you only know Java.


1 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)

like image 194
VirtualTroll Avatar answered Sep 30 '22 10:09

VirtualTroll