[Spring] custom annotation custom annotation

Custom annotation custom annotation

scenes to be used

  • Class property will be populated.
  • Verify the integrity of the object properties.
  • Instead of profile feature, like spring annotation-based configuration.
  • You can generate documentation, like java code annotation @ see, @ param etc.

First, cut way

  1. Parameter calibration implements ConstraintValidator
  2. HandlerInterceptorAdapter class implements HandlerInterceptorAdapter section, and into the container, required registry aspect class
  3. aop

 

Second, the parameter descriptions
  1, @ Target

  1.      Action: illustrates the modified target range Annotation: Annotation may be used for packages, types (classes, interfaces, enumerations, Annotation type), a member type (method, constructor, member variables enumerated value), and the method parameters local variables (e.g., variable loop, catch parameters). Use a target in the Annotation type declaration can be more clear that it modifies the target.
  2.      Value (ElementType) are:
  • CONSTRUCTOR: constructor is used to describe
  • FIELD: used to describe the field
  • LOCAL_VARIABLE: local variables used to describe the
  • METHOD: The method described for
  • PACKAGE: package used to describe
  • PARAMETER: parameters used to describe the
  • TYPE: used to describe a class, an interface (including type of annotation), or an enum declaration

 

2、@Documented

  1.       Action: @Documented display annotation information when generating javac

 

3、@Retention

  1.      Role: Define how long it is retained annotated notes, a total of three strategies, as defined in RetentionPolicy enumeration.
  2.      Value:
  •    SOURCE: being ignored by the compiler. In the compilation phase discarded, these notes after the end of the compiler will no longer have any meaning, they do not write byte code. @Override, @SuppressWarnings belong to such comments. So they are called source-level annotations. Automatic code generation had experienced developers, such as lombok, we know that it is part of the code is automatically generated at compile time by annotations, make the source code look cleaner, byte code is very powerful. 
  •     CLASS: Class notes will be retained in the files, but at run time and will not be VM reserved. This is the default behavior, all to no avail Retention annotation notes will adopt this strategy.
  •    RUNTIME: reserved to run. So we can go to get information through reflection comment.

 

4、@Inherited

  1.     Role: Allows annotation subclass inherits the parent class. If the annotation definition annotation acts on class A, B inheritance if A, then B class also inherits the annotation A

 

      github:https://github.com/Baker-abc/baker-learning-springboot/tree/master/learn-custom-annotation

Guess you like

Origin www.cnblogs.com/kbian/p/12028532.html