Read the video learning materials about Java annotations today. Make some notes:
To learn the purpose of Java annotations:
Can see other people's code, especially the framework code. Because it must be related to annotations.
Programming is more concise and the code is clear.
Java annotations are introduced by java1.5: annotation concepts are a way for Java to provide an element in the original program, regardless of information and metadata.
Common annotations (compile-time annotations);
@override: method overrides the method of its parent class
@Deprecated: This gaze is a mark of gaze.
So-called mark gazing. The addition of this tag in the source program does 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 according to the implementation mechanism:
1, source code annotations: Annotations only exist in the source code, compiled into a. class file will not exist.
2, compile-time annotations: exists in both source code and. Class
3. Annotations at execution time: annotations that do not work at the execution stage, or even annotations that perform logic.
Classification according to Source:
1. Annotations from the JDK:
2. Annotations from third parties:
3. Our custom 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
Define your own 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, Contains the method declaration, the type class interface, and the parameter parameter declaration. Package declaration, Local_variable local variable Declaration, field fields declaration, and constructor constructor method declaration
@Retention (retentionpolicy.runtime) @Retenion refers to the life cycle: 1. Source code annotations: annotations exist only in the source code. Compiled into a. class file does not exist. 2, compile-time annotations: In both the source code and. Class, there are 3, execution annotations: annotations that do not work during the execution phase, or even annotations that perform logic.
@Inherited //@Inherited refers to consent 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.) contains the original type and string,class,annotation,enumeration, such as
* The annotation class can have no members. Annotations that do not have a member are called identity Annotations ****/
String desc ();//Members (member variables) in annotations declare
String author ();
int age () default 18;//ability to specify a default value for a member
}
The following is a small sample that operates on annotations:
@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, using the 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);//inferred annotations are present
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);//inferred annotations are present
if (ismexist) {
Get an annotation instance
Description d = (Description) m.getannotation (Description.class);
System.out.println (D.desc ());
}
}
/**********************************************/
The second method of parsing
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
Java Annotations Learning Notes