What's the difference between interface and @interface in java? and defining an annotation type
今天细看了@PathVarible
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) // package org.springframework.web.bind.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.core.annotation.AliasFor; @Target({ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface PathVariable { @AliasFor("name") String value() default ""; @AliasFor("value") String name() default ""; boolean required() default true; }
上面代码是annotation的定义,它类似于接口的定义。
Many APIs require a fair amount of boilerplate boiler+plate n.(可供模仿的)样板文件 code. For example, in order to write a JAX-RPC(Java APIs for XML-based Remote Procedure Call ( JAX-RPC)) web service, you must provide a paired interface and implementation. This boilerplate could be generated automatically by a tool if the program were “decorated” with annotations indicating which methods were remotely accessible.
Other APIs require “side files” to be maintained in parallel with programs. For example JavaBeans requires a BeanInfo
class to be maintained in parallel with a bean, and Enterprise JavaBeans (EJB) requires a deployment descriptor. It would be more convenient and less error-prone if the information in these side files were maintained as annotations in the program itself.
The Java platform has always had various ad hoc([ˌæd ˈhɑk]adj.临时安排的;特别的;专门的 ) annotation mechanisms. For example the transient
modifier is an ad hoc annotation indicating that a field should be ignored by the serialization subsystem, and the @deprecated
javadoc tag is an ad hoc annotation indicating that the method should no longer be used. As of release 5.0, the platform has a general purpose annotation (also known as metadata) facility that permits you to define and use your own annotation types. The facility consists of a syntax for declaring annotation types, a syntax for annotating declarations, APIs for reading annotations, a class file representation for annotations, and an annotation processing tool.
Annotations do not directly affect program semantics [səˈmæntɪks]n.语义学;(单词、短语或其他符号系统的)含义
, but they do affect the way programs are treated by tools and libraries, which can in turn affect the semantics of the running program. Annotations can be read from source files, class files, or reflectively at run time.
Annotations complement javadoc tags. In general, if the markup is intended to affect or produce documentation, it should probably be a javadoc tag; otherwise, it should be an annotation.
Typical application programmers will never have to define an annotation type, but it is not hard to do so. Annotation type declarations are similar to normal interface declarations. An at-sign (@
) precedes the interface
keyword. Each method declaration defines an element of the annotation type. Method declarations must not have any parameters or a throws
clause. Return types are restricted to primitives, String
, Class
, enums, annotations, and arrays of the preceding types. Methods can have default values. Here is an example annotation type declaration:
/** * Describes the Request-For-Enhancement(RFE) that led * to the presence of the annotated API element. */ public @interface RequestForEnhancement { int id(); String synopsis(); String engineer() default "[unassigned]"; String date(); default "[unimplemented]"; }
Once an annotation type is defined, you can use it to annotate declarations. An annotation is a special kind of modifier, and can be used anywhere that other modifiers (such as public
, static
, or final
) can be used. By convention, annotations precede other modifiers. Annotations consist of an at-sign (@
) followed by an annotation type and a parenthesized[pə'renθəˌsaɪz]把…括入圆括号内 list of element-value pairs. The values must be compile-time constants. Here is a method declaration with an annotation corresponding to the annotation type declared above:
@RequestForEnhancement( id = 2868724, synopsis = "Enable time-travel", engineer = "Mr. Peabody", date = "4/1/3007" ) public static void travelThroughTime(Date destination) { ... }
An annotation type with no elements is termed a marker annotation type, for example:
/** * Indicates that the specification of the annotated API element * is preliminary and subject to change. */ public @interface Preliminary { }
It is permissible to omit the parentheses in marker annotations, as shown below:
@Preliminary public class TimeTravel { ... }
In annotations with a single element, the element should be named value
, as shown below:
/** * Associates a copyright notice with the annotated API element. */ public @interface Copyright { String value(); }
本文来自博客园,作者:z_s_s,转载请注明原文链接:https://www.cnblogs.com/zhoushusheng/p/15886114.html