注解,其实就是这么简单( 三 )

在实际的方法使用中 , 我们只需要给必须要修改的值赋值即可
@myAnnotaion2(name = "Simon")public void test1(){System.out.println("测试注解1");}复制代码因此 , 自定义注解可以归纳如下:

  • @interface用来声明一个注解 , 格式:public @interface 注解名{定义内容}
  • 其中每一个方法实际上声明了一个配置参数
  • 方法的名称就是参数的名称
  • 返回值类型就是参数的类型(返回值只能时基本类型 , Class,String,enum)
  • 可以通过default用来声明参数的默认值
  • 如果只有一个参数成员 , 一般参数名为value
  • 注解元素必须要有值 , 我们定义注解元素时 , 经常使用空字符串0作为默认值
5、获取注解中的参数值在以上的讲解中 , 我们使用注解都是对所修饰的类、方法、变量进行规范和约束 , 在大多数使用场景中 , 以方法为例 , 我们需要将注解中的信息同方法联系起来 , 即将注解中的参数信息的注入到方法中 。
5.1参数值是基本类型
  • 首次 , 我们创建一个带有参数的注解
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD})@Retention(value = http://kandian.youth.cn/index/RetentionPolicy.RUNTIME)@interface MyAnnotation5 {String name() default" ";int age() default 0;}复制代码
  • 将该注解修饰到某一方法上
@MyAnnotation5(name = "Simon", age = 25)public void testInjectValue(String name,int age) {System.out.println("获取注解中的参数值:");System.out.println(name);System.out.println(age);}复制代码
  • 反射获取注解中的参数并注入到方法中
反射获取该类的方法 通过方法获取注解中的参数值 将注解中的参数值注入到相应的方法中
//反射获取类 , 并得到类中的方法Class aClass = InjectValue.class;Method method=aClass.getMethod("testInjectValue",String.class,int.class);//获取注解中的属性值MyAnnotation5 myAnnotation5=method.getAnnotation(MyAnnotation5.class);String name=myAnnotation5.name();int age=myAnnotation5.age();//将属性值注入到相应的方法中Object o=aClass.newInstance();method.invoke(o,name,age);复制代码5.2参数值是对象前面我们讲解如何将注解中的参数为基本数据类型注入到方法中 , 那么如何将注解中的参数为对象注入到方法中呢?
  • 创建一个类用于生成对象
public class Animal {private String name;private int age;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 class AnimalDao {private Animal animal;public Animal getAnimal() {return animal;}@MyAnnotation6(name = "Dog",age = 12)public void setAnimal(Animal animal) {this.animal = animal;}}复制代码
  • 获取注解中的对象
public class TestInjectObject {public static void main(String[] args) throws Exception {//1.使用PropertyDescriptor得到想要注入的属性PropertyDescriptor descriptor = new PropertyDescriptor("animal", AnimalDao.class);//2.得到要想注入属性的具体对象Animal animal = (Animal) descriptor.getPropertyType().newInstance();//3.得到该属性的写方法Method method = descriptor.getWriteMethod();//4.得到写方法的注解Annotation annotation = method.getAnnotation(MyAnnotation6.class);//5.得到注解上的信息Method[] methods = annotation.getClass().getMethods();//6.将注解上的信息填充到animal对象上for (Method m : methods) {//得到注解上属性的名字String name = m.getName();//看看animal对象有没有与之对应的方法try {PropertyDescriptor descriptor1 = new PropertyDescriptor(name, Animal.class);Method method1 = descriptor1.getWriteMethod();//得到注解中的值Object o = m.invoke(annotation, null);//调用animal对象的setter方法 , 将注解上的值设置进去method1.invoke(animal, o);} catch (Exception e) {continue;}}AnimalDao animalDao = new AnimalDao();method.invoke(animalDao, animal);System.out.println(animalDao.getAnimal().getName());System.out.println(animalDao.getAnimal().getAge());}}@Target(value = http://kandian.youth.cn/index/{ElementType.METHOD, ElementType.TYPE})@Retention(value = RetentionPolicy.RUNTIME)@interface MyAnnotation6 {String name();int age();}复制代码【注解,其实就是这么简单】因此 , 将对象注入到方法中可以总结如下
  • 创建想要获得属性的对象
  • 根据对象获取该属性的方法
  • 得到方法中的注解
  • 获取注解中的信息
  • 将注解信息注入到对象中
  • 将对象中的属性写入到方法中
关注作者-私信:" Java "免费领取一份学习资料 , 包含(Java学习视频 , 技术文档 , 电子书籍 , 面试题等资料...)还可免费学习Java基础到项目实战课程!