元数据
要了解注解前提要了解有关元数据的一些概念。
元数据是关于数据的数据。在编程语言上下文中,元数据是添加到程序元素如方法、字段、类和包上的额外信息。对数据进行说明描述的数据。
一般来说,元数据可以用于创建文档,跟踪代码中的依赖性,执行编译时检查,代码分析。
什么是注解
注解Annotation就是java平台的元数据,该机制允许在Java代码中添加自定义注释,并允许通过反射(Reflection),以编程方式访问元数据注释。通过提供为程序元素(类、方法等)附加额外数据的标准方法,元数据功能具有简化和改进许多应用程序开发领域的潜在能力,其中包括配置管理、框架实现和代码生成。
Annotations are primarily used by code that is inspecting other code. They are often used for modifying (i.e. decorating or wrapping) existing classes at run-time to change their behavior.
Annotations are meta-meta-objects which can be used to describe other meta-objects. Meta-objects are classes, fields and methods. Asking an object for its meta-object (e.g. anObj.getClass() ) is called introspection. The introspection can go further and we can ask a meta-object what are its annotations (e.g. aClass.getAnnotations). Introspection and annotations belong to what is called reflection and meta-programming.
注解需要以某种方式解释才能有用。注解可是在开发时由IDE或编译器解释,也可以在运行时由框架解释。
注解是一种非常强大的机制,可以用很多不同的方法:
- 描述一个元素的约束或用法:例如@Deprecated、@Override或@NotNull。
- 描述元素的“性质”,例如@Entity, @TestCase, @WebService
- 描述元素的行为:@Statefull, @Transaction
- 描述如何处理元素:@Column, @XmlElement
注解分类
There are 3 categories of Annotations:
- Marker Annotation
- Single Value Annotation
- Full Annotation
Java defines seven built-in annotations.
- Four are imported from java.lang.annotation: @Retention, @Documented, @Target, and @Inherited.
- Three are included in java.lang: @Deprecated, @Override and @SuppressWarnings
@Target
- ANNOTATION_TYPE
- CONSTRUCTOR
- FIELD
- LOCAL_VARIABLE
- METHOD
- PACKAGE
- PARAMETER
- TYPE
@Retention
- SOURCE
- CLASS
- RUNTIME
注解的提取
正确检阅注解,使用反射。
1 | public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {} |
注解的使用场景
- 文档,例如XDoclet
- 编译
- IDE
- 测试框架,例如JUnit
- IoC容器,如弹簧
- XML序列化,例如
- 面向方面的编程(AOP),例如Spring AOP。
- 应用服务器,例如EJB容器、Web服务
- 对象关系映射(ORM),例如Hibernate、JPA
- 和更多的……
Annotations can be used for many different purposes, the most common ones are:
Information for the compiler: Annotations can be used by the compiler to produce warnings or even errors based on different rules. One example of this kind of usage is the Java 8 @FunctionalInterface annotation. This one makes the compiler to validate the annotated class and check if it is a correct functional interface or not.
编译器可以利用注解来探测错误和警告信息
Documentation: Annotations can be used by software applications to measure the quality of the code like FindBugs or PMD do or generate reports automatically like Jenkins, Jira or Teamcity.
Code generation: annotations can be used to generate code or XML files automatically using metadata information present in the code. A good example of this is the JAXB library.
软件工具可以用来利用注解信息来生成代码、文档或者做其他相应处理
Runtime processing: Annotations that are examined in runtime can be used for different objectives like unit testing (Junit), dependency injection (Spring), validation, logging (Log4J) ,data access (Hibernate) etc.
某些注解可以在程序运行的时候接受代码的提取
用户定义/定制注解
User-defined/Custom Annotations
1 | Syntax of Declaration |
Spring注解
- @Component:表示此注释标记的元素,在本例中为类,是自动检测的候选对象。这意味着带注释的类可能是bean,应该由Spring容器考虑。
- @Autowired: Spring容器将尝试在这个setter方法上执行byType自动连接(这是一种使用元素类型的属性匹配)。它也可以应用于构造函数和属性,Spring容器在这些情况下采取的操作是不同的。
Reference
https://zhuanlan.zhihu.com/p/27643133
https://stackoverflow.com/questions/1372876/how-and-where-are-annotations-used-in-java/1918154
https://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why