A brief introduction to Java custom annotations and annotations

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.