Get the App
SLTechnology News&Howtos  ›  Development  › 

How Spring customizes annotations

Shulou Source: shulou.com Published: 2022-06-03 05:31:18 11月19日 Update

This article will explain in detail how to customize the annotations in Spring. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Field annotation

Field annotations are generally used to verify whether fields meet the requirements. Hibernate-validate dependencies provide a lot of verification annotations, such as @ NotNull, @ Range, etc., but these annotations are not suitable for all business scenarios.

For example, if we want the passed parameters to be in the specified String collection, then the existing annotations cannot meet the requirements and need to be implemented by ourselves.

Custom annotation

Define a @ Check annotation and declare an annotation through @ interface

@ Target ({ElementType.FIELD}) / / only allowed to be used on fields of a class @ Retention (RetentionPolicy.RUNTIME) / / annotations can be retained during the run of the program, when all annotations defined on a class @ Constraint (validatedBy = ParamConstraintValidated.class) public @ interface Check {/ * * legal parameter values * * / String [] paramValues () can be obtained by reflection. / * * prompt * * / String message () default "parameter is not specified"; Class [] groups () default {}; Class

Tags: Annotations methods parameters permissions fields caching processing scenarios that is data information articles types interceptors validators tests validation legality business aspects Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology macOS Redmi OPPO Reno Huawei