1. Introduction to Annotations
When developing Java programs, especially Java EE applications, it is always unavoidable to deal with a variety of configuration files. With the typical S (pring) s (truts) H (ibernate) architecture in Java EE, the three frameworks of Spring, struts, and hibernate have their own XML-formatted configuration files. These configuration files need to be synchronized with the Java source code, otherwise there may be errors. And these errors are likely to be discovered at run time. Keeping the same piece of information in two places is always a bad idea. Ideally, it would be better to maintain this information in one place. The information required by other parts is generated in an automated manner. The mechanism of annotations (annotation) in the source code is introduced in JDK5. Annotations allow the Java source to include not only the functional implementation code, but also the addition of metadata. The functionality of annotations is similar to comments in code, and the difference is that annotations are not a description of the functionality of the code, but rather an important part of implementing the program's functionality. Java annotations have been widely used in many frameworks to simplify configuration in the program.
A. Generate the document. This is the most common, and also the earliest annotations that Java provides. Commonly used have @see @param @return and so on;
B. Tracking code dependencies, implementing alternative profile features. The most common is the annotation-based configuration that starts with spring 2.5. The function is to reduce the configuration. Now the framework basically uses this configuration to reduce the number of configuration files;
C. Format check at compile time. such as @override before the method, if you do not override the method of the superclass method, compile time can be checked out.
2. Using Annotations
The syntax for
annotations is simple, except for the use of the @ symbol, which is essentially consistent with the Java intrinsic syntax. The Java SE5 contains three standard annotation: @Override that indicate that the current method definition overrides methods in the superclass. @Deprecated, using annotations for its elements the compiler will issue a warning because annotations @deprecated is deprecated code that is not approved for use. @SuppressWarnings, turn off improper compiler warning messages. The above three annotations how much we will be encountered when writing code. Java also provides 4 of annotations, specifically responsible for the creation of new annotations.
|
@Target |
Indicates where the annotation can be used, the possible ElementType parameters are:
CONSTRUCTOR: Declaration of the constructor
Field: Domain declaration (including enum instance)
local_variable: local variable declaration
Method: Methods declaration
Package: Packet declaration
PARAMETER: Parameter declaration
Type: class, interface (including annotation type), or enum declaration |
|
@Retention |
Indicates at what level the annotation information needs to be saved. The optional retentionpolicy parameters include:
SOURCE: Annotations are discarded by the compiler
CLASS: Annotations are available in the class file, but are discarded by VMS
RUNTIME:VM will retain annotations during run time. The information of the annotations can therefore be read through the reflection mechanism. |
|
@Document |
include annotations in Ja Vadoc |
|
@Inherited |
Allowed Xu Zi class inherits annotations from parent class |
in the General Java development, the most frequent contact may be @override and @supresswarnings these two annotations. When using @override, you only need a simple declaration. This is called the markup Annotation (markerannotation), which appears to represent a certain configuration semantics. Other annotations can have their own configuration parameters. Configuration parameters appear in the form of a name-value pair. The use of @SupressWarnings requires a syntax like @supresswarnings ({"Uncheck", "unused"}). Inside the parentheses is the value that the annotation is available for configuration. Since this annotation has only one configuration parameter, the name of the parameter defaults to value and can be omitted. The curly braces indicate the array type. The @table annotations in JPA use a syntax like @table (name = "Customer", schema = "APP"). You can see the usage of the name-value pairs from here. The value of the configuration parameter when using annotations must be a constant at compile time. in Some ways, annotations can be thought of as an XML element that can have different predefined attributes. The value of a property can be specified by itself when declaring the element. Using annotations in your code is equivalent to moving part of the metadata from an XML file into the code itself, and managing and maintaining it in one place.
3. Development Notes
In general development, you only need to read the relevant API documentation to understand the meaning of the configuration parameters for each annotation and use it correctly in your code. In some cases, it may be necessary to develop your own annotations. This is more common in the development of libraries. Annotations are defined somewhat like interfaces. The following code gives a simple note describing the arrangement of code Division. This annotation allows you to record the division and progress of each class or interface in the source code.
1 @Retention (retentionpolicy.runtime) 2 @Target (elementtype.type) Span style= "color: #008080;" >3 public @interface Assignment { 4 string assignee (); 5 Effort (); 6 default 0; 7 }
@interface is used to declare an annotation, in which each method actually declares a configuration parameter. The name of the method is the name of the parameter, and the return value type is the type of the parameter. You can declare the default value of a parameter through default. A meta-annotation such as @retention and @target can be seen here to declare the behavior of the annotation itself. @Retention the retention policy used to declare annotations, there are three types of class, runtime, and source, respectively, indicating that annotations are saved in the class file, the JVM runtime, and the source code. It is only when the runtime is declared that it is possible to obtain information about annotations at run time through the reflection API. @Target is used to declare what types of elements annotations can be added to, such as types, methods, and fields.
Java @ annotations