Annotaton (note) Yes jdk5.0 after the introduction of the first many mainstream frameworks are supported annotations.
annotations are typically used to create documents, trace dependencies in code, and compile checks. exists with the @ Comment name
JDKthe annotations commonly used in the
@Override overriding methods in super-class
@Deprecated Deprecated Code
@suppressWarings Warning
There are also annotations that are used to create custom customizations specifically
Custom annotations
@ Target Application Note location: fields, methods, classes :
@retemtop usage level, (runtime,class,source)
@ Documente Is included in the Javadoc in
@Inherited allow subclasses to inherit annotations from parent class
To define an annotation purpose or to use, the most important part of using annotations is the processing of annotations, then the annotation processor is involved. in principle, the annotation processor obtains the annotation information on the inspected method through the reflection mechanism, and then carries out the specific processing according to the value of the annotation element.
According to the reflection mechanism we learned from the previous section
We do a definition and use of annotations
1 Defining annotation Classes
Importjava.lang.annotation.elementtype;importjava.lang.annotation.retention; Importjava.lang.annotation.retentionpolicy;importjava.lang.annotation.target; @Target (Elementtype.method) @Retention (retentionpolicy.runtime) public @interfaceUseCase {public String name (); Public String desciption () default "no description";}
2 Defining User Classes
Package Com.bjpower.node.spring;public class User {private string username;private string password; @UseCase (name = " Password ", desciption =" password ") public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;} @UseCase (name= "UserName", desciption= "user name") public String GetUserName () {return userName;} public void Setusername (String userName) {this.username = UserName;} Public user () {}public User (String username) {this.username = username;}}
3 Get the Annotation class method
public static void Getrefect (String clas) {try {class<?> cls = Class.forName (clas);//Reflection Gets the class object method [] Methods = Cls.getdeclaredmethods ();//Gets all the methods of the class for (method Method:methods) {//Determines whether there is a specified annotation on the method, and then gets the annotation to print if ( Method.isannotationpresent (usecase.class) = = True) {UseCase usercase = method.getannotation (Usecase.class);// Gets the annotation class System.out.println ("User information:" + usercase.name () + "User description:" +usercase.desciption ());}} } catch (Exception e) {e.printstacktrace ();}}
4 Client Testing
public class Client {public static void main (string[] args) {getrefect ("Com.bjpower.node.spring.User");}}
Test results:
User information: Password User description: Password
User information: UserName User Description: User name
Summary:
The Interpretation of annotations is the use of reflection, dynamic parsing, compared to comments, he has his own unique advantages, we can be specific to some fields or methods to show the page information.
Java--Use of custom annotations