Good programmers tutorial Java annotations and sharing application programming notes

  Good programmers tutorial Share Java annotations and application programming notes , annotations and use :

First look at the concept was first comments from the point of view;

Note: The code to add to the description and interpretation, comments help developers understand the program. (Comment) that the white point is the comment is posters.

Note: Add a description to the code interpretation , explanation for this program. (Annotation)

From the beginning of JDK 5.0, Java adds support for metadata (MetaData), that is, Annotation (annotation).

Three basic Annotation:

@Override: defining override the parent class method, the method can only be used annotation

@Deprecated: used to represent a program element (classes, methods etc.) Obsolete

@SuppressWarnings: suppress compiler warnings.

What is a comment

Annotation code is actually a special mark, which is used to replace the configuration file, that is, the traditional way to tell the class how to run through the configuration file, after the notes have technology, developers can annotate how to tell the class to run. Typical applications annotations in Java technology is there: by reflection technique to get inside the class notes, to decide how to run the class.

Important technical notes:

How to define comment

How reflective notes and annotations based on the reflection of information to decide how to run the class

2.1 custom annotation:

Annotation define a new type of keyword use @interface

Statement annotation properties


Notes the role of the property: the original write information in the configuration file, can be described by annotation property.
Annotation property declarations manner: String name ();
Property Default declaratively: Stringname () default "xxx" ;
Specialties value: If the annotation has a name value of attribute, but value = partially omitted when using annotations, such as @MyAnnotation ( "xxx")
special property value [];


Type of annotation attributes can be:
    String type
    basic data types
    Class type
    enumeration type
    annotation type
    more than one type of one-dimensional array

Case 1 demonstrates the creation and use of annotations


public @interface MyAnnocation {
    String name();
    int num() default 10;
    MyAnnocation2 anno();
}
public @interface MyAnnocation2 {
    String value();
}

public class Demo1 {
    @MyAnnocation(name="哈哈",num=50,anno=@MyAnnocation2(value = "xxx"))
    public void show() {
        System.out.println("xxxxxxx");
    }
}

2.2 JDK meta Annotation

Yuan Annotation refers to modifications of the Annotation Annotation.

@Retention : Annotation can only be used to modify a definition for the specified domain can retain the Annotation, @Rentention RetentionPolicy contains a member variable of type, specify the domain through this variable.


RetentionPolicy.CLASS: compiler will record notes in class files when you run a Java program, JVM will not be reserved Note This is the default value..
RetentionPolicy.RUNTIME: compiler will record notes in class file when run Java programs. when, JVM will keep notes, you can obtain the comment by reflection.
RetentionPolicy.SOURCE: compiler discards the comments of this strategy

@Target : annotations are used to specify which members of the modified class @ Target contains a named value, a member variable of type of ElementType.

@Documented: it is used to specify the yuan Annotation Annotation modified class will be extracted into the javadoc documentation tool.

@Inherited: is it modified Annotation would have inherited if a class is @Inherited modified using the Annotation, then it will automatically have the subclass comment.

Case 2 demo use reflection to obtain information notes


@Retention(RetentionPolicy.RUNTIME)
public @interface PersonInfo {
    String name();
    int age() default 20;
    String gender();
}

public class PersonOpe {
    @PersonInfo(name="李四",age=20,gender="男")
    public void show(String name,int age,String gen) {
        System.out.println(name);
        System.out.println(age);
        System.out.println(gen);
    }
}
public class Demo2 {
    public static void main(String[] args) throws Exception{
        PersonOpe ope=new PersonOpe();
        Class<?> class1=PersonOpe.class;
        Method method = class1.getMethod("show", String.class,int.class,String.class);
        PersonInfo annotation = method.getAnnotation(PersonInfo.class);
        String name=annotation.name();
        int age=annotation.age();
        String gender=annotation.gender();
        method.invoke(ope, name,age,gender);
        
    }
}


Guess you like

Origin blog.51cto.com/14256902/2423176