注解(Annotation)是java的重要组成部分,用于标记,从jdk1.5开始引入的新特性。
Jdk早起引入的注解有:@Deprecated,@Override,@SuppressWarnings
注解是一冲标记,可以标记在ElementType枚举出的类型。
1 public enum ElementType { 2 /** Class, interface (including annotation type), or enum declaration */ 3 TYPE, 4 5 /** Field declaration (includes enum constants) */ 6 FIELD, 7 8 /** Method declaration */ 9 METHOD, 10 11 /** Formal parameter declaration */ 12 PARAMETER, 13 14 /** Constructor declaration */ 15 CONSTRUCTOR, 16 17 /** Local variable declaration */ 18 LOCAL_VARIABLE, 19 20 /** Annotation type declaration */ 21 ANNOTATION_TYPE, 22 23 /** Package declaration */ 24 PACKAGE, 25 26 /** 27 * Type parameter declaration 28 * 29 * @since 1.8 30 */ 31 TYPE_PARAMETER, 32 33 /** 34 * Use of a type 35 * 36 * @since 1.8 37 */ 38 TYPE_USE 39 }ElementType
简单注解格式(以spring Autowired为例)
1 @Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE}) 2 @Retention(RetentionPolicy.RUNTIME) 3 @Documented 4 public @interface Autowired { 5 6 /** 7 * Declares whether the annotated dependency is required. 8 * <p>Defaults to {@code true}. 9 */ 10 boolean required() default true; 11 12 }Autowired