Java基础(十三):注解(Annotation)


一、注解概述

什么是注解

  • 注解(Annotation)是从JDK5.0开始引入,以“@注解名”在代码中存在
  • Annotation 可以像修饰符一样被使用,可用于修饰包、类、构造器、方法、成员变量、参数、局部变量的声明
  • 还可以添加一些参数值,这些信息被保存在 Annotation 的 “name=value” 对中
  • 注解可以在类编译、运行时进行加载,体现不同的功能

注解的重要性

  • 在JavaSE中,注解的使用目的比较简单,例如标记过时的功能,忽略警告等
  • JavaEE/Android中注解占据了更重要的角色,例如用来配置应用程序的任何切面,代替JavaEE旧版中所遗留的繁冗代码XML配置
  • 注解是一种趋势,一定程度上可以说:框架 = 注解 + 反射 + 设计模式

二、元注解

JDK1.5在java.lang.annotation包定义了4个标准的meta-annotation类型,它们被用来提供对其它 annotation类型作说明

  • @Target: 用于描述注解的使用范围
    • 可以通过枚举类型ElementType的10个常量对象来指定
    • TYPE,METHOD,CONSTRUCTOR,PACKAGE…
  • @Retention: 用于描述注解的生命周期
    • 可以通过枚举类型RetentionPolicy的3个常量对象来指定
    • SOURCE(源代码)、CLASS(字节码)、RUNTIME(运行时)
    • 唯有RUNTIME阶段才能被反射读取到
  • @Documented: 表明这个注解应该被 javadoc工具记录
  • @Inherited: 允许子类继承父类中的注解

示例代码:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
    String[] value();
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
public @interface Deprecated {
}
  • @Repeatable:允许在相同的程序元素中重复实用同一个注解

不使用@Repeatable修饰的自定义注解完成重复注解

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Persons {
    Person[] value();
}

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Person {
    String name() default "";
}
@Persons(value = {@Person(name = "zhangsan"),@Person(name = "lisi")})
public static void doSome(){

}

使用@Repeatable修饰的自定义注解

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Persons {
    Person[] value();
}

@Repeatable(Persons.class)
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Person {
    String name() default "";
}
@Person(name = "ding")
@Person(name = "yang")
public static void doSome(){

}

三、自定义注解

声明自定义注解

【元注解】
【修饰符】 @interface 注解名{
    【成员列表】
}
  • 自定义注解可以通过四个元注解@Retention,@Target,@Inherited,@Documented,
    分别说明它的声明周期使用位置是否被继承是否被生成到API文档中
  • Annotation 的成员在 Annotation 定义中以无参数有返回值的抽象方法的形式来声明,我们又称为配置参数
    返回值类型只能是八种基本数据类型、String类型、Class类型、enum类型、Annotation类型、以上所有类型的数组
  • 可以使用 default 关键字为抽象方法指定默认返回值
  • 如果定义的注解含有抽象方法,那么使用时必须指定返回值
    • 除非它有默认值。格式是“方法名 = 返回值”
    • 如果只有一个抽象方法需要赋值,且方法名为value,可以省略“value=”
    • 所以如果注解只有一个抽象方法成员,建议使用方法名value

示例:

@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {
    String value();
}
@Inherited
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
    String columnName();
    String columnType();
}

使用自定义注解

@Table("t_stu")
public class Student {
    @Column(columnName = "sid",columnType = "int")
    private int id;
    @Column(columnName = "sname",columnType = "varchar(20)")
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值