您好,欢迎来到华佗小知识。
搜索
您的当前位置:首页spring的元注解

spring的元注解

来源:华佗小知识
spring的元注解

Spring的元注解:注解上的注解。

1.@Target(ElementType.TYPE)

使⽤java.lang.annotation.Target可以定义其使⽤时机,在定义时要时要指定java.lang.annotaton.ElementType的枚举值之⼀。

@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();}

下⾯看⼀下ElementType:

public enum ElementType {

/** Class, interface (including annotation type), or enum declaration */ TYPE,

/** Field declaration (includes enum constants) */ FIELD,

/** Method declaration */ METHOD,

/** Formal parameter declaration */ PARAMETER,

/** Constructor declaration */ CONSTRUCTOR,

/** Local variable declaration */ LOCAL_VARIABLE,

/** Annotation type declaration */ ANNOTATION_TYPE, /** Package declaration */ PACKAGE,

/**

* Type parameter declaration *

* @since 1.8 */

TYPE_PARAMETER, /**

* Use of a type *

* @since 1.8 */

TYPE_USE}

ANNOTATION_TYPE: 注解只能修饰注解,不能修饰其他的东西CONSTRUCTOR: 注解只能修饰构造⽅法FIELD: 注解只能修饰属性(成员变量)LOCAL_VARIABLE: 注解只能修饰局部变量METHOD: 注解只能修饰⽅法PACKAGE: 注解只能修饰包

PARAMETER: 注解只能修饰⽅法的参数TYPE: 注解只能修饰类、接⼝、枚举

2.@Retention(RetentionPolicy.RUNTIME)

@Documented

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.ANNOTATION_TYPE)public @interface Retention { /**

* Returns the retention policy. * @return the retention policy */

RetentionPolicy value();}

下⾯看⼀下RetentionPolicy:

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}

SOURCE:编译程序处理完Annotation信息后就完成任务,编译时忽略CLASS:编译程序将Annotation存储于class档中,JVM忽略

RUNTIME:编译程序将Annotation储存于class档中,可由VM使⽤反射机制的代码所读取和使⽤。

3.Documented

Documented 注解表明这个注解应该被 javadoc⼯具记录. 默认情况下,javadoc是不包括注解的. 但如果声明注解时指定了 @Documented,则它会被 javadoc 之类的⼯具处理, 所以注解类型信息也会被包括在

@Documented

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.ANNOTATION_TYPE)public @interface Documented {}

4.@Inherited

这是⼀个稍微复杂的注解类型. 它指明被注解的类会⾃动继承. 更具体地说,如果定义注解时使⽤了 @Inherited 标记,然后⽤定义的注解来标注另⼀个⽗类, ⽗类⼜有⼀个⼦类(subclass),则⽗类的所有属性将被

@Documented

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.ANNOTATION_TYPE)public @interface Inherited {}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo0.cn 版权所有 湘ICP备2023017654号-2

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务