了解Java中的注解
2020-06-05 16:24
330 查看
介绍
本文将讨论用Java创建注解的方法。还有如何将注解应用于其他声明。最后,讨论一下如何在运行时使用Reflection获得注解信息。
背景
注解是J2SE 5引入的一项新功能,允许程序员将称为元数据的其他信息嵌入Java源文件中。注解不会改变程序的执行,但是在开发和部署期间,各种工具都可以使用使用注解嵌入的信息。
使用代码
创建注解与创建接口相似。只不过注解声明前面有一个
@符号。注解声明本身使用注解进行
@Retention注解。该
@Retention注解用于指定保留策略,可以是
SOURCE,
CLASS或
RUNTIME。
RetentionPolicy.SOURCE
仅在源文件中保留注解,并在编译期间将其丢弃。RetentionPolicy.CLASS
将注解存储在.class文件中,但在运行时不可用。RetentionPolicy.RUNTIME
将注解存储在.class文件中,并使其在运行时可用。
// Specifying runtime retention policy @Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation { String author(); // Annotation member String date(); // Annotation member }
注解会隐式扩展
Annotation接口。注解的主体由不带主体的方法声明组成。这些方法像字段一样工作。
在上面的例子中,我已经创建了两个部件,
author并
date以表示关于创建者的信息,并且类和方法的写入日期。
创建注解后,可以将其应用于类,方法,字段,参数,枚举等。在对声明应用注解时,必须为其成员提供如下值:
// Applying annotation to the class @MyAnnotation(author="Azim",date="22/10/2011,23/10/2011") public class Test { // Applying annotation to the method @MyAnnotation(author="Azim",date="22/10/2011") public static void testMethod() { System.out.println("Welcome to Java"); System.out.println("This is an example of Annotations"); } public static void main(String args[]) { testMethod(); showAnnotations(); }
可以在运行时使用Reflection来查询注解,如下所示:
public static void showAnnotations() // Function to show annotation information { Test test=new Test(); // Instantiating Test class try { Class c=test.getClass(); // Getting Class reference Method m=c.getMethod("testMethod"); // Getting Method reference // Getting Class annotation MyAnnotation annotation1= (MyAnnotation)c.getAnnotation(MyAnnotation.class); // Getting Method annotation MyAnnotation annotation2=m.getAnnotation(MyAnnotation.class); // Displaying annotation information System.out.println("Author of the class: "+annotation1.author()); // Displaying annotation information System.out.println("Date of Writing the class: "+annotation1.date()); // Displaying annotation information System.out.println("Author of the method: "+annotation2.author()); // Displaying annotation information System.out.println("Date of Writing the method: "+annotation2.date()); } catch(NoSuchMethodException ex) { System.out.println("Invalid Method..."+ex.getMessage()); } }
在上面的代码中,我查询了应用于类
Test以及method的注解
testMethod()。
要获取该类的注解信息,请使用以下语句:
MyAnnotation annotation1=(MyAnnotation)c.getAnnotation(MyAnnotation.class);
要获取该方法的注解信息,请使用以下语句:
MyAnnotation annotation2=m.getAnnotation(MyAnnotation.class);
通过使用注解对象来打印注解信息。
问题:注解声明后,在运行期如何查询它的信息?
答:使用Reflection来查询注解
相关文章推荐
- 黑马程序员-了解注解及java提供的几个基本注解
- 分析Spring 源码前需要了解的java自定义注解
- java注解的简单了解基础知识
- 黑马程序员之Java--了解注解及其应用
- JAVA基础学习之IP简述使用、反射、正则表达式操作、网络爬虫、可变参数、了解和入门注解的应用、使用Eclipse的Debug功能(7)
- Spring Framework 5.0:使用注解的方式来加载Bean、入门注解、了解java反射
- Java注解的简单了解
- Java篇 - 深入了解注解
- 一张图了解 Java 注解(Annotation)
- 了解Java中的注解
- Java注解初步了解
- 了解注解及java提供的几个基本注解
- Android注解学习之了解Java动态代理Proxy
- Java篇 - 深入了解注解
- 由浅入深 带你了解 JAVA 注解
- 通过项目了解JAVA注解
- Java基本注解以及了解自定义注解
- 黑马程序员_了解注解及java提供的几个基本注解
- 了解和使用Java中的注解
- 想自己写框架?不了解Java注解(Annotation)机制可不行,来跟我一起通过源码深入Java注解机制