Note Annotation
Note-based development, which makes the code concise, readable, simplified configuration, but also improve the efficiency of development, especially the rise of springboot, with the start-up and automatic configuration of the perfect, it is based on the development of annotations to new heights.
Meta Annotations Meta-annotation
Java 5 defines four standard meta-annotation types to provide functional descriptions of other annotations.
Located under the Java.lang.annotation package, respectively:
1. @Target
2. @Retention
3. @Documented
4. @Inherited
Take @profile annotations as an example:
@Target ({elementtype.type, elementtype.method}) @Retention (retentionpolicy.runtime) @Documented @conditional ( Profilecondition. class ) public @Interface Profiles { /** * The set of profiles for which the annotated component should be registered. */ string[] Value ();}
@Target
Describes the range of objects that the annotation modifies.
Annotations can be used for: package, type (class, interface, enumeration, annotation), type members (methods, construction methods, member variables, enumeration values), method parameters, and local variables (loop variables, catch parameters).
According to the scope of action, the common annotations given by the individual, according to the scope of action, various types of temporary lift one:
@Configuration, @MapperScan, @RestController, @RequestMapping, @ResponseBody, @Autowired, @Resource, @Value, @ Pathvariable ...
The specific value range is ElementType (enum) type of array, see source code:
@Documented @retention (retentionpolicy.runtime) @Target (elementtype.annotation_type) public @Interface Target { /** * Returns An array of the kinds of element s an annotation type * can is applied to. @return An array of the kinds of elements a annotation type * can be applied to*/ Ele Menttype[] Value ();}
- Elementtype.constructor: Description Constructor
- Elementtype.field: Description Domain
- Elementtype.localvariable: Describing local variables
- Elementtype.method: Description method
- Elementtype.package: Description Package
- Elementtype.parameter: Description parameter
- Elementtype.type: Description class, interface, enum, annotation
@Retention
Defines the duration of the annotation (source file, bytecode, run time).
To illustrate the extent to which the annotations described are in effect.
The value is unique, that is, one of the specific save policies, Retentionpolicy (enum).
@Documented @retention (retentionpolicy.runtime) @Target (elementtype.annotation_type) public @Interface Retention { /** * Returns the Retention policy. @return the retention policy * /Retentionpolicy value ();}
Retentionpolicy includes:
- Source: Original File valid
- CLASS: Byte-code file is valid
- Runtime: Valid at run time
@Documented
markup annotations, which can be documented by tools such as Javadoc, which describe annotations of that type, should be used as public APIs for annotated program members, and no members
@Documented @retention (retentionpolicy.runtime) @Target (elementtype.annotation_type) public @Interface documented {}
@Inherited
Tag annotations, which describe annotations that can be inherited. That is, if a class is decorated with annotations of the @inherited tag, the annotation also works on subclasses of the class.
@Documented @retention (retentionpolicy.runtime) @Target (elementtype.annotation_type) public @Interface inherited {}
Summary of spring annotations and Java meta annotations