Java注解(Annotation)

一、什么是注解

个人理解,注解就是代码中的特殊标记,这些标记可以在编译、类加载、运行时被读取,从而做相对应的处理

注解跟注释很像,区别是注释是给人看的;而注解是给程序看的,它可以被编译器读取

二、注解的作用

注解大多时候与反射或者 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);
    }

    @Init
    public void print(){
        System.out.println("无参初始化方法");
    }
    @Init
    public 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) {

            //判断方法是否存在Init
            boolean 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() 在特定的情况下,可以调用私有的构造函数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Weber77

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值