之 Java元注解释义
Question
- 注解在Java中如何起作用?
- Spring是如何识别注解?
- 如何自定义注解为我所用?
- Spring注解:
@Aotuwired @Required @Qualifier @Provider @Scope ...- Spring MVC 注解:
@Controller @Service @Repository @Component @RequestMapping @RequetBody @ResponseBody ...
Extension
Solution
Java注解起源:JDK1.5
常见Java注解 :
- @Override
- @Deprecated
- @SupressWarnings
1.从@Override说起,引出Java注解和元注解。
@Override 源码如下:
/*If a method is annotated with this annotation type
* compilers are required to generate an error message
* unless at least one of the following conditions hold:
*
* The method does override or implement a method declared in a
* supertype.
* The method has a signature that is override-equivalent to that of
* any public method declared in {@linkplain Object}.
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override { // @interface 修饰注解类
}
注意@Override 源码中有3个要点:
- @interface:修饰注解类使用@interface,而不是interface。这就定义了一个注解 @Override。
- Java元注解:@Target, @Retention。Java元注解即:定义注解的注解(To annotate the annotation)。
- 元注解参数:ElementType.METHOD, RetentionPolicy.SOURCE
2.再看@Retention源码(Retention: 保留,滞留之意。)
/* Indicates how long annotations with the annotated type are to
* be retained. If no Retention annotation is present on
* an annotation type declaration, the retention policy defaults to
* {@code RetentionPolicy.CLASS}.
*
* A Retention meta-annotation has effect only if the
* meta-annotated type is used directly for annotation. It has no
* effect if the meta-annotated type is used as a member type in
* another annotation type.
*/
@Documented // 表明 注解会被包含在Java API文档中。
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
/**
* Returns the retention policy.
* @return the retention policy
*/
RetentionPolicy value();
}
public enum RetentionPolicy {
/**
* Annotations are to be discarded by the compiler.
*/
SOURCE,
/**
* Annotations are to be recorded in the class file by the compiler
* but need not be retained by the VM at run time. This is the default
* behavior.
*/
CLASS,
/**
* Annotations are to be recorded in the class file by the compiler and
* retained by the VM at run time, so they may be read reflectively.
*
* @see java.lang.reflect.AnnotatedElement
*/
RUNTIME
}
RetentionPolicy.SOURCE 保留在源码级别,被编译器抛弃,如@Override(如上@Override源码);
RetentionPolicy.CLASS 被编译器保留在编译后的class文件,但是被VM抛弃;
RetentionPolicy.RUNTIME 保留至运行时,可以被反射读取。如 @Retention 元注解本身。
** 引申1:如果定义一个注解需要被反射读取,则在定义这个注解的时候将添加@Retention(RetentionPolicy.RUNTIME) 元注解。**
3.再看@Target 元注解,定义了注解应该起作用的地方。
@Documented
@Retention(RetentionPolicy.RUNTIME) // 保留到运行时
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
/**
* Returns an array of the kinds of elements an annotation type
* can be applied to.
* @return an array of the kinds of elements an annotation type
* can be applied to.
*/
ElementType[] value();
}
注解作用位置:
public enum ElementType {
TYPE, // 类,接口(包括注解),enum;
FIELD, // 属性域
METHOD, // 方法
PARAMETER, // 参数
CONSTRUCTOR, // 构造函数
LOCAL_VARIABLE, // 局部变量
ANNOTATION_TYPE, // 注解类型
PACKAGE, // 包
/**
* Type parameter declaration
* @since 1.8
*/
TYPE_PARAMETER, // 表明可以标注 类型参数
/**
* Use of a type
* @since 1.8
*/
TYPE_USE // 可以注解 任何类型名称
}
** 引申2:如果想要自定义一个注解,就必须指定注解作用的位置。作用在 类,方法,属性域,构造函数等。 **
举例 SpringMVC 中的 @RequestMapping。
其源码定义如下:
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
// skip its class code.
}
@RequestMapping 的@Target元注解 表明它可以被使用在方法和类(或接口,注解,enum)上。
@RequestMapping 的@Retention元注解表明它可以保留到运行时(RUNTIME),被反射读取。
** 引申3:如果想要自定义注解,除了添加@interface 修饰类名,必须满足上述引申1和引申2。 **
那么如何自定义注解?
请参考:Spring注解原理探索(二)之 Java中如何自定义注解
网友评论