简体   繁体   中英

How do Annotations works internally in Java or any other programming language?

I having hard time understanding importance and benefits of Annotations and so have two questions regarding them:

  1. What are the benefits of Annotations as compared to XML Configuration?
  2. How do Annotations work internally?
  3. Is it fair enough to say that annotation binds application tightly whereas with XML Configuration Application is loosely coupled?

Would appreciate pros and cons comparison with XML Configuration with example so that it would be much more helpful for me to understand.

Regards.

For your 1st question,

Personally, I feel, there are two criteria's

  1. Can annotations simplify the metadata?

    If annotations do not reduce the amount of metadata that you have to provide (in most cases they do), then you shouldn't use annotation.

  2. Can changes to the metadata break behavior in your application?

    If not, then you can feel comfortable applying the change while the system is running in production. External config files are the best place for the metadata in this case because you don't want to have to recompile your code to make the change.

For your 2nd question,


Important Links:

To answer the first question, IMO the greatest benefit is the potential for compiler integration. I can write an annotation processor that can validate some semantics related to the application of the annotation. That kind of compile-time checking is not possible (or would at least be way more difficult) if the same information was instead part of an XML document.

To answer the second question, annotations don't really "work" internally, per se, in the sense that they don't have any inherent execution semantics. They are source level entities that may or may not be retained in the classfile. The can be processed during compilation of the source, and if they were retained in the classfile, can be accessed via reflection.

Both annotations and XML descriptors are used to describe some metadata on top of regular code. The primary difference is that in case of annotations you only have to deal with one file which includes code and metadata. It is also a big advantage of annotations as it reduces number of moving parts and increases productivity.

On the other hand, the drawback of annotations is that they bind together the code and the system or framework that operates using those annotations. That makes it harder to separate those in future.

For example, if you use Hibernate Annotations, you bind your model objects with Hibernate. If you choose to switch to different framework, you will have to rip out Hibernate annotations from the code.

But practically, it's not that likely that you will be changing frameworks that often. There are usually many other reasons why changing framework on existing code base may be hard. So often annotations is a good choice.

As to how they work, annotations are a part of the language and are processed by compiler and other tools and, depending on retention, can be included in produced bytecode for use at runtime. Ultimately, it's up to consumer to decide on how to use annotations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM