Java Annotation Overview Detailed _java

Source: Internet
Author: User

Java Annotations Overview:

1. Annotations are given to the compiler, which is different from the annotation

2. Three basic annotations:

@Override tell the compiler that this is the override method
@Deprecated tell the compiler that this method is obsolete.
@SuppressWarnings ("unchecked") do not warn
= (value={"Unchecked"})
3. Annotations can be used in place of traditional configuration files
4. JDK5 began, Java increased support for metadata (MetaData), that is, annotation.

custom Annotation and reflection annotations

Custom annotations:
1. New annotation: (more than an @ symbol for the definition of an interface)

Copy Code code as follows:

Public @interface Myannotation {
Property
String who ();
int age ();
String gender ();
}

2. Set annotations with default values

Copy Code code as follows:

Public @interface Youannotation {
String who () default "Tom";
int age () default 0;
String gender () default "female";
}

3. Array case

Copy Code code as follows:

Public @interface Theyannotation {
String[] Value (); Be sure to have ()
}

Yuan Annotation/metaannotation
Used to modify the annotation. (can view the source code of @override)

@Retention annotation Policy, which specifies the domain that the annotation can hold
Retentionpolicy.class
At the bytecode level, not visible at run level (default)
Retentionpolicy.runtime
Three levels are visible, the runtime can reflect
Retentionpolicy.source is only available at the source level and is not visible at the byte code level

@Target specify which ranges the annotation can be used on
@Documented write to the document, which is written to the HTML document when you use the Javadoc command
@Inherited inheritable, subclasses of this class still have the attributes of the annotation of the parent class

Ex. Perform connection database operations in the way reflected annotations:

The definition notes are as follows:

Copy Code code as follows:

Allow an annotation to be reflected at run time
@Retention (Retentionpolicy.runtime)
Public @interface DbInfo {
String driver () default "Com.mysql.jdbc.Driver";
String URL () default "url = jdbc:mysql://localhost:3306/academic";
String password () default "1234";
String username () default "root";
}

Reflection annotation:

Copy Code code as follows:

@DbInfo
public static Connection getconnection () throws exception{
Gets the byte code of the class
Class clazz = Demo2.class;
Gets the public method named Getconnection () in the class
Parameter 1: Method name
Parameter 2: The byte code object corresponding to the method type parameter, or null
Method method = Clazz.getmethod ("getconnection", null);
By this method, the annotation defined on the method is obtained.
DbInfo DbInfo = method.getannotation (Dbinfo.class);
String Driver = Dbinfo.driver ();
String URL = dbinfo.url ();
String user = Dbinfo.username ();
String password = Dbinfo.password ();

Class.forName (driver);
Return drivermanager.getconnection (URL, user, password);
}

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.