注解:Annotation

内置注解

@Override

定义在java.lang.Override中,此注释只适用于修辞方法,表示一个方法声明打算重写超类中的另一个方法声明。

@Deprecated

定义在java.lang.Deprecated中,此注解可用于修辞方法、属性、类,表示不鼓励程序员使用这样的元素,通常是因为它很危险或存在更好的选择

@SuppressWarnings

定义在java.lang.SuppressWarnings中,用来抑制编译时的警告信息。

自定义注解

使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口

要点

—@interface用来声明一个注解

.格式为:public @interface 注解名(定义体)

—其中的每一个方法实际上是声明了一个配置参数。

.方法的名称就是参数的名称

.返回值类型就是参数的类型(返回值类型只能是基本数据类型、Class、String、enum)。

.可以通过default来声明参数的默认值

.如果只有一个参数成员,一般参数名为value

元注解

@Target:注解所修饰对象的范围

@Retention:注解保留的时间

@Documented:描述其他类型的注解

@Inherited:表示某个类是可以被继承的

Target
作用

——用于描述注解的使用范围(即被描述的注解可以用在什么地方)

左边取值ElementType——右边所修饰的范围.

PACKAGE——package包

TYPE——类,接口,枚举,Annotation类型

CONSTRUCTOR:用于描述构造器;FIELD:用于描述域;METHOD:用于描述方法——类型成员(方法,构造方法、成员变量、枚举值)

LOCAL_VARLABLE:用于描述局部变量,PARAMETER:用于描述参数——方法参数和本地变量

Retention
作用

表示需要什么级别保存改注解信息,用于描述注解的生命周期

左边取值,右边作用

SOURCE:在源文件中有效(即源文件保留)

CLASS:在class文件中有效(即class保留)

RUNTIME:运行时有效(即运行时保留),为Runtime可以被反射机制读取(此时SOUCE和CLASS都有效)

反射机制读取注解

①定义表的属性注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Filed {
String columnName();//表的列名

String type();//表的类型

int length();//表的长度
}
定义一个表的属性注解
 @Target(ElementType.TYPE)
 @Retention(RetentionPolicy.RUNTIME)
 public @interface Table {
 String value();//一个属性的时候,命名为value
 }
定义一个Student类
@Table(value = "tb_student")
public class Student {
@Filed(columnName = "id", type = "int", length = 10)
private int id;
@Filed(columnName = "name", type = "varchar", length = 10)
private String name;
@Filed(columnName = "age", type = "int", length = 10)
private int age;

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;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}
}
然后利用反射获取所有注解信息
 try {
        Class clazz = Class.forName("com.hbwj.myapplication.Student");
        //获取类的所有有效注解
        Annotation[] annotations = clazz.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);//@com.hbwj.myapplication.Table(value=tb_student)
        }
        //获取指定类注解的值
        Table table = (Table) clazz.getAnnotation(Table.class);
        System.out.println(table.value());//tb_student
        //获取所有属性的注解
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field filed : declaredFields) {
            Filed file = filed.getAnnotation(Filed.class);
            System.out.println("所有属性的值==" + file.columnName() + "--" + file.type() + "--" + file.length());
        }
        //获取指定属性的注解,获取name的注解
        Field field = clazz.getDeclaredField("name");
        Filed file = field.getAnnotation(Filed.class);
        System.out.println("当前name属性的值==" + file.columnName() + "--" + file.type() + "--" + file.length());//当前name属性的值==name--varchar--10
    } catch (Exception e) {
        e.printStackTrace();
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值