Java注解

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

1. 什么是注解

注解是 Java 5 的一个新特性。注解是插入你代码中的一种注释或者说是一种元数据(meta data)。这些注解信息可以在编译期使用预编译工具进行处理(PRe-compiler tools),也可以在运行期使用 Java 反射机制进行处理。下面是一个类注解的例子:

@MyAnnotation(name="someName",  value = "Hello World") public class TheClass { } 

注解的处理除了可以在运行时通过反射机制处理外,还可以在编译期进行处理,参考这个

2. 元注解/自定义注解

元注解的作用就是负责注解其他注解。Java5.0定义了4个标准的meta-annotation类型,它们被用来提供对其它 annotation类型作说明。Java5.0定义的元注解:
 1.@Target,
 2.@Retention,
 3.@Documented,
 4.@InherITed
这些类型和它们所支持的类在java.lang.annotation包中可以找到。下面我们看一下每个元注解的作用和相应分参数的使用说明。 元注解和自定义注解的具体内容参考这里

3. 利用Java反射机制可以在运行期处理注解信息

你可以在运行期访问类,方法或者变量的注解信息.其核心还是反射.
看个例子:

package com.demo.java;  import java.lang.annotation.Annotation; import java.lang.reflect.Method;  @MyAnnotation(value = "test for class ", url = "www.baidu.com") public class AnnotationUsageTest {      @Deprecated     @MyAnnotation(value = "test for method", url = "www.google.com")     public void myMethod() {          System.out.println("my method have been used!");     }      public void outPutUrl(@MyAnnotation(             value = "test for params",             url = "www.bing.com") String kind) throws Exception {          Class<AnnotationUsageTest> c = AnnotationUsageTest.class;         Method method = c.getMethod("outPutUrl", new Class[]{String.class});         Annotation[][] parameterAnnotations = method.getParameterAnnotations();         Class[] params = method.getParameterTypes();         int i = 0;         for (Annotation[] annotations : parameterAnnotations) {             Class param = params[i];             for (Annotation annotation : annotations) {                 if (annotation instanceof MyAnnotation) {                     MyAnnotation myAnnotation = (MyAnnotation) annotation;                     System.out.println("value is :" + myAnnotation.value());                     System.out.println("url is" + myAnnotation.url());                 }             }         }     }      public static void main(String[] args) throws Exception {         //1.访问方法的所有注解         Class<AnnotationUsageTest> c = AnnotationUsageTest.class;         Method method = c.getMethod("myMethod", new Class[]{});          Annotation[] annotations = method.getDeclaredAnnotations();         for (Annotation annotation : annotations) {             if (annotation instanceof MyAnnotation) {                 MyAnnotation myAnnotation = (MyAnnotation) annotation;                 System.out.println(myAnnotation.value());                 System.out.println(myAnnotation.url());             } else {                 System.out.println(annotation.toString());             }         }          //2. 访问方法的特定的注解         if (method.isAnnotationPresent(MyAnnotation.class)) {             AnnotationUsageTest test = new AnnotationUsageTest();             method.invoke(test, new Object[]{});             MyAnnotation myAnnotation_certain = method.getAnnotation(MyAnnotation.class);             System.out.println("method annotation test 2:" + myAnnotation_certain.value() + " and url is :" + myAnnotation_certain.url());         }          //3. 访问类注解         Class<AnnotationUsageTest> classType = AnnotationUsageTest.class;         Annotation[] annotation_array = classType.getDeclaredAnnotations();         for (Annotation annotation : annotation_array) {             if (annotation instanceof MyAnnotation) {                 MyAnnotation myAnnotation = (MyAnnotation) annotation;                 System.out.println(myAnnotation.value());                 System.out.println(myAnnotation.url());             }         }          //4.测试参数注解         //将注解的获取放到方法本身里面了,同样可以运行         AnnotationUsageTest annotationUsageTest = new AnnotationUsageTest();         annotationUsageTest.outPutUrl(null);       } } 
package com.demo.java; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;  @Retention(value = RetentionPolicy.RUNTIME) public @interface MyAnnotation {  String value() default "test for annotation";     String url(); } 

主要参考的是[http://tutorials.jenkov.com/java-reflection/annotations.html]

另外,这个文章里的例子也不错.(http://www.cnblogs.com/peida/archive/2013/04/26/3038503.html)
下面的图也是COPY自这个文章的.

Java注解

脚本宝典总结

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

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

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