Java @ annotation, java Annotation

Source: Internet
Author: User

Java @ annotation, java Annotation
1. annotation Introduction

When developing Java programs, especially Java EE applications, it is always inevitable to deal with various configuration files. In Java EE's typical S (pring) S (truts) H (ibuts) architecture, Spring, Struts, and Hibernate all have their own XML format configuration files. These configuration files must be synchronized with the Java source code. Otherwise, errors may occur. In addition, these errors may only be detected at runtime. It is always a bad idea to store the same piece of information in two places. The ideal situation is to maintain the information in one place. The information required for other parts is generated automatically. JDK 5 introduces the annotation (annotation) mechanism in the source code. Annotations allow Java source code to not only contain functional implementation code, but also add metadata. The annotation function is similar to the annotation in the Code. The difference is that the annotation is not a description of the Code function, but an important part of implementing the Program function. Java annotations have been widely used in many frameworks to simplify the configuration of programs.

A. Generate a document. This is the most common and the earliest annotation provided by java. Commonly used include @ see @ param @ return, etc;
B. tracking code dependencies and implementing the alternative configuration file function. It is common to use annotation-based configuration starting with spring 2.5. The function is to reduce the configuration. The current framework basically uses this configuration to reduce the number of configuration files;
C. Check the format during compilation. For example, if @ Override is placed before the method, if your method does not overwrite the superclass method, you can check it during compilation.

2. Use annotations

The annotation syntax is relatively simple. In addition to the use of the @ symbol, it is basically consistent with the Java inherent syntax. Java SE5 has three built-in standard Annotations: @ Override, which indicates that the current method definition will overwrite the methods in the superclass. @ Deprecated: the element compiler that uses annotation for it will issue a warning because annotation @ Deprecated is a code that is not in favor of use and is discarded. @ SuppressWarnings: disables improper compiler warnings. The above three annotations will be encountered during code writing. Java also provides 4 annotations, specifically responsible for creating new annotations.

@ Target

It indicates where the annotation can be used. Possible ElementType parameters include:

CONSTRUCTOR: CONSTRUCTOR Declaration

FIELD: FIELD Declaration (including enum instances)

LOCAL_VARIABLE: local variable Declaration

METHOD: METHOD declaration

PACKAGE: PACKAGE Declaration

PARAMETER: PARAMETER Declaration

TYPE: Class, interface (including annotation TYPE), or enum Declaration

@ Retention

Indicates the level at which the annotation information needs to be saved. Optional RetentionPolicy parameters include:

SOURCE: annotation will be discarded by the compiler

CLASS: Annotations are available in the class file, but are discarded by the VM.

RUNTIME: the VM retains the annotation during running, so the annotation information can be read through the reflection mechanism.

@ Document

Include annotation in Javadoc

@ Inherited

Allow subclass to inherit the annotation in the parent class

In general Java development, the two annotations @ Override and @ SupressWarnings are most often exposed. When using @ Override, you only need a simple declaration. This kind of annotation is called markerannotation. Its appearance represents some configuration semantics. Other annotations can have their own configuration parameters. Configuration parameters are displayed as name-value pairs. When using @ SupressWarnings, you need a syntax similar to @ SupressWarnings ({"uncheck", "unused. In the brackets, the annotation can be configured. Because this annotation only has one configuration parameter, the parameter name defaults to value and can be omitted. Curly brackets indicate the array type. The @ Table annotation in JPA uses a syntax similar to @ Table (name = "Customer", schema = "APP. Here we can see the usage of the name-value pair. When using annotations, the value of the configuration parameter must be a constant at the time of compilation. In a way, annotations can be viewed as an XML element, which can have different predefined attributes. The attribute value can be specified when the element is declared. Using annotations in code is equivalent to moving a part of metadata from an XML file to the Code itself and managing and maintaining it in one place.

3. Develop annotations

In general development, you only need to read the relevant API documentation to understand the meaning of configuration parameters for each annotation and use them correctly in the code. In some cases, you may need to develop your own annotations. This is common in library development. The annotation definition is somewhat similar to the interface. The following code provides a simple description of the code division. This annotation can be used to record the division of labor and progress of each class or interface in the source code.

1 @Retention(RetentionPolicy.RUNTIME)2 @Target(ElementType.TYPE)3 public @interface Assignment {4 String assignee();5 int effort();6 double finished() default 0;7 } 

@ Interface is used to declare an annotation. Each method actually declares a configuration parameter. The method name is the parameter name, and the return value type is the parameter type. You can use default to declare the default value of a parameter. Here we can see meta annotations such as @ Retention and @ Target, which are used to declare the behavior of the annotation itself. @ Retention is used to declare the annotation Retention policy. There are three types: CLASS, RUNTIME, and SOURCE, which indicate that the annotation is saved in the CLASS file, jvm runtime, and SOURCE Code respectively. Only when declared as RUNTIME can the annotation information be obtained through the reflection API at RUNTIME. @ Target is used to declare the types of elements that the annotation can be added to, such as types, methods, and fields.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.