Java中Annotation简单了解

  1. 什么是Annotation
    • Annotation是java对元数据(MetaData)的支持,写在代码中的特殊标记,这些标记可以在编译,加载类或者运行时被读取,并执行相应的处理
  2. Annotation的作用
    • Annotation可以使开发人员在不改变业务逻辑的情况下,在源文件中嵌入一些补充信息,这些补充信息可以传递给开发,分析,部署等工具使用。或者进行验证。

    • Annotation可以修饰包,类,构造,方法,成员变量,参数,局部变量的声明,将这些信息存放在Annotation的“name=value”中


  3. Annotation的分类
    1. 预定义的
      1. @Override表示重写,只能修饰方法,并表示该方法为重写的方法,如果父类没有该方法,在编译时会报错
      2. @Deprecated表示过期的,可用在任何地方,不建议继续使用
      3. @SupressWarnings表示压制警告
    2. 例如:
      • 父类
        public class Super {
        
        	public void eat() {
        		System.out.println("这是父类的方法,父类会吃东西");
        	}
        }

      • 子类
        public class Sub extends Super{
        	
        	@Override//表示该方法为重写的方法
        	public void eat() {
        		System.out.println("子类的eat方法");
        	}
        	
        	@Deprecated//表示该方法已过期,不建议使用
        	public void run() {
        		System.out.println("子类run方法");
        	}
        
        }

      • 测试方法
        public class TestAnnotation {
        	/**
        	 * Annotation叫做注解,主要作用是给元数据添加一些说明信息
        	 * Annotation分为两种,一种是预定义的(JDK提供的),另一种是自定义的
        	 * 预定义的分为三种:
        	 * 	@Override只能作用在方法上,说明此方法为重写的方法,如果父类没有该方法,加上该注解编译器会报错
        	 * 	@Deprecated过期的,表示不建议使用
        	 * 
        	 * @param args
        	 */
        	@SuppressWarnings("deprecation")//压制使用过期的方法的警告
        	public static void main(String[] args) {
        
        		Sub s=new Sub();
        		s.run();
        	}
        }
        

    3. 自定义的Annotation
      • 定义一个Annotation需要说明的信息
        1. @Retention:保留的时间,Scource, Class ,Runtime
          • JDK中关于Retention参数的源码
            public enum RetentionPolicy {
                /**
                 * Annotations are to be discarded by the compiler.
                 */
                SOURCE,
            
                /**
                 * Annotations are to be recorded in the class file by the compiler
                 * but need not be retained by the VM at run time.  This is the default
                 * behavior.
                 */
                CLASS,
            
                /**
                 * Annotations are to be recorded in the class file by the compiler and
                 * retained by the VM at run time, so they may be read reflectively.
                 *
                 * @see java.lang.reflect.AnnotatedElement
                 */
                RUNTIME
            }

        2. @Target:Type ,Field ,Constructor
          • Target中定义的参数为一个ElementType类型的数组,所以在传参的时候应以数组的形式传递
            @Documented
            @Retention(RetentionPolicy.RUNTIME)
            @Target(ElementType.ANNOTATION_TYPE)
            public @interface Target {
                /**
                 * Returns an array of the kinds of elements an annotation type
                 * can be applied to.
                 * @return an array of the kinds of elements an annotation type
                 * can be applied to
                 */
                ElementType[] value();
            }

          • JDK关于Target参数的源码
            public enum ElementType {
                /** Class, interface (including annotation type), or enum declaration */
                TYPE,
            
                /** Field declaration (includes enum constants) */
                FIELD,
            
                /** Method declaration */
                METHOD,
            
                /** Formal parameter declaration */
                PARAMETER,
            
                /** Constructor declaration */
                CONSTRUCTOR,
            
                /** Local variable declaration */
                LOCAL_VARIABLE,
            
                /** Annotation type declaration */
                ANNOTATION_TYPE,
            
                /** Package declaration */
                PACKAGE,
            
                /**
                 * Type parameter declaration
                 *
                 * @since 1.8
                 */
                TYPE_PARAMETER,
            
                /**
                 * Use of a type
                 *
                 * @since 1.8
                 */
                TYPE_USE
            }

        3. Annotation的属性(参数)
        • @Table(表名)放在类 的上方,表示这个类对应表(避免了表名和java中的关键字重复的问题)
      • 定义Annotation属性的声明
        1. 属性的定义格式: 数据类型 属性名()  [default 默认值];
          • String value() default "book";//属性名为value,默认值book
          • String name();
          • int[] a;//整型数组类型的属性参数
        2. 每个Annotation中可以包含多个属性
          • 当注解中只包含一个参数时,默认的属性名为value,在传参的时候可以省去value=参数(不明确指出指定的属性名);有多个参数时或者参数只有一个且不是value时,需要写明 属性名=参数;
          • 例子
            import java.lang.annotation.Retention;
            import java.lang.annotation.Target;
            
            //该注解存活的时间,括号中的参数为表示时间为整个RUNTIME过程
            @Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
            //表示该注解作用的范围
            @Target(java.lang.annotation.ElementType.TYPE)
            public @interface Table {
            
            	//注解的String类型的参数,默认值为tb_book,默认值可以不写
            	//value为默认的属性名称,当只有一个属性且属性名为value时,在使用注解时可以省略value=?
            	String value() default "tb_book";
            	String name();
            	//整数类型的参数
            	int id();
            	//数组类型的参数
            	String[] hobby();
            }

        3. 自定义的Annotation分为两种
          1. 标识的Annotation(无属性)
          2. 可添加属性的Annotation
    4. 获得Annotation的方法
      1. Annotation[]  Class.getAnnotations();获得当前类中定义的所有RUNTIME类型的Annotation
      2. Annotation  Class.getAnnotation(Class c);获得当前类中c类型的Annotation,如果没有则返回null
      3. (其他方法)boolean Class.isAnnotationPresent(Class<? extends Annotation> annotationClass)   如果指定类型的注释存在于此元素上,则返回 true,否则返回 false。
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值