Methods for creating custom Java annotation classes

If you are already using java programming and any popular frameworks such as spring and hibernate, you should be very familiar with the use of annotations. When working with an existing framework, its annotations are usually enough. However, do you sometimes have the need to create your own annotations?

Not long ago, I found a reason to create an annotation myself, which is a project involving verifying common data stored in a variety of databases. Scene description

There are many databases in this service that store the same data, and they have different methods to keep the data updated The business had planned to integrate all these data into a master database to reduce the complexity of problems caused by multiple data sources

However, before the project starts, the business also needs to know how far the data is from being synchronized and make any necessary corrections to make it synchronized The first step is to create a report showing the common data of multiple databases, verify its value, and highlight the unqualified records Here is a brief summary of the needs at that time:

annotation

After a period of deliberation on requirements and some ideas, I decided to use annotations to drive the configuration of data comparison and report processing What we need is simple, highly flexible and scalable These annotations will be field level, and I like that the configuration will not be hidden in a file somewhere in the classpath In this way, you can directly view the annotation associated with the same field to know how it is handled

In the simplest case, an annotation is nothing more than a tag, that is, metadata that provides information and does not have a direct impact on the operation itself If you have been working in Java programming, you should be quite familiar with their use by now, but you may never have the need to create your own annotations To do this, you need to create a new type with Java type @ interface, which will contain elements that can specify metadata details

Here is an example from this project:

This is the main annotation that drives how the data comparison process works It contains the basic elements, which can meet most of the needs of data comparison between different data sources@ Reconfield can handle most of the requirements we expect except for more complex comparisons, which we will discuss later Most of these elements are described in the one-to-one comments in the code list, and it should be noted that there are several key comments on our @ reconfield

@The target C annotation allows you to specify which Java element your annotation should be used on The possible target type is annotation_ TYPE,CONSTRUCTOR,FIELD,LOCAL_ Variable, method, package, parameter and type In our @ reconfield annotation, he is assigned to the field level

@Retention C allows you to specify when annotations take effect Possible values are class, runtime, and source Because we will handle this annotation at runtime, that is the value we need to set

This data validation process will run a query for each database and map the results to entity beans that show all fields for a specific business record type Annotations on each field of the mapped data entity tell the processor how to perform data comparisons for specific fields and their values found in each database So let's look at a few examples to see how these annotations are applied to different data comparison configurations

In order to verify the existing values and match them exactly with those in each data source, you only need to provide a field ID and the tag of the field that will be displayed on the report

In order to show the values found in each data source without any data comparison, you may need to formulate the comparesources element and set its value to false

In order to verify the values found in the specified data source, not all, you may use elementsourcestocompare Using this tool will show all the found values, but only compare the values found in the data sources listed in the element In this way, we can handle some data scenarios that are not stored in every data source Reconsource is an enumeration type that contains data sources that can be used for comparison

Now that we have met our basic needs, we need to solve the problem of realizing the ability of specifying fields for complex data comparison To do this, we will create a second annotation to drive custom rule processing

It is very similar to the previous annotation. The biggest difference is that we specify a class in the @ reconcustomrule annotation, which can perform data comparison during reconstruction processing You can define only the classes that will be used, so that your processor can instantiate and initialize the classes you specify The class specified in this annotation will need to implement a general rule interface, which will be used by the rule processor to execute rules

Now let's look at an example of using this annotation

In this example, we use a custom rule, which will check whether the stock exchange is a United States, and if so, skip this data To do this, this rule will need to check the exchange country field in the same record

In the example here, we specify a parameter for the custom rule, which is a packet capacity For this special data comparison, the compared value cannot deviate more than 1000 By using the parameters that specify the package capacity, we can use different package capacities to apply the same set of custom rules to multiple fields The only disadvantage is that due to the nature of annotations, these parameters can only be static, so they cannot be modified dynamically

As you can see, we only use a few simple annotations to design a data validation report function for multi database scenarios with considerable flexibility In this special case, annotations drive the data comparison process, so we actually use annotations to calculate the mapped data entities found and process them directly

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>