- 什么是Annotation
- Annotation是java对元数据(MetaData)的支持,写在代码中的特殊标记,这些标记可以在编译,加载类或者运行时被读取,并执行相应的处理
- Annotation的作用
-
Annotation可以使开发人员在不改变业务逻辑的情况下,在源文件中嵌入一些补充信息,这些补充信息可以传递给开发,分析,部署等工具使用。或者进行验证。
-
Annotation可以修饰包,类,构造,方法,成员变量,参数,局部变量的声明,将这些信息存放在Annotation的“name=value”中。
-
- Annotation的分类
- 预定义的
- @Override表示重写,只能修饰方法,并表示该方法为重写的方法,如果父类没有该方法,在编译时会报错
- @Deprecated表示过期的,可用在任何地方,不建议继续使用
- @SupressWarnings表示压制警告
- 例如:
- 父类
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(); } }
- 父类
- 自定义的Annotation
- 定义一个Annotation需要说明的信息
- @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 }
- JDK中关于Retention参数的源码
- @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 }
- Target中定义的参数为一个ElementType类型的数组,所以在传参的时候应以数组的形式传递
- Annotation的属性(参数)
- @Table(表名)放在类 的上方,表示这个类对应表(避免了表名和java中的关键字重复的问题)
- @Retention:保留的时间,Scource, Class ,Runtime
- 定义Annotation属性的声明
- 属性的定义格式: 数据类型 属性名() [default 默认值];
- String value() default "book";//属性名为value,默认值book
- String name();
- int[] a;//整型数组类型的属性参数
- 每个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(); }
- 自定义的Annotation分为两种
- 标识的Annotation(无属性)
- 可添加属性的Annotation
- 属性的定义格式: 数据类型 属性名() [default 默认值];
- 定义一个Annotation需要说明的信息
- 获得Annotation的方法
- Annotation[] Class.getAnnotations();获得当前类中定义的所有RUNTIME类型的Annotation
- Annotation Class.getAnnotation(Class c);获得当前类中c类型的Annotation,如果没有则返回null
- (其他方法)boolean Class.isAnnotationPresent(Class<? extends Annotation> annotationClass) 如果指定类型的注释存在于此元素上,则返回 true,否则返回 false。
- 预定义的
Java中Annotation简单了解
最新推荐文章于 2024-11-29 09:43:31 发布
本文深入讲解Java中的注解(Annotation)概念,包括其定义、作用、分类及如何使用。介绍了预定义注解如@Override、@Deprecated等的功能,并详细阐述自定义注解的创建方法。
27万+

被折叠的 条评论
为什么被折叠?



