Java知识点总结(注解-解析注解)

发布时间:2019-11-17 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Java知识点总结(注解-解析注解)脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

Java知识点总结(注解-解析注解)

@(Java知识点总结)[Java, 注解]

通过反射获取类、函数或成员上的运行时注解信息,从而实现动态控制程序运行的逻辑。

使用注解步骤:

  1. 定义注解
  2. 类中使用注解
  3. 解析注解

示例:

import java.lang.annotation.ElementTyPE; import java.lang.annotation.Retention; import java.lang.annotation.Retentionpolicy; import java.lang.annotation.Target;   @Target(value={ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface MethodAnnotation {   String value(); }
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;   @Target(value={ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) public @interface FieldAnnotation {   String columnName();   String type();   int length(); }
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 TableAnnotation {   String value(); }
@TableAnnotation("tb_student") public class Student {   @FieldAnnotation(columnName="id",type="int",length=10)   PRivate int id;   @FieldAnnotation(columnName="sname",type="vArchar",length=10)   private String name;   @FieldAnnotation(columnName="age",type="int",length=3)   private int age;      @MethodAnnotation("get方法上的注解")   public int getId() {    return id;   }   @MethodAnnotation("set方法上的注解")   public void setId(int id) {    this.id = 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;   }    }
import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Method;   public class Demo3 {     // 使用反射获取注解,生成类对应的数据库   private static void test1(Class clazz) {       // 获得类所有的注解     Annotation[] annotations = clazz.getAnnotations();     for (Annotation a : annotations) {      System.out.println(a);     }       // 通过注解的名字获取注解     TableAnnotation t = (TableAnnotation) clazz.getAnnotation(TableAnnotation.class);     System.out.println(t.value());       // 获得属性上的注解     try {      Field f = clazz.getDeclaredField("age");      FieldAnnotation fa = f.getAnnotation(FieldAnnotation.class);        System.out.println("字段名称:" + fa.columnName() + ",字段类型:" + fa.type() + ",字段长度:" + fa.length());       } catch (NoSuchFieldException | SecurITyException e) {      e.printStackTrace();     }       // 根据获得的表名、字段信息,拼出DDL语句,然后使用JDBC执行这个SQL语句,在数据库中生成相关的表     }     //获取方法上的注解   private static void test2(Class clazz) {     // 获取所有方法上的注解     Method[] ms = clazz.getMethods();     for (Method m : ms) {      boolean isExist = m.isAnnotationPresent(MethodAnnotation.class);      if (isExist) {        MethodAnnotation ma = m.getAnnotation(MethodAnnotation.class);        System.out.println(ma);      }     }       // 获取指定方法上的注解     try {      Method method = clazz.getMethod("getId", null);      Annotation[] as = method.getAnnotations();      for (Annotation a : as) {        if (a instanceof MethodAnnotation) {          MethodAnnotation ma = (MethodAnnotation) a;          System.out.println(ma);        }      }       } catch (NoSuchMethodException | SecurityException e) {      e.printStackTrace();     }   }     public static void main(String[] args) {     Class clazz = null;     try {      clazz = Class.forName("com.gs.Student");     } catch (ClassNotFoundException e) {      e.printStackTrace();     }     test1(clazz);       test2(clazz);     }   }

脚本宝典总结

以上是脚本宝典为你收集整理的Java知识点总结(注解-解析注解)全部内容,希望文章能够帮你解决Java知识点总结(注解-解析注解)所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。