What are some suitable uses for custom Java annotations?

Most of the articles I see in user annotations have clear examples of how to create and use them, but they offer mostly trivial problems that can be solved. Besides being a runtime template such as JUnit, these examples do not lead me to any idea of ​​how I can apply custom annotations to simplify the code.

I understand how to use them, but I'm interested in knowing any scenarios that I need to consider in order to create them. Are there general object-oriented templates where you can simplify the solution for using custom annotations?

+4
source share
1 answer

You use them when you want to either insert or retrieve something from an object through reflection.

If you are not writing some kind of framework, client, or reusable code, this is usually not what you are going to do.

A concrete example would be an annotation in a Riak Java client .

For ORM (Relational Object Mapping), I created a complete set of annotations that allows the user to annotate their own classes so that they can simply say “Please save this in Riak” and pass in your own object. Annotations allow you to annotate a bucket, key, content type, vector clock, etc., And using reflection, I extract this information and create the corresponding wire protocol object, then send Riak.

Riak; /.

, - ORM , . . , JUnit - , , JSON-, (Gson, Jackson). Spring - .

+6

Source: https://habr.com/ru/post/1536531/


All Articles