Home  >  Article  >  Java  >  Introduction to the use of Java implementation annotations

Introduction to the use of Java implementation annotations

黄舟
黄舟Original
2017-09-14 10:49:431489browse

This article mainly introduces relevant information on the implementation and use of Java annotations in detail. I hope that through this article everyone can understand and master the knowledge of Java annotations. Friends in need can refer to

Detailed Java annotations. Implementation and use of annotations

Java annotations are released in java5. Their function is to save configuration files and enhance code readability. It is very common in various frameworks and development today, so I would like to explain it here.

How to create an annotation

Each custom annotation consists of four meta-annotations, which are provided by java itself:

@Target(ElementType.**)

This is an enumeration, and the top is where the custom annotations are used, such as classes, variables, methods, etc.

@Retention(RetentionPolicy.**) is used to indicate at what level the annotation is saved, such as during compilation, in class files, and during vm running

@Documented Include this annotation in javadoc , which means that this annotation will be extracted into a document by the javadoc tool. The content in the doc document will be different depending on the information content of this annotation.

@Inherited: After you define an annotation and use it in program code, the annotations in the parent category will be defaulted. It will not be inherited into subcategories. You can add an Annotation qualified by java.lang.annotation.Inherited when defining annotations, which allows the Annotation type you define to be inherited.

After introducing the theory, start the code (talk is cheap, show your code)


package com.yasin.JavaLearn;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 这是一个类级别的注释,这个注释中有一个name字段,默认值是 yasin
 * @author yasin
 *
 */

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Learn {
  String name() default "yasin";
}


package com.yasin.JavaLearn;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 这是一个变量级别的注解,注解中有一个字段name,默认值是field
 * @author yasin
 *
 */

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FiledLearn {
  String name() default "field";


}


package com.yasin.JavaLearn;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 这是一个方法级别的注解
 * @author yasin
 *
 */

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MethodLearn {
  String name() default "method";

}

I have defined three annotations above, which are commonly used three-level annotations of classes, variables, and methods.

Below I define a class and use these three annotations


package com.yasin.JavaLearn;

@Learn
public class Yasin {

  @FiledLearn
  public int level;

  @FiledLearn(name="xq")
  public String xq;

  public String a;


  @MethodLearn(name="test")
  public void setMain(){

  }

  public void setA(){

  }

}

The following is how to use this annotation. The extraction of annotations is Obtain the corresponding variables and methods through class reflection, and obtain annotations from the variables and methods.


package com.yasin.JavaLearn;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.xml.DOMConfigurator;

/**
 * Hello world!
 *
 */
public class App {

  public static void main(String[] args) {

    Learn learn = Yasin.class.getAnnotation(Learn.class);
    System.out.println(learn.name());


    Field[] fields = Yasin.class.getFields();//获取该类所有的字段

    for(Field filed:fields){
      if(filed.isAnnotationPresent(FiledLearn.class)){//校验该字段是否添加这个注解
        System.out.println(filed.getName());
        FiledLearn filedLearn = filed.getAnnotation(FiledLearn.class);
        System.out.println(filedLearn.name());
      }
    }

    Method[] methods = Yasin.class.getMethods();
    for(Method method:methods){
      if(method.isAnnotationPresent(MethodLearn.class)){//校验该方法是否有这个注解
        System.out.println(method.getName());
        MethodLearn methodLearn = method.getAnnotation(MethodLearn.class);
        System.out.println(methodLearn.name());
      }

    }


  }
}

The above is the detailed content of Introduction to the use of Java implementation annotations. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn