Overview of JAVA comments:
1. Annotations are for the compiler to see, which is different from annotations
2. Three basic annotations:
@Override tells the compiler that this is overriding the method
@Deprecated Tell the compiler that the method is outdated
@SuppressWarnings("unchecked") Don't warn
= (value={"unchecked"})
3. Annotations can be used to replace traditional configuration files
4. Starting from JDK5, Java has added support for metadata (MetaData), namely Annotation.
Custom annotations and reflection annotations
Custom annotations:
1. Create a new annotation: (only one more @ symbol than the interface definition)
The code copy is as follows:
public @interface myAnnotation {
//property
String who();
int age();
String gender();
}
2. Set annotation with default values
The code copy is as follows:
public @interface YouAnnotation {
String who() default "tom";
int age() default 0;
String gender() default "female";
}
3. Array situation
The code copy is as follows:
public @interface TheyAnnotation {
String[] value(); // Must have ()
}
MetaAnnotation / MetaAnnotation
Used to modify Annotation. (You can view the source code of @Override)
@Retention Annotation policy, used to specify the domains that the Annotation can retain
RetentionPolicy.CLASS
There are at the bytecode level, but are not visible at the run level (default)
RetentionPolicy.RUNTIME
All three levels are visible and can be reflected during runtime
RetentionPolicy.SOURCE is only available at the source code level and is not visible at the bytecode level.
@Target Specifies the scope of the annotation being used
@Documented Write to the document. When writing to the html document using the javadoc command, the annotation is written to
@Inherited Inheritability, subclasses inheriting this class still have the characteristics of the parent class as the annotation.
Ex. Reflection annotation to perform the connection to the database operation:
The definition annotations are as follows:
The code copy is as follows:
//Let an annotation be reflected at runtime
@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 Injection:
The code copy is as follows:
@DbInfo
public static Connection getConnection() throws Exception{
//Get the bytecode of this class
Class clazz = Demo2.class;
//Get the public method named getConnection() in this class
// Parameter 1: Method name
//Parameter 2: The bytecode object corresponding to the method type parameter. If not, null
Method method = clazz.getMethod("getConnection", null);
// Through this method, obtain the annotation defined on the method
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);
}