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.