个人理解,注解就是代码中的特殊标记,这些标记可以在编译、类加载、运行时被读取,从而做相对应的处理。
注解跟注释很像,区别是注释是给人看的;而注解是给程序看的,它可以被编译器读取。
注解大多时候与反射或者 AOP 切面结合使用,它的作用有很多,比如标记和检查,最重要的一点就是简化代码,降低耦合性,提高执行效率。比如可以通过自定义注解 + AOP 切面结合,解决写接口重复提交的问题。
// 声明 NoRepeatSubmit 注解
@Target(ElementType.METHOD) // 元注解
@Retention(RetentionPolicy.RUNTIME) // 元注解
public @interface MyAnnotation {/*** 锁定时间,默认单位(秒)*/long lockTime() default 3L;}
注解可以分为三类,基本注解、元注解和自定义注解
其中基本注解例如@Override 用于表明检查方法是否被重写;@SuppressWarning 表明编译器忽略注解中声明的警告。
元注解常用的@ Retention用来定义该注解在哪一个级别可用,在源代码中(SOURCE)、类文件中(CLASS)或者运行时(RUNTIME);@Target:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)。

自定义注解则可以根据第三部分语法自己定义需要的注解。
首先声明一个注解,Init用来表示这个方法是初始化方法。
import java.lang.annotation.*;@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Init {}
再写一个Demo类,写几个方法并用Init注解,表明这些类会在类初始化时候使用。 Demo类中有两个构造方法,有两个被@Init修饰的方法。
package Annotation;public class Demo {int i;int l;public Demo(){System.out.println("无参构造方法");}public Demo(int i,int l ){this.i = i;this.l = l;System.out.println("有参构造方法"+this.i+this.l);}@Initpublic void print(){System.out.println("无参初始化方法");}@Initpublic void printInt(int i){System.out.println("有参初始化方法"+i);}
}
定义一个测试类用来通过反射来获取被@Init修饰的方法,并操作方法
import java.lang.reflect.Method;public class Test {public static void main(String[] args) throws Exception {Class extends Demo> aClass = Demo.class;//有参建立实例Demo demo = aClass.getConstructor(int.class, int.class).newInstance(6, 7);//无参数建立实例//1、aClass.newInstance();//2、aClass.getConstructor(null).newInstance(null);System.out.println("-----------------------------");//不含构造方法父类继承方法Method[] declaredMethods = aClass.getDeclaredMethods();for (Method declaredMethod : declaredMethods) {//判断方法是否存在Initboolean annotationPresent = declaredMethod.isAnnotationPresent(Init.class);//如果存在则调用if(annotationPresent){if(declaredMethod.getName().equals("printInt")){declaredMethod.invoke(aClass.getConstructor(null).newInstance(null),5);}else {declaredMethod.invoke(aClass.getConstructor(null).newInstance(null));}}}}
}
运行结果:
有参构造方法67
无参构造方法
无参构造方法
-----------------------------
无参构造方法
无参初始化方法
无参构造方法
有参初始化方法5
这里有一个小的知识点,newInstance()创建实例的两种方法
Class.newInstance()
Constructor(Classes).newInstance(args)以下对两种调用方式给以比较说明:
Class.newInstance() 只能够调用无参的构造函数,即默认的构造函数;
Constructor.newInstance() 可以根据传入的参数,调用任意构造构造函数。Class.newInstance() 抛出所有由被调用构造函数抛出的异常。
Class.newInstance() 要求被调用的构造函数是可见的,也即必须是public类型的;
Constructor.newInstance() 在特定的情况下,可以调用私有的构造函数。