zoukankan      html  css  js  c++  java
  • java注解相关

    本文参考很多大神的文档,特别再次表示感谢分享!!

    1.何为注解?

      概念:注解(Annotation),也叫元数据。一种代码级别的说明。

    它是JDK1.5及以后版本引入的一个特性,与类、接口、枚举是在同一个层次。

    它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些元素进行注释说明.

    Annotations提供一些本来不属于程序的数据.

    2.注解的分类

    2.1 按照运行机制  (往后会讲)

    源码注解:
           注解只在源码中存在,编译成.class文件就不存在了。

    编译时注解:
           注解在源码和.class文件中都存在。(例如:JDK的三个注解)
    运行时注解:
          在运行阶段还起作用,甚至会影响运行逻辑的注解。

     

    2.2 按照来源分

    来自JDK的注:上面图片的三个@Override  @Deprecated  @SuppressWarning,

    第三方的注解:比如Spring框架、Mybatis框架的注解等等,以后再说吧

    自己定义的注解:往后会详细地讲

    3.JDK自带注解

    @Override 表示当前方法覆盖了父类的方法
    @Deprecation 表示方法已经过时,方法上有横线,使用时会有警告。
    @SuppviseWarnings 表示关闭一些警告信息(通知java编译器忽略特定的编译警告)

    1部分 Annotation架构


    先看看Annotation的架构图:

     

    从中,我们可以看出:

    (01) 1个Annotation 和 1个RetentionPolicy关联。
           可以理解为:每1个Annotation对象,都会有唯一的RetentionPolicy属性。

    (02) 1个Annotation 和 1~n个ElementType关联。
           可以理解为:对于每1个Annotation对象,可以有若干个ElementType属性。

    (03) Annotation 有许多实现类,包括:Deprecated, Documented, Inherited, Override等等。
    (04) Annotation 的每一个实现类,都“和1个RetentionPolicy关联”并且“和1~n个ElementType关联”。

    2部分 Annotation组成部分


    1 annotation组成成分

    java annotation 的组成中,有3个非常重要的主干类。它们分别是:

    (01) Annotation.java

    annotationType(); } " v:shapes="文本框_x0020_2">

    (02) ElementType.java

    (03) RetentionPolicy.java

    说明:
    (01) Annotation 就是个接口。
          “每1个Annotation” 都与 “1个RetentionPolicy”关联,并且与 “1~n个ElementType”关联。可以通俗的理解为:每1个Annotation对象,都会有唯一的RetentionPolicy属性;至于ElementType属性,则有1~n个。

    (02) ElementType 是Enum枚举类型,它用来指定Annotation的类型。
          “每1个Annotation” 都与 “1~n个ElementType”关联。当Annotation与某个ElementType关联时,就意味着:Annotation有了某种用途。
          例如,若一个Annotation对象是METHOD类型,则该Annotation只能用来修饰方法。

    (03) RetentionPolicy 是Enum枚举类型,它用来指定Annotation的策略。通俗点说,就是不同RetentionPolicy类型的Annotation的作用域不同。
          “每1个Annotation” 都与 “1个RetentionPolicy”关联。
          a) 若Annotation的类型为 SOURCE,则意味着:Annotation仅存在于编译器处理期间,编译器处理完之后,该Annotation就没用了。
              例如,“ @Override ”标志就是一个Annotation。当它修饰一个方法的时候,就意味着该方法覆盖父类的方法;并且在编译期间会进行语法检查!编译器处理完后,“@Override”就没有任何作用了。
          b) 若Annotation的类型为 CLASS,则意味着:编译器将Annotation存储于类对应的.class文件中,它是Annotation的默认行为。
          c) 若Annotation的类型为 RUNTIME,则意味着:编译器将Annotation存储于class文件中,并且可由JVM读入。

    这时,只需要记住1Annotation” 都与 “1RetentionPolicy”关联,并且与 “1nElementType”关联。学完后面的内容之后,再回头看这些内容,会更容易理解。

    3部分 java自带的Annotation


    理解了上面的3个类的作用之后,我们接下来可以讲解Annotation实现类的语法定义了。

    1 Annotation通用定义

    @Documented
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyAnnotation1 {
    }

    说明:

    上面的作用是定义一个Annotation,它的名字是MyAnnotation1。
    定义了MyAnnotation1之后,我们可以在代码中通过“@MyAnnotation1”来使用它。
    其它的,@Documented, @Target, @Retention, @interface都是来修饰MyAnnotation1的。下面分别说说它们的含义:

    (01) @interface
           使用@interface定义注解时,意味着它实现了java.lang.annotation.Annotation接口,即该注解就是一个Annotation。
           定义Annotation时,@interface是必须的。
           注意:它和我们通常的implemented实现接口的方法不同。Annotation接口的实现细节都由编译器完成。通过@interface定义注解后,该注解不能继承其他的注解或接口。

    (02) @Documented 
           类和方法的Annotation在缺省情况下是不出现在javadoc中的。如果使用@Documented修饰该Annotation,则表示它可以出现在javadoc中。
           定义Annotation时,@Documented可有可无;若没有定义,则Annotation不会出现在javadoc中。

    (03) @Target(ElementType.TYPE)
          前面我们说过,ElementType 是Annotation的类型属性。而@Target的作用,就是来指定Annotation的类型属性。
          @Target(ElementType.TYPE) 的意思就是指定该Annotation的类型是ElementType.TYPE。这就意味着,MyAnnotation1是来修饰“类、接口(包括注释类型)或枚举声明”的注解。
          定义Annotation时,@Target可有可无。若有@Target,则该Annotation只能用于它所指定的地方;若没有@Target,则该Annotation可以用于任何地方。

    (04) @Retention(RetentionPolicy.RUNTIME)
          前面我们说过,RetentionPolicy 是Annotation的策略属性,而@Retention的作用,就是指定Annotation的策略属性。
          @Retention(RetentionPolicy.RUNTIME) 的意思就是指定该Annotation的策略是RetentionPolicy.RUNTIME。这就意味着,编译器会将该Annotation信息保留在.class文件中,并且能被虚拟机读取。
          定义Annotation时,@Retention可有可无。若没有@Retention,则默认是RetentionPolicy.CLASS

    2 java自带的Annotation

    通过上面的示例,我们能理解:
    @interface用来声明Annotation,
    @Documented用来表示该Annotation是否会出现在javadoc中,
    @Target用来指定Annotation的类型,
    @Retention用来指定Annotation的策略。

    java 常用的Annotation

    @Deprecated  -- @Deprecated 所标注内容,不再被建议使用。
    @Override    -- @Override 只能标注方法,表示该方法覆盖父类中的方法。
    @Documented  -- @Documented 所标注内容,可以出现在javadoc中。
    @Inherited   -- @Inherited只能被用来标注“Annotation类型”,它所标注的Annotation具有继承性。
    @Retention   -- @Retention只能被用来标注“Annotation类型”,而且它被用来指定Annotation的RetentionPolicy属性。
    @Target      -- @Target只能被用来标注“Annotation类型”,而且它被用来指定Annotation的ElementType属性。
    @SuppressWarnings -- @SuppressWarnings 所标注内容产生的警告,编译器会对这些警告保持静默。

    4部分 Annotation 的作用


    Annotation 是一个辅助类,它在Junit、Struts、Spring等工具框架中被广泛使用。

    我们在编程中经常会使用到的Annotation作用有:

    1 编译检查

    Annotation具有“让编译器进行编译检查的作用”。

    2 在反射中使用Annotation

    在反射的Class, Method, Field等函数中,有许多于Annotation相关的接口。
    这也意味着,我们可以在反射中解析并使用Annotation。

  • 相关阅读:
    2016/11/2
    2016/11/1
    bzoj 3809: Gty的二逼妹子序列
    bzoj 1207: [HNOI2004]打鼹鼠
    bzoj 1191: [HNOI2006]超级英雄Hero
    BZOJ 1854: [Scoi2010]游戏
    BZOJ 1296: [SCOI2009]粉刷匠
    BZOJ 1787: [Ahoi2008]Meet 紧急集合
    tarjan算法
    高级的暴力(一)——分块
  • 原文地址:https://www.cnblogs.com/xiaofeifei-wang/p/8027275.html
Copyright © 2011-2022 走看看