目录
一、Java自定义注解详解
1.定义注解:
注解的定义很像接口的定义。事实上与其他java接口一样,注解也会被编译成class文件。定义注解时需要一些元注解。
2.元注解介绍
@Target详细介绍
值 | 描述 |
---|---|
ElementType.TYPE | 接口、类、枚举、注解 |
ElementType.FIELD | 字段、枚举的常量 |
ElementType.METHOD | 方法 |
ElementType.PARAMETER | 方法参数 |
ElementType.CONSTRUCTOR | 构造函数 |
ElementType.LOCAL_VARIABLE | 局部变量 |
ElementType.ANNOTATION_TYPE | 注解 |
ElementType.PACKAGE | 包 |
@Relation详细介绍
值 | 描述 |
---|---|
RetentionPolicy.SOURCE | 注解将被编译器丢弃,此处是源码阶段也就是javac编译时 |
RetentionPolicy.CLASS | 注解在class中可用,但会被vm丢弃 |
RetentionPolicy.RUNTIME | vm运行期间也会保留注解,可以使用反射机制读取注解的信息 |
@Documented介绍
将此注解包含在javadoc中
@Inherited介绍
允许子类继承父类中的注解,千万不要误解是注解的嵌套,是Class类继承的时候,是否拥有父类的注解
3.注解可用的类型
-
所有基本类型(int、float、double,boolean)
-
String
-
Class
-
enum
-
Annotaion
-
以上类型的数组
注解可以嵌套注解
4.默认值限制
元素必须具有默认值,默认值不能是null
注解不支持继承,不像类一样可以通过extends继承
5.创建一个简单的自定义注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface CustomAnnotation {
}
二、读取注解上的信息
通过反射机制去获取注解上的信息
-
创建各种类型的注解
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited public @interface AnnotationForClass { String value(); } @Target(ElementType.CONSTRUCTOR) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited public @interface AnnotationForConstructor { String value(); } @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited public @interface AnnotationForField { String value(); } @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited public @interface AnnotationForMethod { String value(); } @Target(ElementType.PARAMETER) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited public @interface AnnotationForParam { String value(); }
-
带有各种注解的测试类
@AnnotationForClass(value = "AnnotationForClassValue") public class Test { @AnnotationForField(value = "AnnotationForFieldValue") private String name; @AnnotationForMethod(value = "AnnotationForMethodValue") public String getName() { return this.name; } public void setName(@AnnotationForParam(value = "AnnotationForParamValue") String name) { this.name = name; } @AnnotationForConstructor(value = "AnnotationForConstructorValue") public Test() { } }
-
获取注解以及注解上的信息
public class Main { // getDeclaredAnnotations 获取所有的注解 // getDeclaredAnnotation 获取指定的注解 public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException { Class<Test> testClazz = Test.class; // 获取(类、接口、枚举、注解)上注解的 AnnotationForClass annotationForClass = testClazz.getDeclaredAnnotation(AnnotationForClass.class); System.out.println("类上的注解的值\t" + annotationForClass.value()); // 获取类中构造器的注解 Constructor<Test> constructor = testClazz.getConstructor(); AnnotationForConstructor annotationForConstructor = constructor.getDeclaredAnnotation(AnnotationForConstructor.class); System.out.println("类中构造器的注解的值\t" + annotationForConstructor.value()); // 获取类中字段的注解 Field nameField = testClazz.getDeclaredField("name"); AnnotationForField annotationForField = nameField.getDeclaredAnnotation(AnnotationForField.class); System.out.println("类中字段的注解的值\t" + annotationForField.value()); // 获取类中方法的注解 Method getNameMethod = testClazz.getDeclaredMethod("getName"); AnnotationForMethod annotationForMethod = getNameMethod.getDeclaredAnnotation(AnnotationForMethod.class); System.out.println("类中方法的注解的值\t" + annotationForMethod.value()); // 获取方法上参数的注解 Method setNameMethod = testClazz.getDeclaredMethod("setName", String.class); AnnotationForParam annotationForParam = setNameMethod.getDeclaredAnnotation(AnnotationForParam.class); System.out.println("类中方法的参数的注解的值\t" + annotationForParam.value()); } }
-
运行结果
类上的注解的值 AnnotationForClassValue
类中构造器的注解的值 AnnotationForConstructorValue
类中字段的注解的值 AnnotationForFieldValue
类中方法的注解的值 AnnotationForMethodValue
类中方法的参数的注解的值 AnnotationForParamValue
原文地址:http://www.cnblogs.com/linmt/p/16909142.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性