Commonly used notes of spring series notes

  • 2021-07-16 02:26:46
  • OfStack

Preface

One of the core functions of Spring is IOC, which initializes and loads Bean into the container. How Bean is loaded into the container can be annotated by Spring or configured by Spring XML.

Spring annotation reduces the content of configuration files, makes it easier to manage, and can greatly improve the development efficiency by using annotations!

This article mainly makes annotated notes, lest I often forget ~

Give an demo first


package com.nuofankj.springdemo.resource;

import java.lang.annotation.*;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ResourceConfig {

}

Start with @ Target, check the source code directly here and add comments


public enum ElementType {
 /**  Class ,  Interface  ( Include annotation types ),  Or   Enumerate   Declaration  */
 TYPE,

 /**  Field declarations (including enumeration constants)  */
 FIELD,

 /**  Method declaration (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 
  */
 TYPE_PARAMETER,

 /**
  *  Types used 
  */
 TYPE_USE
}

Obviously the @ Target enumeration provides a simple categorization of where it might appear in an Java program.

Next, look at @ Retention, and look at the source code as well


public enum RetentionPolicy {

 /**
  *  Comments are retained only at the source level and ignored at compile time 
  */
 SOURCE,

 /**
  *  Comments will be recorded by the compiler in the class file but not required at run time JVM Keep it. This is the default behavior 
  */
 CLASS,

 /**
  * Comments will be recorded by the compiler in the class file and retained at runtime VM So it can be read backwards. 
  */
 RUNTIME
}

RetentionPolicy, a constant of enumeration type, describes various strategies for retaining annotations, that is, specifying how long annotations should be retained.

The last one is @ Documented, which indicates that the annotation javadoc is recorded. That is, if a type declaration is documented with annotations, its annotations become part 1 of the common API.

Summarize


Related articles: