Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the use of Spring custom annotations

Shulou Source: shulou.com Published: 2022-06-03 09:36:01 11月15日 Update

This article mainly introduces the use of Spring custom annotations, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

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 articles information types interceptors validators tests validation legality business aspects Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB OPPO Reno Shulou Tech Info Docker Shulou Technology