Java annotations

http://www.importnew.com/10294.html

The above explanation is more detailed.

However, there are some people who probably know what annotations do, but now they just want to know how to customize annotations and get the values ​​on the annotations. If so, please follow the steps below. If you don't understand what the annotation does, you can study it carefully according to the link at the beginning of the article.

Custom annotations:

Field in Target indicates that this annotation is used on Field (member variable), so you can know that there must be similar values ​​such as Method.

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelRole {

    public String value() default "";
}

Use custom annotations on classes

public class Test {

    @ExcelRole(value = "Hello")
    public String name;
}

How to get annotation data (reflection):

public static void main(String[] args) {
        Class c = Test.class;
        for (Field field : c.getFields()) {
            ExcelRole todoAnnotation = (ExcelRole) field.getAnnotation(ExcelRole.class);
            if (todoAnnotation != null) {
                System.out.println("Method Name:" + field.getName());
                System.out.println("value:" + todoAnnotation.value());
            }
        }
    }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325342144&siteId=291194637