Read the video learning materials about Java annotations Today, and make some notes:
To learn the purpose of Java annotations:
Can see other people's code, especially the framework code, because it is definitely related to annotations.
Programming is more concise and the code is clear.
Java annotations are introduced by java1.5: The annotation concept is a way and method that Java provides an element in the original program to correlate any information and metadata.
Common annotations (compile-time annotations);
@override: method overrides the method of its parent class
@Deprecated: This comment is a markup comment. The so-called tag annotations, which are added to the source program, do not affect the compilation of the program, but sometimes the compiler displays some warning messages. Or add a line to the method name, meaning it is recommended not to use
@SuppressWarnings (""): a warning for a variable or method in the code, and a potential risk to tell the method.
Annotation classification:
Classification by operating mechanism:
1, Source notes: Annotations only exist in the source code, compiled into a. class file will not exist.
2, compile-time annotations: exist in source code and. Class
3, runtime Annotations: The operation phase will not be bothered by annotations, or even affect the operation of the logic of the annotations.
Classification by Source:
1, annotations from the JDK:
2, annotations from third parties:
3, our own definition of annotations:
Another class called meta-annotations: annotations to annotations.
Common third-party annotations:
@autowired @Service @Repository in spring
@insertprovider @UpdateProvider @Options, etc. in the MyBatis
Custom annotations:
With @interface + annotation name
As the conclusion of a note named Description:
@Target ({elementtype.method, elementtype.type}) //@Targe refers to the scope of the annotation, Includes method declaration, type class interface, parameter parameter declaration, package packet declaration, local_variable local Variable Declaration, Field field declaration, and constructor constructor method declaration
@Retention ( Retentionpolicy.runtime) //@Retenion refers to the life cycle: 1, Source notes: Annotations only exist in the source code, compiled into a. class file will not exist. 2, compile-time annotations: There are 3 in the source code and. class, runtime annotations: It is not a headache to note at run time, even the annotations that affect the running logic.
@Inherited //@Inherited means allow subclass inheritance
@Documented//@Documented include annotation information when Java doc is generated
//above 4 behavioral element annotations
Public @ Interface Description {
/* The member type in the annotation is restricted: the Legal class (int Double, etc.) includes the original type and string,class,annotation,enumeration, such as
* If the annotation has only one member, The member name must be named value (), and the member name and assignment symbol (=) can be ignored when using annotations
* Annotation classes can have no members, annotations without members are called identity annotations ****/
String desc ();//the member (member variable) in the annotation is declared with no arguments and no exception
String author ();
int age () default 18;//you can specify a default value for a member
}
with default the following is a small example of how annotations are manipulated:
@Description (author = "Child", desc = "Class annotation on child")//using annotations::@< annotation name > (member name 1= value 1, member name 2= value 2 ...) using annotation syntax. Multiple members are separated by commas, and are used according to the usage range specified by the @Target
public class child{
@Description (author = "LDM", desc = "Name () method annotation in child")
Public String name () {
TODO auto-generated Method Stub
return null;
}
@Description (author = "Child", desc = "Number () method annotation in child")
Public String number () {
return "100";
}
}
public static void Main (string[] args) {
First load class with class loader
try {
Class C = class.forname ("Com.ldm.test.Child");
Find the annotations above the class
Boolean isexist = C.isannotationpresent (Description.class);//determine if annotations exist
if (isexist) {
Get an annotation instance
Description d = (Description) c.getannotation (Description.class);
System.out.println (D.desc ());
}
Find annotations on a method
Method[] Ms=c.getmethods ();
for (Method m:ms) {
Boolean ismexist = M.isannotationpresent (Description.class);//determine if annotations exist
if (ismexist) {
Get an annotation instance
Description d = (Description) m.getannotation (Description.class);
System.out.println (D.desc ());
}
}
/**********************************************/
Another kind of analytic method
for (Method m:ms) {
Annotation []as=m.getannotations ();
for (Annotation A:As) {
if (a instanceof Description) {
Description d= (Description) A;
System.out.println (D.desc ());
}
}
}
}
catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
----------------Console Print Results------------------------------------------
class annotations on child
The name () method annotation in child
Note for number () method in child
The name () method annotation in child
Note for number () method in child
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Annotations Learning Notes