What is annotation, how to use annotation, why is annotation used, and why is annotation used

Source: Internet
Author: User

What is annotation, how to use annotation, why is annotation used, and why is annotation used

Annotation

Annotation can be seen as an extension template for a class/method. Each class/method is annotated with different parameters for the class/method according to the rules in the annotation class, you can obtain the parameters and values annotated in different classes/methods.

How to Use annotations

1. Custom annotation class

See the link for the meanings of the annotations above the annotation class.Http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html

2. Enumeration class

3. annotation usage

Get the specific configuration information through the annotation and print it. The annotation function is complete.

The above demonstrates the type annotation, and the method annotation modifies the annotation class.

@ Target (ElementType. TYPE) ==>@ Target (ElementType. METHOD) can be called as follows

Why annotation?

Advantages

Configuration File

1. Follow the OCP development principles to modify the configuration file for Function Extension (OCP Open and Close Principle Open Closed Principle)

2. centrally manage the composite relationship between objects and make it easy to read

Annotation

1. Fast Development

2. It is easy to find the source of the error during compilation.

Disadvantages

Configuration File

1. the development speed is relatively slow;

2. It is difficult to check for errors during compilation, but it is difficult to locate errors during running and make debugging difficult.

Annotation

1. Scattered management, basically in each class;

2. The OCP development principle is not followed when the function is extended.

A small feature

Annotations have A mechanism similar to inheritance.. java implements spring @ Component and can be injected into spring containers. However, if the custom annotation has the @ Component annotation of spring, the original spring annotation is not required when you use this custom annotation. The example is as follows:

This is the original annotation form, a custom annotation and a spring annotation.

If the custom annotation has the @ component annotation of spring

In actual use, you can ignore the previous annotations.

 

Related Article

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.