77 注解(二)——反射机制读取注解

步骤

  • 编写类
  • 编写类的注解
  • 编写类的属性的注解
  • 在类中添加注解
  • 反射读取注解内容

编写类和添加注解

package _20200102_reflectAnnotation_again;
@StuTable("tb_student")//添加注解
public class Student {
	@StuField(columnName="name",type="varchar",length=10)//添加注解
	private String name;
	@StuField(columnName="age",type="int",length=3)//添加注解
	private int age;
	@StuField(columnName="id",type="varchar",length=10)//添加注解
	private String 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;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
}

  

编写类的注解

package _20200102_reflectAnnotation_again;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(value=ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface StuTable {
	String value();
}

  

编写类的属性的注解

package _20200102_reflectAnnotation_again;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Student类属性的注解
 * @author TEDU
 *
 */
@Target(value=ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface StuField {
	String columnName();
	String type();
	int length();
}

  

反射读取注释内容

package _20200102_reflectAnnotation_again;

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

/**
 * 	读取Student类中的注解
 * @author TEDU
 *
 */
public class ReflectAnnotation {
	public static void main(String[] args) throws Exception {
		//反射加载类
		Class<?> clz = Class.forName("_20200102_reflectAnnotation_again.Student");
		//读取类的注解
		Annotation[] annos = clz.getAnnotations();
		for(Annotation an : annos) {
			System.out.println(an);
		}
		//读取类的注解的单个值的方式
		StuTable sb = clz.getAnnotation(StuTable.class);//获取当前类的注解的对象
		System.out.println(sb.value());
		//读取属性的注解
		Field f = clz.getDeclaredField("name");//根据Student类中的属性名获取对应的属性变量(Field是reflect包中的类)
		StuField sf = f.getAnnotation(StuField.class);//获取属性对应的注解对象(StuField是一个Annotation)
		System.out.println("columnName:"+sf.columnName()+" type:"+sf.type()+" length:"+sf.length());
		Field f1 = clz.getDeclaredField("id");
		StuField sf1 = f1.getAnnotation(StuField.class);
		System.out.println("columnName:"+sf1.columnName()+" type:"+sf1.type()+" length:"+sf1.length());
		Field f2 = clz.getDeclaredField("age");
		StuField sf2 = f2.getAnnotation(StuField.class);
		System.out.println("columnName:"+sf2.columnName()+" type:"+sf2.type()+" length:"+sf2.length());
          //以上很冗余,可以一次性读取类中属性的数组,然后循环获取对应的注解,然后输出

          Field[] fs = clz.getDeclaredFields();
          for(Field field : fs) {
          CarField cf = field.getAnnotation(StuField.class);
          System.out.println("columnName:"+cf.columnName()+" type:"+cf.type()+" length:"+cf.length());
}


} }

  

运行结果

@_20200102_reflectAnnotation_again.StuTable(value=tb_student)
tb_student
columnName:name type:varchar length:10
columnName:id type:varchar length:10
columnName:age type:int length:3

  

原文地址:https://www.cnblogs.com/Scorpicat/p/12141821.html