本文共 10379 字,大约阅读时间需要 34 分钟。
java注解
Java Annotations provides information about the code. Java annotations have no direct effect on the code they annotate. In java annotations tutorial, we will look into the following;
Java注释提供有关代码的信息。 Java注释对其注释的代码没有直接影响。 在Java批注教程中,我们将研究以下内容;
Java 1.5 introduced annotations and now it’s heavily used in Java EE frameworks like Hibernate, , and .
Java 1.5引入了注释,现在它已在Hibernate, 和等Java EE框架中大量使用。
Java Annotation is metadata about the program embedded in the program itself. It can be parsed by the annotation parsing tool or by the compiler. We can also specify annotation availability to either compile time only or till runtime.
Java注释是有关嵌入在程序本身中的程序的元数据。 可以通过注释解析工具或编译器进行解析。 我们还可以指定注释可用性以仅编译时间或直到运行时。
Before java annotations, program metadata was available through java comments or by Javadoc but annotation offers more than that. Annotations metadata can be available at runtime too and annotation parsers can use it to determine the process flow.
在使用Java注释之前,程序元数据可通过Java注释或Javadoc获得,但注释提供的功能更多。 注释元数据也可以在运行时使用,注释解析器可以使用它来确定流程。
For example, in we add PATH annotation with URI string to a method and at runtime, jersey parses it to determine the method to invoke for given URI pattern.
例如,在我们将带有URI字符串的PATH注释添加到方法中,并且在运行时,jersey对其进行解析,以确定要为给定URI模式调用的方法。
Creating custom annotation is similar to writing an interface, except that the interface keyword is prefixed with @ symbol. We can declare methods in annotation.
创建自定义批注与编写接口相似,不同之处在于interface关键字的前缀是@符号。 我们可以在注解中声明方法。
Let’s see java custom annotation example and then we will discuss its features and important points.
让我们看一下Java自定义注释示例,然后我们将讨论其功能和要点。
package com.journaldev.annotations;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Inherited;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Documented@Target(ElementType.METHOD)@Inherited@Retention(RetentionPolicy.RUNTIME)public @interface MethodInfo{ String author() default "Pankaj"; String date(); int revision() default 1; String comments();}
Some important points about java annotations are:
关于Java注释的一些重要点是:
There are five types of meta annotations:
元注释有五种类型:
Java Provides five built-in annotations.
Java提供五个内置注释。
@Override
– When we want to override a method of Superclass, we should use this annotation to inform compiler that we are overriding a method. So when superclass method is removed or changed, compiler will show error message. Learn why we should always use while overriding a method. @Override
–当我们要重写超类的方法时,应使用此批注通知编译器我们正在重写方法。 因此,当删除或更改超类方法时,编译器将显示错误消息。 了解为什么我们在覆盖方法时总是应该使用 。 @Deprecated
– when we want the compiler to know that a method is deprecated, we should use this annotation. Java recommends that in javadoc, we should provide information for why this method is deprecated and what is the alternative to use. @Deprecated
–当我们希望编译器知道某个方法已被弃用时,应使用此注释。 Java建议在javadoc中,我们应提供有关为什么不赞成使用此方法以及可以使用什么替代方法的信息。 @SuppressWarnings
– This is just to tell compiler to ignore specific warnings they produce, for example using raw types in . It’s retention policy is SOURCE and it gets discarded by compiler. @SuppressWarnings
–这只是告诉编译器忽略它们产生的特定警告,例如使用原始类型。 它的保留策略是SOURCE,并且被编译器丢弃。 @FunctionalInterface
– This annotation was introduced in to indicate that the interface is intended to be a . @FunctionalInterface
– 中引入了此批注,以指示该接口旨在用作 。 @SafeVarargs
– A programmer assertion that the body of the annotated method or constructor does not perform potentially unsafe operations on its varargs parameter. @SafeVarargs
–程序员断言带注释的方法或构造函数的主体不会对其varargs参数执行潜在的不安全操作。 Let’s see a java example showing the use of built-in annotations in java as well as the use of custom annotation created by us in the above example.
让我们看一个Java示例,该示例显示在Java中使用内置注释以及在上面的示例中由我们创建的自定义注释的使用。
package com.journaldev.annotations;import java.io.FileNotFoundException;import java.util.ArrayList;import java.util.List;public class AnnotationExample { public static void main(String[] args) { } @Override @MethodInfo(author = "Pankaj", comments = "Main method", date = "Nov 17 2012", revision = 1) public String toString() { return "Overriden toString method"; } @Deprecated @MethodInfo(comments = "deprecated method", date = "Nov 17 2012") public static void oldMethod() { System.out.println("old method, don't use it."); } @SuppressWarnings({ "unchecked", "deprecation" }) @MethodInfo(author = "Pankaj", comments = "Main method", date = "Nov 17 2012", revision = 10) public static void genericsTest() throws FileNotFoundException { List l = new ArrayList(); l.add("abc"); oldMethod(); }}
I believe above java annotation example is self-explanatory and showing the use of annotations in different cases.
我相信上面的Java注释示例是不言自明的,并显示了在不同情况下使用注释的情况。
We will use Reflection to parse java annotations from a class. Please note that Annotation Retention Policy should be RUNTIME otherwise its information will not be available at runtime and we won’t be able to fetch any data from it.
我们将使用Reflection来解析类中的Java批注。 请注意,注释保留策略应为RUNTIME,否则其信息将在运行时不可用,我们将无法从中获取任何数据。
package com.journaldev.annotations;import java.lang.annotation.Annotation;import java.lang.reflect.Method;public class AnnotationParsing { public static void main(String[] args) { try { for (Method method : AnnotationParsing.class.getClassLoader() .loadClass(("com.journaldev.annotations.AnnotationExample")).getMethods()) { // checks if MethodInfo annotation is present for the method if (method.isAnnotationPresent(com.journaldev.annotations.MethodInfo.class)) { try { // iterates all the annotations available in the method for (Annotation anno : method.getDeclaredAnnotations()) { System.out.println("Annotation in Method '" + method + "' : " + anno); } MethodInfo methodAnno = method.getAnnotation(MethodInfo.class); if (methodAnno.revision() == 1) { System.out.println("Method with revision no 1 = " + method); } } catch (Throwable ex) { ex.printStackTrace(); } } } } catch (SecurityException | ClassNotFoundException e) { e.printStackTrace(); } }}
Output of the above program is:
上面程序的输出是:
Annotation in Method 'public java.lang.String com.journaldev.annotations.AnnotationExample.toString()' : @com.journaldev.annotations.MethodInfo(author=Pankaj, revision=1, comments=Main method, date=Nov 17 2012)Method with revision no 1 = public java.lang.String com.journaldev.annotations.AnnotationExample.toString()Annotation in Method 'public static void com.journaldev.annotations.AnnotationExample.oldMethod()' : @java.lang.Deprecated()Annotation in Method 'public static void com.journaldev.annotations.AnnotationExample.oldMethod()' : @com.journaldev.annotations.MethodInfo(author=Pankaj, revision=1, comments=deprecated method, date=Nov 17 2012)Method with revision no 1 = public static void com.journaldev.annotations.AnnotationExample.oldMethod()Annotation in Method 'public static void com.journaldev.annotations.AnnotationExample.genericsTest() throws java.io.FileNotFoundException' : @com.journaldev.annotations.MethodInfo(author=Pankaj, revision=10, comments=Main method, date=Nov 17 2012)
Reflection API is very powerful and used widely in Java, J2EE frameworks like Spring, , JUnit, check out .
Reflection API非常强大,并且在Java,Spring, ,JUnit等J2EE框架中得到了广泛使用,并在Java中签出了 。
That’s all for the java annotations example tutorial, I hope you learned something from it.
这就是Java注释示例教程的全部内容,希望您从中学到一些东西。
Java Annotations Updates:
Java注释更新 :
Reference:
参考:
翻译自:
java注解
转载地址:http://eaozd.baihongyu.com/