Java Foundation "11" @ Annotations

Source: Internet
Author: User

Read Catalogue

    • 1. Introduction to Annotations
    • 2. Using annotations
    • 3. Development Notes
Back to top 1. Introduction of annotations

The Java.lang.annotation package introduced in JDK 1.5 provides programmatic support for annotations, allowing classes to be read at compile time, class load, runtime, and handled accordingly.

In Java EE applications, it is always unavoidable to deal with various 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.

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 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.

    • 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;
    • 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;
    • Compile-time for formatting checks. such as @override before the method, if you do not override the method of the superclass method, compile time can be checked out.
Back to top 2. Using annotations

The syntax of annotations is simple, except for the use of the @ symbol, which is basically consistent with the Java intrinsic syntax.

The Java SE5 contains three standard annotations: @Override, which means that the current method definition overrides methods in the superclass.

@Deprecated, the element compiler that uses annotations for it will issue a warning because the annotation @deprecated is deprecated code, which is not approved for use.

@SuppressWarnings, turn off improper compiler warning messages. How much of the above three annotations we are going to have 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, and the possible ElementType parameters are:

CONSTRUCTOR: Declaration of the constructor

Field: Domain declarations (including enum instances)

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 the VM

RUNTIME:VM will retain annotations during the run, so the information of the annotations can be read through the reflection mechanism.

@Document

To include annotations in the Javadoc

@Inherited

Allow subclasses to inherit annotations from parent class

In the general Java development, the most frequently contacted 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.

Back to top 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.

@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 Foundation "11" @ Annotations

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.