Java元注解

目录

        什么是注解?

        注解的重要性:

        @Target

        @Retention

        @Documented (很少)

        @Inherited(极少)


什么是注解?

        注解其实就是代码里的特殊标记,这些标记可以在编译,类加载,运行时被读取,并执行相应的处理。通过使用注解,程序员可以在不改变原有逻辑的情况下,在源文件中嵌入一些补充信息。代码分析工具、开发工具和部署工具可以通过这些补充信息进行验证或者进行部署。

        使用注解时要在其前面增加@符号,并把该注解当成一个修饰符使用。用于修饰它支持的程序元素。

注解的重要性:

        Annotation 可以像修饰符一样被使用,可用于修饰包,类,构造器,方法,成员变量,参数,局部变量的声明,这些信息被保存在Annotation的"name=value"对中。在JavaSE中,注解的使用目的比较简单,例如标记过时的功能,忽略警告等。在JavaEE/ArIdroid中注解占据了更重要的角色,例如用来配置应用程序的任何切面,代替JavaEE旧版中所遗留的繁冗代码和XML配置等。未来的开发模式都是基于注解的,JPA(java的持久化API)是基于注解的,Spring2.5以. E都是基于注解的,Hibernate3.x以后也是基于注解的,现在的Struts2有一部分也是基于注解的了,注解是一种趋势,一定程度上可以说 :框架=注解+反射+设计模式。

元注解是用于修饰其它注解的注解。JDK5.0提供了四种元注解:

  • @Retention
  • @Target
  • @Documented
  • @Inherited

 IDEA自定义注解:

步骤一

 步骤二

@interface 准确的说它不是一个接口,而是一个新的注释类型-注释类,它的类名就是注释名

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

官方文档注释:

@Target 官方注释

它的意思是说,用了@Target 注解的注解,可以被用在哪些作用域中,有哪些作用域需要到java.lang.annotation.ElementType 里面去找

public enum ElementType {
    /** 用在描述类、接口(包括注解类型)或枚举 */
    TYPE,

    /** 用在字段声明(包括枚举) */
    FIELD,

    /** 用于方法上 */
    METHOD,

    /** 用在参数上 */
    PARAMETER,

    /** 用在构造器(又叫构造方法)上 */
    CONSTRUCTOR,

    /** 用在局部变量上 */
    LOCAL_VARIABLE,

    /** 用在描述注释上 */
    ANNOTATION_TYPE,

    /** 用在包上 */
    PACKAGE,

    /** 从1.8开始支持,自定义类型参数上 */
    TYPE_PARAMETER,

    /** 从1.8开始支持,对类型注解 */
    TYPE_USE
}

@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}.
 *
 * <p>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.
 *
 * @author  Joshua Bloch
 * @since 1.5
 * @jls 9.6.3.2 @Retention
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
    /**
     * Returns the retention policy.
     * @return the retention policy
     */
    RetentionPolicy value();
}

@Retention 是定义该注解的生命周期有多久 ,而决定它的生命周期在一个枚举类型的RetentionPolicy里

RetentionPolicy

  • SOURCE:在源文件中有效(即源文件保留),编译器直接丢弃这种策略的注释,在.class文件中不会保留注解信息
  • CLASS:在class文件中有效(即class保留),保留在.class文件中,但是当运行Java程序时,他就不会继续加载了,不会保留在内存中,JVM不会保留注解。如果注解没有加Retention元注解,那么相当于默认的注解就是这种状态。
  • RUNTIME:在运行时有效(即运行时保留),当运行 Java程序时,JVM会保留注释,加载在内存中了,那么程序可以通过反射获取该注释。

@Documented (很少)

/**
 * Indicates that annotations with a type are to be documented by javadoc
 * and similar tools by default.  This type should be used to annotate the
 * declarations of types whose annotations affect the use of annotated
 * elements by their clients.  If a type declaration is annotated with
 * Documented, its annotations become part of the public API
 * of the annotated elements.
 *
 * @author  Joshua Bloch
 * @since 1.5
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}

官方文档注释:

@Documented 官方注释

用于指定被该元注解修饰的注解类将被javadoc工具提取成文档。默认情况下,javadoc是 不包括注解的,但是加上了这个注解生成的文档中就会带着注解了。

@Documented 注释只是用来生成文档的,不重要

@Inherited(极少)

/**
 * Indicates that an annotation type is automatically inherited.  If
 * an Inherited meta-annotation is present on an annotation type
 * declaration, and the user queries the annotation type on a class
 * declaration, and the class declaration has no annotation for this type,
 * then the class's superclass will automatically be queried for the
 * annotation type.  This process will be repeated until an annotation for this
 * type is found, or the top of the class hierarchy (Object)
 * is reached.  If no superclass has an annotation for this type, then
 * the query will indicate that the class in question has no such annotation.
 *
 * <p>Note that this meta-annotation type has no effect if the annotated
 * type is used to annotate anything other than a class.  Note also
 * that this meta-annotation only causes annotations to be inherited
 * from superclasses; annotations on implemented interfaces have no
 * effect.
 *
 * @author  Joshua Bloch
 * @since 1.5
 * @jls 9.6.3.3 @Inherited
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {
}

官方文档注释:

@Inherited 官方注释

被它修饰的Annotation将具有继承性。如果某个类使用了被@Inherited修饰的Annotation,则其子类将自动具有该注解。

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java自定义注解通常用于对注解进行注释和描述,其使用方法如下: 1. 定义一个注解,使用@Target、@Retention、@Documented三个注解注解进行描述。 2. 使用@Inherited注解,表示注解可以被继承。 3. 在需要使用注解的类、方法、属性等上面使用定义的注解,通过反射获取注解信息。 示例代码如下: @Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited public @interface CustomAnnotation { String value() default ""; } 在需要使用注解的类、方法、属性等上面使用定义的注解: @CustomAnnotation(value = "这是一个自定义注解") public class Test { @CustomAnnotation(value = "这是一个字段注解") private String name; @CustomAnnotation(value = "这是一个方法注解") public void sayHello() { System.out.println("Hello World!"); } } 通过反射获取注解信息: Class testClass = Test.class; CustomAnnotation annotation1 = (CustomAnnotation) testClass.getAnnotation(CustomAnnotation.class); System.out.println(annotation1.value()); // 输出 "这是一个自定义注解" Field nameField = testClass.getDeclaredField("name"); CustomAnnotation annotation2 = nameField.getAnnotation(CustomAnnotation.class); System.out.println(annotation2.value()); // 输出 "这是一个字段注解" Method sayHelloMethod = testClass.getDeclaredMethod("sayHello"); CustomAnnotation annotation3 = sayHelloMethod.getAnnotation(CustomAnnotation.class); System.out.println(annotation3.value()); // 输出 "这是一个方法注解"

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值