JDK comes with annotations
@Override methods that override the parent class
@Deprecated annotation A method means that the method is obsolete.
@Suppvisewarnings @SupressWarnings ("deprecation")//Ignore warning
Common third-party annotations
Spring:
@Autowired automatically generate an instance of a class
@Service
@Repository
Mybatis:
@InsertProvider
@UpdateProvider
@Options
Classification of annotations
Operation mechanism of the sub-
Source note Annotations only appear in the source code class file will not be
Compilation annotations exist in both the source code and the. class file
Running the annotation run phase affects the running logic of the program
Sources to divide
JDK annotations
Third-party annotations
Custom annotations
Annotations of meta-annotation annotations
Custom annotations
Syntax requirements
Meta annotations
@Target (Elementtype.method,elementtype.type)//Annotated scope can be used in constructor constructor method declaration Field field declaration local_variable local variable method declaration Package declaration parameter parameter declaration type class interface
The @Retention (retentionpolicy.runtime) life cycle source displays compile-time-discarded class compilation When the source code is compiled, and the runtime ignores runtime presence, which can be obtained by reflection
@Inherited allow subclasses to inherit inheritance only the annotations above the class are inherited will not inherit annotations on the method
@Documented generate Javadoc contains annotation information
Public @interface description{
String desc ();
String author ();
int age () default 18;
}
Defining annotations with the @interface keyword
Member declared without argument or exception
You can specify default values with Defalut members
The member types include the original type and the String class Annotation enumeration
If the annotation has only one member, the member name must be named value (), and the member name and assignment number can be ignored when used =
Note Classes can have no members as identity annotations
Using annotations
@Description (desc= "GAC", autor= "Gac1", age=18)
Public String Eyecolor () {
return "Red";
}
Parsing annotations
To obtain the runtime annotation information on a class, function, or member by reflection, thus realizing the logic of dynamic control program running
public class parseann{
To get annotations on a class using the ClassLoader load class
try{
Class C = Class.forName ("Com.ann.test.Child");//This class uses a defined annotation
2. Find the annotations above the class
Boolean isexists = C.isannotationpresent (Description.class);
if (isexits) {
Get an annotation instance
Description d = (Description) c.getannotation (Description.class);
System.out.println (D.value ());
}
Find annotations on a method
Method[] ms = C.getmethods ();
for (Method m:ms) {
Boolean ismexits = M.isannotationpresent (Description.class);
if (ismexits) {
Description d = (Description) m.getannotation (Description.class);
System.out.println (D.value ());
}
}
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.value ());
}
}
}
}catch (ClassNotFoundException e) {
E.printstacktrace ();
}
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
A brief introduction to Java custom annotations and annotations