Annotation related
Common JDk annotations include:
- @Override is overridden to identify the method that overrides its parent class
- @SuppressWarnings suppress warnings, suppress warnings @ SuppressWarnings({"rawtypes", "unused", "degradation"})
- @Deprecated deprecated
- @PostConstruct
- @Functional interface (introduced by jdk8)
spring notes include:
- @Component instantiates the ordinary pojo into the spring container, which is equivalent to the pojo in the configuration file
- @Controller is used to label the control layer, which is equivalent to the action layer in struts
- @service
- @repository is used to label the data access layer
- @Autowired dependency injection
- @Value dynamically injects external values into beans, which can act on methods
- @Scope for example: @ Scope("prototype") public class A {}
- @Transactional transaction management, for example: @ Transactional(rollbackFor = {Exception.class}, propagation = Propagation.REQUIRED)
Media type annotation
- @Consumption, for example: @ consumption ({mediatype. Application_json}), http requests the format of transmitted content, and the value of content type in the request header, such as application/xm
- @Products, such as: @ consumers ({MediaType. Application_json}), @ RequestMapping, which provides the function of filtering according to MediaType. It will be mapped only when the content type or Accept header has some MediaType
Meta annotation
What is meta annotation: basic annotation, which can be applied to other annotations to explain other annotations. For example -- use range, annotation retention time range, annotation inheritance (whether the subclass inherits the annotation of the parent class), etc
@Retention survival time
Function of Reteniton annotation: describe the time range of annotation retention
public enum RetentionPolicy { SOURCE, // Source file retention CLASS, // Compile time retention, default RUNTIME // The annotation information can be obtained through reflection }
@Save Documented to javadoc
Related to documents. Its function is to include the elements in the annotation into Javadoc
@Target restricted usage scenario
Target means target, @ target specifies the place where the annotation is used. When an annotation is annotated by @ target, the annotation defines the application scenario.
By analogy to tags, the original tag is that you can post wherever you want, but due to the existence of @ Target, it can only be posted on methods, classes, method parameters and so on.
@Target has the following values:
- ElementType.ANNOTATION_TYPE comments
- ElementType.CONSTRUCTOR annotates the construction method
- ElementType.FIELD attribute annotation
- ElementType.LOCAL_VARIABLE annotates local variables
- ElementType.METHOD annotation
- ElementType.PACKAGE annotation
- ElementType.PARAMETER annotates the parameters in the method
- ElementType.TYPE annotates types, such as classes, interfaces, and enumerations
@Inherited annotation inheritance
Indicates that an annotation type is automatically inherited. If an Inherited meta-annotation is present on an annotation type declaration, and the user queries the annotation type on a class declaration, and the class declaration has no annotation for this type, then the class's superclass will automatically be queried for the annotation type. This process will be repeated until an annotation for this type is found, or the top of the class hierarchy (Object) is reached. If no superclass has an annotation for this type, then the query will indicate that the class in question has no such annotation. Note that this meta-annotation type has no effect if the annotated type is used to annotate anything other than a class. Note also that this meta-annotation only causes annotations to be inherited from superclasses; annotations on implemented interfaces have no effect.
Inherited means inheritance, but it does not mean that the annotation itself can be inherited, but that if a superclass is annotated by the annotation annotated by @ inherited (really detour, that is, @ inherited annotates B annotation and B annotates others), then if its subclass is not applied by any annotation, then this subclass inherits the annotation of the superclass.
It's more abstract. Code to explain
Custom annotation @ Inherited actual combat
@Repeatable repeat
Repeatable means repeatable@ Repeatable is only added in Java 1.8, so it is a new feature.
What kind of annotation will be applied many times? Usually, the value of annotation can take multiple values at the same time
For example, a person is both a programmer and a product manager, and he is also a painter.
@interface Persons { Person[] value(); } @Repeatable(Persons.class) @interface Person{ String role default ""; } @Person(role="artist") @Person(role="coder") @Person(role="PM") public class SuperMan{}
Custom annotation
What is annotation: in essence, it can be regarded as a special annotation. If its code is not parsed, it is no better than ordinary annotation.
Custom annotation, create a new @ interface class and add meta annotation
@Target(ElementType.TYPE) //Act on class @Retention(RetentionPolicy.RUNTIME) // Valid at runtime @Documented @Inherited // What happens to subclasses annotated with @ Huan? What happens after subclasses have other annotations? public @interface Huan { String color() default "red"; }
Test analysis notes:
AnnotationHuan class, using @ Huan annotation
package com.chuliuhuan.annotation; @Huan(color = "white") public class AnnotationHuan { }
Parsing AnnotationHuan class
package com.chuliuhuan.test; import com.chuliuhuan.annotation.AnnotationHuan; import com.chuliuhuan.annotation.Huan; import java.lang.annotation.Annotation; /** * @author Chu Liuhuan * @date 2021-06-01 8:33 */ public class AnnotationTest { public static void main(String[] args) { // How to annotate class is Huan annotation /* When the annotation is: @Target(ElementType.TYPE) @Retention(RetentionPolicy.CLASS) @Documented zcls.isAnnotationPresent(Huan.class) Return to flash @Retention(RetentionPolicy.CLASS) Change it to @ Retention(RetentionPolicy.RUNTIME) and you can't go in After searching the meaning of isAnnotationPresent, I found that the trial method of isAnnotationPresent is wrong: A.isAnnotationPresent(B.class); Whether note B is on this A. If yes, return true; If not, false is returned. Therefore, B.isAnnotationPresent(B.class); X */ /* // Error example Class zcls= Huan.class; if (zcls.isAnnotationPresent(Huan.class)) { // To strong annotation type Huan huan = (Huan) zcls.getAnnotation(Huan.class); System.out.println(huan.color()); } */ // Gets the Class object of AnnotationHuan Class clsz = AnnotationHuan.class; // Judge whether there are Huan annotations on the AnnotationHuan class if (clsz.isAnnotationPresent(Huan.class)) { //Gets the annotation of the Huan type on this class Huan huan = (Huan) clsz.getAnnotation(Huan.class); System.out.println(huan.color()); } /* //Returns the specified annotation getAnnotation //Determines whether the current element is modified by the specified annotation isAnnotationPresent //Return all comments getAnnotations */ } }
Custom annotation @ Inherited actual code verification
In order to understand the function of Inherited annotation more specifically, let's take a look at chestnuts
The modified annotation of the parent class has Inherited meta annotation, and the subclass has no annotation
// Parent class package com.chuliuhuan.annotation; @Huan(color = "white") public class AnnotationHuan { } // Subclass package com.chuliuhuan.annotation; public class AnnotationHuanSubclass extends AnnotationHuan{ } // Test the class to see whether the subclass of AnnotationHuanSubclass has Huan annotation package com.chuliuhuan.test; import com.chuliuhuan.annotation.AnnotationHuanSubclass; import com.chuliuhuan.annotation.Huan; /** * @author chuliuhuan * @date 2021-06-01 23:34 */ public class AnnotationHuanSubclassTest { public static void main(String[] args) { Class<AnnotationHuanSubclass> annotationHuanSubclassClass = AnnotationHuanSubclass.class; if (annotationHuanSubclassClass.isAnnotationPresent(Huan.class)) { System.out.println("AnnotationHuanSubclass On class Huan annotation"); }else{ System.out.println("AnnotationHuanSubclass Not on class Huan annotation==========NO============="); } } }
Console print results:
C:\Users\ha-ha\.jdks\adopt-openjdk-1.8.0_292\bin\java.exe ... AnnotationHuanSubclass On class Huan annotation Process finished with exit code 0
The modified annotation of the parent class has Inherited meta annotation, and the child class has any other annotation (other annotations of the same type)
Subclasses and superclasses use the same inherited annotation
// Add another comment package com.chuliuhuan.annotation; import java.lang.annotation.*; /** * @author chuliuhuan * @date 2021-06-01 23:42 */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited public @interface HuanOther { String color() default "blue"; } // Subclass annotations use this newly added annotation package com.chuliuhuan.annotation; /** * @author chuliuhuan * @date 2021-06-01 23:34 */ @HuanOther public class AnnotationHuanSubclass extends AnnotationHuan{ } // Test this annotation package com.chuliuhuan.test; import com.chuliuhuan.annotation.AnnotationHuanSubclass; import com.chuliuhuan.annotation.Huan; import com.chuliuhuan.annotation.HuanOther; /** * @author chuliuhuan * @date 2021-06-01 23:34 */ public class AnnotationHuanSubclassTest { public static void main(String[] args) { Class<AnnotationHuanSubclass> annotationHuanSubclassClass = AnnotationHuanSubclass.class; if (annotationHuanSubclassClass.isAnnotationPresent(Huan.class)) { System.out.println("AnnotationHuanSubclass On class Huan annotation"); }else{ System.out.println("AnnotationHuanSubclass Not on class Huan annotation==========NO============="); } if (annotationHuanSubclassClass.isAnnotationPresent(HuanOther.class)) { System.out.println("AnnotationHuanSubclass On class HuanOther annotation"); }else{ System.out.println("AnnotationHuanSubclass Not on class HuanOther annotation******************NO***************"); } } }
Console print results:
C:\Users\ha-ha\.jdks\adopt-openjdk-1.8.0_292\bin\java.exe ... AnnotationHuanSubclass On class Huan annotation AnnotationHuanSubclass On class HuanOther annotation Process finished with exit code 0
Subclasses also use this annotation
Subclasses and superclasses use the same inherited annotation
Custom annotation:package com.chuliuhuan.annotation; import java.lang.annotation.*; /** * @author chuliuhuan */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited public @interface Huan { String color() default "red"; int age() default 18; }
Parent class:
package com.chuliuhuan.annotation; @Huan(color = "white",age = 24) public class AnnotationHuan { }
Subclass AnnotationHuanSubclass Code:
//Use the same type of annotation and set the initial value for the annotation property package com.chuliuhuan.annotation; /** * @author chuliuhuan * @date 2021-06-01 23:34 */ @HuanOther @Huan(color = "111111111111111") public class AnnotationHuanSubclass extends AnnotationHuan{ }
Test class:
// Test class: package com.chuliuhuan.test; import com.chuliuhuan.annotation.AnnotationHuanSubclass; import com.chuliuhuan.annotation.Huan; import com.chuliuhuan.annotation.HuanOther; /** * @author chuliuhuan * @date 2021-06-01 23:34 */ public class AnnotationHuanSubclassTest { public static void main(String[] args) { Class<AnnotationHuanSubclass> annotationHuanSubclassClass = AnnotationHuanSubclass.class; if (annotationHuanSubclassClass.isAnnotationPresent(Huan.class)) { Huan annotation = annotationHuanSubclassClass.getAnnotation(Huan.class); System.out.println("color is ========>"+annotation.color()); System.out.println("age is ========>"+annotation.age()); System.out.println("AnnotationHuanSubclass On class Huan annotation"); }else{ System.out.println("AnnotationHuanSubclass Not on class Huan annotation==========NO============="); } if (annotationHuanSubclassClass.isAnnotationPresent(HuanOther.class)) { System.out.println("AnnotationHuanSubclass On class HuanOther annotation"); }else{ System.out.println("AnnotationHuanSubclass Not on class HuanOther annotation******************NO***************"); } } }
Unit test results:
C:\Users\ha-ha\.jdks\adopt-openjdk-1.8.0_292\bin\java.exe ... color is ========>111111111111111 age is ========>18 AnnotationHuanSubclass On class Huan annotation AnnotationHuanSubclass On class HuanOther annotation Process finished with exit code 0
Classes A and B are inheritance relationships (A is the parent class and B is the child class), and annotation C is modified by @ inherited meta annotation.
If A uses @ C and B does not, B will inherit all annotation information in A, for example: The parent class uses inherited annotations, while the child class does not
If A uses @ C and B also uses @ C, B does not inherit the annotation information in A, for example: Subclasses and superclasses use the same inherited annotation