注解(Annotation)

注解(Annotation)

  • Java 注解用于为 Java 代码提供元数据。作为元数据,注解不直接影响你的代码执行,但也有一些类型的注解实际上可以用于这一目的。
  • 注解的本质是接口,当我们在类、方法、成员变量上使用注解的时候,相当于在此处定义了一个引用类型的变量(面向接口编程)。当使用注解时(相当于接口的实例化),通过JDK动态代理生成代理对象,而代理对象可以在不改变原有的基础上附加功能。
public @interface test {
    String value();
}

通过javap -c可以得到

Compiled from "test.java"
public interface test extends java.lang.annotation.Annotation {
  public abstract java.lang.String value();
}
内置注解
注解作用注意事项
@Override它是用来描述当前方法是一个重写的方法,在编译阶段对方法进行检查jdk1.5中它只能描述继承中的重写,jdk1.6中它可以描述接口实现的重写,也能描述类的继承的重写
@Deprecated它是用于描述当前方法是一个过时的方法
@SuppressWarnings对程序中的警告去除
元注解
  • 元注解顾名思义我们可以理解为注解的注解,它是作用在注解中,方便我们使用注解实现想要的功能。元注解分别有@Retention、 @Target、 @Document、 @Inherited和@Repeatable(JDK1.8加入)五种。具体参照
注解作用
@Target使用@Target元注解表示我们的注解作用的范围
@Retention它表示注解存在阶段是保留在源码(编译期),字节码(类加载)或者运行期(JVM中运行)
@Documented它的作用是能够将注解中的元素包含到 Javadoc 中去
@Inherited@Inherited注解了的注解修饰了一个父类,如果他的子类没有被其他注解修饰,则它的子类也继承了父类的注解
@Repeatable被这个元注解修饰的注解可以同时作用一个对象多次,但是每次作用注解又可以代表不同的含义
public class Test01 {
    @MyAnnotation(name = "POP")
    public void test(){

    }
}

@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation{
    //注解参数 = 参数类型 + 参数名()
    String name() default "";
    int age() default 0;
    int id() default -1;
    String[] school() default {"A","B"};
}

通过反射获取注解

import java.lang.annotation.*;
import java.lang.reflect.Field;

public class Test02 {
    public static void main(String[] args) {
        try {
            Class c1 = Class.forName("Student");

            //通过反射获取注解
            Annotation[] annotations = c1.getAnnotations();
            for (Annotation annotation : annotations) {
                System.out.println(annotation);
            }

            //获取注解的值
            MyLabel myLabel= (MyLabel) c1.getAnnotation(MyLabel.class);
            String value = myLabel.value();
            System.out.println(value);

            //获取类指定的注解
            Field name = c1.getDeclaredField("name");
            MyFiled name_annotation = name.getAnnotation(MyFiled.class);
            System.out.println(name_annotation.columnName());
            System.out.println(name_annotation.type());
            System.out.println(name_annotation.length());
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

@MyLabel("db_table")
class Student{
    @MyFiled(columnName = "db_id",type = "int",length = 10)
    private int id;

    @MyFiled(columnName = "db_age",type = "int",length = 10)
    private int age;

    @MyFiled(columnName = "db_name",type = "varchar",length = 3)
    private String name;

    public Student(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }

    public Student() {
    }

    public int getId() {
        return id;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

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


//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface MyLabel{
    String value();
}

//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface MyFiled{
    String columnName();
    String type();
    int length();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值