java注解

1.什么是注解

  • Annotation是从JDK5.0开始引入的新技术
  • Annotation的作用:
    • 不是程序本身,可以对程序作出解释。(这一点,跟注释没什么区别)
    • 可以被其他程序(比如:编译器等)读取。(注解信息处理流程,是注解和注释的重大区别
      。如果没有注解信息处理流程,则注解毫无意义)
  • Annotation的格式:
    • 注解是以“@注释名”在代码中存在的,还可以添加一些参数值,例如:
      @SuppressWarnings(value=“unchecked”)。
  • Annotation在哪里使用?
  • 可以附加在package, class, method, field等上面,相当于给它们添加了额外的辅助信
    息,我们可以通过反射机制编程实现对这些元数据的访问。

2.内置注解

2.1 @Override

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

2.2@Deprecated

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

2.3@SuppressWarnings

  • 定义在java.lang.SuppressWarnings中
  • 用来抑制编译时的警告信息
  • 与前两个注释有所不同,你需要添加一个参数才能正确使用,这些参数值都是已经定义好了的
    在这里插入图片描述
@SuppressWarnings("unchecked")
@SuppressWarnings(value={"unchecked", "deprecation"})

3.自定义注解

  • 使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口
  • 要点
    • @interface用来声明一个注解
  • 格式:
    • public @interface 注解名 {定义体} (都是 public )
    • 其中的每一个方法实际上是声明了一个配置参数
    • 方法的名称就是参数的名称
    • 返回值类型就是参数的类型(返回值类型只能是基本类型、Class、String、enum)
    • 可以通过default来声明参数的默认值。
    • 如果只有一个参数成员,一般参数名为value
  • 注意
    • 注解元素必须要有值。我们定义注解元素时,经常使用空字符串、0作为默认值
    • 也经常使用负数(比如:-1)表示不存在的含义
@Target({ElementType.TYPE,ElementType.METHOD}) 	// 元注解 
@Retention(RetentionPolicy.RUNTIME) 		// 表示运行时有用(运行时保留 )
public @interface MyAnnotation {
	
	String value() default ""; 		// 如果只有一个参数,习惯将参数定义为 value
	int age() default 0;
	String []ids() default {};		// 定义数组 
	
}

4.元注解

  • 作用:
    • 注解其他注解。 Java定义了4个标准的meta-annotation类型,它们被用来提供对其它 annotation类型作说明
      • @Target
      • @Retention
      • @Documented
      • @Inherited

4.1@Target

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

在这里插入图片描述

 @Target(value=ElementType.TYPE)

4.2Retention

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

在这里插入图片描述

5.使用反射机制读取到注解信息

案例:将注释在一个 pojo 类中的注解拿出来,方便进行框架 sql 语句的拼写

在这里插入图片描述

a.Student

@TableAnnotation(tableName="tb_student")
public class Student {

	@FieldAnnotation(column="id",length=11,type="int")
	public String id;
	
	@FieldAnnotation(column="name",length=11,type="varchar")
	public String name;
	
	@FieldAnnotation(column="age",length=2,type="int")
	public int age;
	
}

b.TableAnnotation

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TableAnnotation {

	String tableName(); 	// 申明表名
}

c.FieldAnnotation

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface FieldAnnotation {

	// 列名 长度 类型
	String column();
	int length();
	String type();
	
}

d.测试方法

public void test() throws ClassNotFoundException {
	/*
    	首先拿到类的字节码对象 -》 拿到标注在类的注解
    	类字节码 -》 字段字节码对象 -》 标注在字段的注解 
    */
    
    // 拿到 pojo 的字节码对象 
		Class<?> clazz = Class.forName("com.bigguy.annotation.Student");
		
		// 得到所有标注在类上的注解 
		Annotation[] annotations = clazz.getAnnotations();
		
		// @com.bigguy.annotation.TableAnnotation(tableName=tb_student)

		for (Annotation annotation : annotations) {
			System.out.println(annotation);
		}
		
		// 得到类的注解
		TableAnnotation tableAnnotation = clazz.getDeclaredAnnotation(TableAnnotation.class);
		
		// 通过类注解实例,拿到该注解的里面的值 
		// tb_student
		System.out.println(tableAnnotation.tableName());
	}
public void testField() throws Exception{
		// 拿到标注在字段上的信息 
		Class<?> clazz = Class.forName("com.bigguy.annotation.Student");
		
		Field idField = clazz.getField("id");
		
		FieldAnnotation idAnnotation = idField.getAnnotation(FieldAnnotation.class);
		
		System.out.println(idAnnotation.column()); 	// id
		System.out.println(idAnnotation.length()); 	// 11 
		System.out.println(idAnnotation.type()); 	// int 
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值