This is probably the easiest to implement custom annotation of the article

Defined annotations

Notes defined by @interface

public @interface Log {
}
复制代码

It's almost the way keywords and interface definition, but in front of one @ symbol

Notes applications

Application of the above embodiment is very simple annotations in a class or a method of adding @xx like, specifically speaking later

public class Controller {
    @Log
    public void test(){
    }
}

@Log
public class Controller {
    public void test(){
    }
}

复制代码

Yuan notes

Meta-annotation: annotation annotation is defined, it is a basic annotation can be defined to comment on comments

@Retention

Notes used to illustrate the life cycle

@Retention (RetentionPolicy.SOURCE) notes retained only in the source stage, compile was discarded

@Retention (RetentionPolicy.CLASS) default policy, annotations are only kept time to build performed, it will not be loaded into the JVM.

@Retention (RetentionPolicy.RUNTIME) annotation can be retained when the program is running, it will be loaded into the JVM, so the program is running can also get to it.

@Documented

Documented annotation indicates that this is a javadoc comment recorded by default has a similar record tool. If a type declaration is annotated documented, it notes become part of the public API.

@Target explain the role of the target annotation

@Target (ElementType.TYPE) interfaces, classes, enumerations, annotations

@Target (ElementType.FIELD) field, enumeration constants

@Target(ElementType.METHOD) 方法

@Target (ElementType.PARAMETER) Method Parameters

@Target (ElementType.CONSTRUCTOR) Constructor

@Target (ElementType.LOCAL_VARIABLE) local variables

@Target(ElementType.ANNOTATION_TYPE) 注解

@Target(ElementType.PACKAGE) 包

@Inherited

@Inherited: If you use a comment to this annotation 1, used a class, then this also Note 1 subclass of class effect. E.g:

@Inherited
@Target(ElementType.METHOD)
public @interface Log {

}

@Log
public class testA(){}

public class testB() extends testA{}

复制代码

testA applied @Log this annotation, then testB class above, there are @Log this comment.

@Repeatable

E.g:

@Target(ElementType.METHOD)  
@Retention(RetentionPolicy.RUNTIME)
public @interface Logs {
      Log[] value();
}

@Repeatable(Logs.class)
public @interface Log{
     String descr() default "";
}

public class Controller {
    @Log(descr="描述1")
    @Log(descr="描述2")
    public void test(){

    }
}

public class LogRunner {
    public static void main(String []args) throws Exception{
        System.out.println("开始时间:"+new Date());
        Class classs=Controller.class;
        Method[] ms=classs.getMethods();
        for(Method method:ms){
            boolean flag=method.isAnnotationPresent(Logs.class);
            if(flag){
                Logs logs=method.getAnnotation(Logs.class);
                for(Log log:logs.value()){
                    System.out.println(log.descr());
                }
            }
        }
    }
}

复制代码

When the output is output, description 1, 2 is described.

Implement custom annotation

The first step is to define a comment

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface Log {
    String methodName() default "defaultAMethod";
    String controller() default "defaultController";
    String descr() default  "defaultDescr";
}
复制代码

The second step applied this comment

public class Controller {
    @Log
    public void test(){
        System.out.println("结束时间:"+new Date());
    }
}
复制代码

The third step is to make this comment by reflecting work

public class LogRunner {
    public static void main(String []args) throws Exception{
        System.out.println("开始时间:"+new Date());
        Class classs=Controller.class;
        Method[] ms=classs.getMethods();
        for(Method method:ms){
            boolean flag=method.isAnnotationPresent(Log.class);
            if(flag){
                Log log=method.getAnnotation(Log.class);
                System.out.println(log.methodName());
                System.out.println(log.descr());
                System.out.println(log.controller());
                method.invoke(classs.newInstance(),null);
            }
        }
    }
}
复制代码

Export

image

Output

If the second step of the above comments do not use the default value, change the code

public class Controller {
    @Log(methodName = "test",controller = "testController",descr = "测试")
    public void test(){
        System.out.println("结束时间:"+new Date());
    }
}
复制代码

It will output

image

Output

Forgive me when a handful of heading the party, ladies and Friends of Scouting please forgive me.

Welcome to public concern number: Thousand Jue, sending back: one hundred books brought back-end book early to architecture.

Guess you like

Origin juejin.im/post/5dfde62e6fb9a0164f294c29