How Spring customizes annotations
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