Java 注解(Annotation)

一:什么是注解?

  1. 面向编程人员和代码的一种特殊解释,从JDK5.0开始。 可以被其他程序读取
  2. 部分注解:@override,@Deprecated,部分注解还可加参数值,如(value=“get”),抑制警告注解@SuppressWarnings(value=“unchecked”)
  3. 注解是一种配置文件,它代替了xml配置文件,使代码更加的简洁。
  4. 注解本身不起作用,起作用的是注解解释器,注解需要和反射一起使用才能发挥大的威力。
    提供信息给编译器: 编译器可以利用注解来探测错误和警告信息
    编译阶段时的处理: 软件工具可以用来利用注解信息来生成代码、Html文档或者做其它相应处理。
    运行时的处理: 某些注解可以在程序运行的时候接受代码的提取

二:内置注解

@Override,表示一个方法声明打算重写超类中的另一个方法声明
@Deprecated:表示不鼓励程序员使用这样的元素,例如(已过时)
@Deprecated  
public static void test(){
	System.out.print("111");
}
public static void main(String[] args){
	test();//可以正常使用,但显示此方法已过时
}

@SupressWarnings:抑制警告注解,加上之后后面的代码警告信息会消失,需加参数(表示抑制哪一类型的警告);

三:元注解

  1. 总共四个,用来解释其他注解的注解,即在定义一个新的注解的时候可以使用到的注解。
  2. @Target:用于描述注解的适用范围,作用域
  3. @Retention:表示注解在那一级别有效,(source < class < runtiome),一般定义为runtime。
  4. @Document:说明该注解将被包含在javadoc中
  5. @Inherited:说明子类可以继承父类中的该注解

四:定义一个注解

//表示此注解在方法和类中可使用,还有其他类型。
@Target(value={ElementType.METHOD,ElementType.TYPE})
//此注解在运行时有效
@Retention(value = RetentionPolicy.RUNTIME)
//标识此注解是否生成在javadoc中
@Document
//表示子类可以继承父类的注解
@Inherited 
public @interface MyAnnotation{
	//注解的参数:参数类型+参数名+()+默认值(可以不写,不写的话使用注解时需要显式配置)
	int age() default 10
}

五:自定义注解

  1. 使用@interface来声明一个注解,会自动继承java.lang.annotation.Annotation接口
  2. 注解内部的每一个方法实际上是声明了一个配置参数,方法的名称就是参数的名称,返回值类型就是参数的类型(只能是Class,String,enum)
  3. 可以使用default来声明参数的默认值
  4. 如果只有一个参数成员,一般参数名为value,如果只有一个参数的话,使用时可以省略
//自定义一个注解
@Target(value=ElementType.METHOD)
@Retention(value=REtentionPolicy.RUNTIME)
@Interface MyAnnotation1{
	//注解的参数:参数类型+参数名();
	String name() default "";
	//没有默认值,则使用时必须显示配置值
	int age();
	String[] schools() default {"西北大学","西北工业大学"};
}

//测试注解
class testAnnotation{
	@MyAnnotation1(age="18")
	public void test1(){
	}
}

六:反射操作注解

任务:利用注解和反射完成类和表结构的映射关系

//练习反射操作注解
public class Test5 {
    public static void main(String[] args) throws Exception {
        Class c1 = Class.forName("com.chelsea.pojo.Student");
        //通过反射获得注解
        Annotation[] annotations = c1.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
            //此处只获得了类注解:com.chelsea.reflection.chelseaTable(value=db_student)
        }
        
        //获得注解value值
        MyTable annotation =(MyTable)c1.getAnnotation(MyTable.class);
        System.out.println(annotation.value());
        //此处获得了具体的注解值:db_student
        
        //获得类属性指定的注解
        Field name = c1.getDeclaredField("name");
        MyField annotation1 = name.getAnnotation(MyField.class);
        System.out.println(annotation1.columnName());//db_name
        System.out.println(annotation1.type());//db_name
        System.out.println(annotation1.length());//3
    }
}

@chelseaTable("db_student")
class Student{
    @chelseaField(columnName ="db_id",type = "int",length =10)
    private int id;
    @chelseaField(columnName ="db_age",type = "int",length =10)
    private int age;
    @chelseaField(columnName ="db_name",type = "varchar",length =3)
    private String name;
    public Student() {
    }
    public Student(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }
    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}
//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface chelseaTable{
    String value();
}
//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface chelseaField{
    String columnName();
    String type();
    int length();
}

七:注解思考

Spring等框架中,有很多注解,类似于@Bean,@Controller,@ResponseBody,@RequestMapping,@GetMapping,@Resourse,@Services,@Componennt等,都是Java注解,其中传入的参数,可以根据反射获取,进而进行配置,来简便我们项目的开发。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值