Teach you how to customize spring boot annotations (including code)

Director Li 2022-05-22 12:40:06 阅读数:726

teachcustomizespringbootannotations
 source :moon Talk about technology https://mp.weixin.qq.com/s/s-B8i1wyJESqvrKp10ytQg
  • Girl friend : I want my own annotation , You teach me !
  • moon : EH ? Why do you suddenly want your own annotation ?
  • Girl friend : It's none of your business! !「 Break up 」!
  • moon : No, no, no, no ! I teach !
  • moon : Look good. , My treasure ~, you spring Good learning , Let me show you around Autowired Well ~
@Target({
ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {

/**
* Declares whether the annotated dependency is required.
* <p>Defaults to {
@code true}.
*/
boolean required() default true;
}
  • moon : notice 「Autowired」 Find out , This class of 「 The class name is Autowired」, So you know why it's @Autowired Did you? ?

  • Girl friend : Oh, oh, oh, oh ! I see ! original 「 The class name is the annotation name 」!

  • moon: My girlfriend is smart ! Let's see , It also has a special place , The flag of the class is class, and 「 The mark of the annotation is @interface」.

  • Girl friend : Um. … Good good , You go on

  • moon: Let's see @Autowired There are three notes on it , What are the roles of each , Let's take a look at the first one 「@Documented」

/**
* Indicates that annotations with a type are to be documented by javadoc
* and similar tools by default. This type should be used to annotate the
* declarations of types whose annotations affect the use of annotated
* elements by their clients. If a type declaration is annotated with
* Documented, its annotations become part of the public API
* of the annotated elements.
*
* @author Joshua Bloch
* @since 1.5
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {

}
  • moon: see , We found out , The first is @Documented, Let's see what its annotation is ?

 picture

  • moon: Through my strong English reading ability , Find out 「@Documented Annotations are actually just used to generate documents 」, Use javadoc You can generate api Document. , So this annotation , sure 「 Is not important 」

  • Girl friend : Bah ! You obviously rely on translation ! Study slacker !

  • moon: Hey , Let's look at the next !「@Retention」! There's something to say about this

 /**
* Indicates how long annotations with the annotated type are to
* be retained. If no Retention annotation is present on
* an annotation type declaration, the retention policy defaults to
* {
@code RetentionPolicy.CLASS}.
*
* <p>A Retention meta-annotation has effect only if the
* meta-annotated type is used directly for annotation. It has no
* effect if the meta-annotated type is used as a member type in
* another annotation type.
*
* @author Joshua Bloch
* @since 1.5
* @jls 9.6.3.2 @Retention
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {

/**
* Returns the retention policy.
* @return the retention policy
*/
RetentionPolicy value();
}
moon: Once again, through my strong English reading ability , What exactly does this note mean ?
picture
moon: In fact, it just tells you , It should be noted that 「 Life cycle 」 How long has it been , And the definition of this life cycle ,「 It's just RetentionPolicy Inside 」, Let's take a look at this RetentionPolicy What is it ?
public enum RetentionPolicy {

/**
* Annotations are to be discarded by the compiler. Official account :moon Talk about technology , Get more interesting articles
*/
SOURCE,
/**
* Annotations are to be recorded in the class file by the compiler
* but need not be retained by the VM at run time. This is the default
* behavior.
*/
CLASS,
/**
* Annotations are to be recorded in the class file by the compiler and
* retained by the VM at run time, so they may be read reflectively.
*
* @see java.lang.reflect.AnnotatedElement
*/
RUNTIME
}
  • Girl friend : I'm familiar with this !「SOURCE It means being applied to the source code ,CLASS It is used on the compiled source code ,RUNTIME It only works at runtime 」! Isn't that what Java The three states of !
  • moon: You have learned to answer my treasure !!!!
  • Girl friend : hum ! Go on !!
  • moon: Ha ha ha , well , Let's talk about the last comment 「@Target」
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {

/** * Returns an array of the kinds of elements an annotation type * can be applied to. * @return an array of the kinds of elements an annotation type * can be applied to */
ElementType[] value();
}
  • moon: The function of this annotation is actually very simple ,「 Is to tell you which scopes the annotation can be posted in 」, And what are the scopes, you know ?
  • Girl friend : Um. … Some kind 、 Method 、 Member variables …
  • moon: Ha ha ha ha ha ha ha , I don't know !!
  • Girl friend : hum !!「 Break up 」!!!!
  • moon: No, no, no, no, no , Let me tell you ! This scope is actually hidden in 「ElementType[]」 In this array , Let's go in and have a look !
public enum ElementType {

/** Class, interface (including annotation type), or enum declaration Official account :moon Talk about technology , Get more interesting articles */
TYPE,
/** Field declaration (includes enum constants) */
FIELD,
/** Method declaration */
METHOD,
/** Formal parameter declaration */
PARAMETER,
/** Constructor declaration */
CONSTRUCTOR,
/** Local variable declaration */
LOCAL_VARIABLE,
/** Annotation type declaration */
ANNOTATION_TYPE,
/** Package declaration */
PACKAGE,
/** * Type parameter declaration * * @since 1.8 */
TYPE_PARAMETER,
/** * Use of a type * * @since 1.8 */
TYPE_USE
}

moon: All in all 「10 Species scope 」

Scope meaning
TYPE Used to describe a class 、 Interface ( Including annotation types ) or enum Statement
FIELD Used to describe domain
METHOD For method
PARAMETER Used to describe parameters
CONSTRUCTOR Used to describe a constructor
LOCAL_VARIABLE Used to describe local variables
ANNOTATION_TYPE Used to describe annotations
PACKAGE Used to describe a package
TYPE_PARAMETER Indicates that the annotation can be used in custom type parameters
TYPE_USE Is a comment on the type

So when you determine the scope of your annotation , You stick @Target( Scope ), That's all right. !

  • Girl friend : Oh, oh, oh , I see , Then I have a question ,「 What should I do if I want my subclass to inherit this annotation 」?

  • moon:!!!!!!! That's what I'm going to talk about !!「@Inherited」 !! It's also java One of the four meta annotations ( The other three are just mentioned @Target,@Retention,@Documented)! Its function is to 「 Let the subclass also inherit the annotation of the parent class 」, Then you know how to use it ?

  • Girl friend : branch …

  • moon: Let me give you an example ! Just practice !

  • Girl friend : hum !

  • moon: Let's write an annotation class first

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {

/** * Say I love you ( Default true) */
boolean sayILoveYou() default true;
}
  • moon: This annotation is very simple ,「 Can only work on methods , Implement... At run time , There is one syaILoveYou Methods , The default is true!」

  • Girl friend :yue~ Say it quickly

  • moon: ha-ha , Define another me , There is one sayLoveYou() Method , Posted our @MyAnnotation annotation , Express my heart

public class Me {

@MyAnnotation
public void sayLoveYou(){

System.out.println(" Express my heart ");
}
}
  • Girl friend :yue~

  • moon: Okay , Now we're testing !

public class Main {

public static void main(String[] args) {

try {

// obtain Me Of Class object 
Me me = new Me();
Class clazz = me.getClass();
// Get the object sayLoveYou On the way Info Notes on type 
MyAnnotation myAnnotation = clazz.getMethod("sayLoveYou", null).getDeclaredAnnotation(MyAnnotation.class);
if (myAnnotation.sayILoveYou()) {

System.out.println(" I love you! ");
} else {

System.out.println(" I do not love you ");
}
} catch (Exception e) {

e.printStackTrace();
}
}
}
  • moon: We got... First Me The object of , And then we get MyAnnotation This annotation , If myAnnotation.sayILoveYou() by true, Will be output " I love you! "! If false, Will be output " I do not love you "!

  • Girl friend : You don't love me ,「 Let's break up 」

  • moon: Cough , Test test ~ Let's run and see , The result must be that I love you ! Because we default to true
     Insert picture description here

  • moon: Let's change the default value of the annotation , The result is for me EN Love you ( Full of desire to survive )

public class Me {

@MyAnnotation(sayILoveYou=false)
public void sayLoveYou(){

System.out.println(" Express my heart ");
}
}

 Insert picture description here

  • Girl friend : hum ~

  • moon: Let's try again @Inherited This annotation , Modify the MyAnnotation,「 add to @Inherited」, add 「 Add ElementType.TYPE And make it work on classes 」

@Retention(RetentionPolicy.RUNTIME)
@Target({
Ele,mentType.METHOD,ElementType.TYPE})
@Inherited
public @interface MyAnnotation {

/** * Say I love you ( Default true) */
boolean sayILoveYou() default true;
}
moon:Me This class is pasted on the class @MyAnnotation annotation
@MyAnnotation
public class Me {

public void sayLoveYou(){

System.out.println(" Express my heart ");
}
}
moon: Then if we have children
public class Child extends Me{

}
  • Girl friend : I won't marry you !

  • moon: Ha ha ha , Suppose , Let's rewrite Main Method

public static void main(String[] args) {

try {

// obtain child Of Class object 
Child child = new Child();
Class clazz = child.getClass();
// Get the object sayLoveYou On the way Info Notes on type 
MyAnnotation myAnnotation = (MyAnnotation) clazz.getAnnotation(MyAnnotation.class);
if (myAnnotation.sayILoveYou()) {

System.out.println(" I love you! ");
} else {

System.out.println(" I do not love you ");
}
} catch (Exception e) {

e.printStackTrace();
}
}
  • moon:「 We are now child The object is not @MyAnnotation Annotated , Just inherited me , But because we are Me Class posted @MyAnnotation annotation , And there are @Inherited annotation , therefore child It also has the function of this annotation , So the result must be that I love you !」

 Insert picture description here - moon: Now you will ! Annotation is so simple !

  • Girl friend : hum , You're still useful , I don't need you , You can leave now.

  • moon: Good boss !( Finally taught , I'm alive again )

How many times did you divide your hands , Did you count it ?

copyright:author[Director Li],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/142/202205211830181281.html