Retention注解
Retention(保留)注解说明,这种类型的注解会被保留到那个阶段. 有三个值:
1.RetentionPolicy.SOURCE —— 这种类型的Annotations只在源代码级别保留,编译时就会被忽略
2.RetentionPolicy.CLASS —— 这种类型的Annotations编译时被保留,在class文件中存在,但JVM将会忽略
3.RetentionPolicy.RUNTIME —— 这种类型的Annotations将被JVM保留,所以他们能在运行时被JVM或其他使用反射机制的代码所读取和使用.
示例演示了 RetentionPolicy.RUNTIME 的声明:
@Retention(RetentionPolicy.RUNTIME) public @interface Test_Retention { String doTestRetention(); }
在这个示例中, @Retention(RetentionPolicy.RUNTIME)注解表明 Test_Retention注解将会由虚拟机保留,以便它可以在运行时通过反射读取.
Documented 注解
Documented 注解表明这个注解应该被 javadoc工具记录. 默认情况下,javadoc是不包括注解的. 但如果声明注解时指定了 @Documented,则它会被 javadoc 之类的工具处理, 所以注解类型信息也会被包括在生成的文档中.
示例进一步演示了使用 @Documented:
@Documented public @interface Test_Documented { String doTestDocument(); }
public class TestAnnotations { public static void main(String arg[]) { new TestAnnotations().doSomeTestRetention(); new TestAnnotations().doSomeTestDocumented(); } @Test_Retention (doTestRetention="保留注解信息测试") public void doSomeTestRetention() { System.out.printf("测试注解类型 'Retention'"); } @Test_Documented(doTestDocument="Hello document") public void doSomeTestDocumented() { System.out.printf("测试注解类型 'Documented'"); } }
现在,如果你使用 javadoc命令生成 TestAnnotations.html文件,你将看到类似于图的结果.
从截图可以看到,文档中没有 doSomeTestRetention() 方法的 annotation-type信息()方法. 但是, doSomeTestDocumented() 方法的文档提供了注解的描述信息. 这是因为 @Documented标签被加到了Test_Documented注解上. 之前的注解Test_Retention并没有指定 @Documented 标记(tag).
Inherited 注解
这是一个稍微复杂的注解类型. 它指明被注解的类会自动继承. 更具体地说,如果定义注解时使用了 @Inherited 标记,然后用定义的注解来标注另一个父类, 父类又有一个子类(subclass),则父类的所有属性将被继承到它的子类中. 在示例7中,你会看到使用 @Inherited 标签的好处.
@Inherited public @interface MyParentObject { boolean isInherited() default true; String doSomething() default "Do what?"; }
@MyParentObject public Class MyChildObject { }
正如你看到的,你不需要在实现类中定义接口方法. 因为使用 @Inherited标记,这些都自动继承了. 如果你使用一种古老的方式定义实现类,会是什么样子呢? 看看下面这张 古老的实现方式吧:
public class MyChildObject implements MyParentObject { public boolean isInherited() { return false; } public String doSomething() { return ""; } public boolean equals(Object obj) { return false; } public int hashCode() { return 0; } public String toString() { return ""; } public Class annotationType() { return null; } }
看到的区别吗? 可以看到,你必须实现父接口的所有方法. 除了isInherited()和从myParentObject doSomething()方法外,你还需要实现 java.lang.Object的 equals(),toString()和hasCode()方法. 还有 java.lang.annotation.Annotation 类的 annotationType()方法. 不管你是不是想要实现这些方法,你必须在继承的对象中包含这些.
from:http://www.jb51.net/article/55371.htm
我们自定义注解(Annotation)时,把自定义的注解标注在父类上不会被子类所继承,但是我们可以在定义注解时给我们自定义的注解标注一个@Inherited注解来实现注解继承。
自定义的注解代码如下:- package com.xdf.annotation;
- import java.lang.annotation.Inherited;
- import java.lang.annotation.Retention;
- @Inherited
- @Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
- public @interface InheritedAnnotation {
- String value();
- }
接着定义一个抽象父类
代码如下:
- package com.xdf.annotation;
- public abstract class AbstractParent {
- @InheritedAnnotation(value = "parent abstractMethod ")
- public abstract void abstractMethod();
- @InheritedAnnotation(value = "Parent's doExtends")
- public void doExtends() {
- System.out.println(" AbstractParent doExtends ...");
- }
- }
我们把自定义的注解父类里的方法上。
接着定义一个继承抽象父类(AbstractParent)的子类
代码如下:
- package com.xdf.annotation;
- public class SubClassImpl extends AbstractParent{
- @Override
- public void abstractMethod() {
- System.out.println("子类实现抽象父类的抽象方法");
- }
- }
在子类中实现了abstractMethod抽象方法,没有重写doExtends方法。
测试类代码如下:
- package com.xdf.annotation;
- import java.lang.reflect.Method;
- public class InheritedAnnotationTest {
- public static void main(String[] args) throws SecurityException, NoSuchMethodException {
- Class<SubClassImpl> clazz=SubClassImpl.class;
- //abstractMethod
- Method method = clazz.getMethod("abstractMethod", new Class[]{});
- if(method.isAnnotationPresent(InheritedAnnotation.class)){
- InheritedAnnotation ma = method.getAnnotation(InheritedAnnotation.class);
- System.out.println("子类实现的抽象方法继承到父类抽象方法中的Annotation,其信息如下:");
- System.out.println(ma.value());
- }else{
- System.out.println("子类实现的抽象方法没有继承到父类抽象方法中的Annotation");
- }
- Method methodOverride = clazz.getMethod("doExtends", new Class[]{});
- if(methodOverride.isAnnotationPresent(InheritedAnnotation.class)){
- InheritedAnnotation ma = methodOverride.getAnnotation(InheritedAnnotation.class);
- System.out.println("子类doExtends方法继承到父类doExtends方法中的Annotation,其信息如下:");
- System.out.println(ma.value());
- }else{
- System.out.println("子类doExtends方法没有继承到父类doExtends方法中的Annotation");
- }
- }
- }
运行结果如下:
- 子类实现的抽象方法没有继承到父类抽象方法中的Annotation
- 子类doExtends方法继承到父类doExtends方法中的Annotation,其信息如下:
- Parent's doExtends
从以上代码运行的结果可以得到如下结论:
1.如果子类继承父类,并且重写了父类中的带有注解的方法,那么父类方法上的注解是不会被子类继承的。
2.如果子类继承父类,但是没有重写父类中带有注解的方法,那么父类方法上的注解会被子类继承,就是说在子类中可以得到父类方法上的注解。
但是.....但是....当我把自定义的注解上的@Inherited注解去掉运行,结果还是一样,也就是说这个@Inherited注解根本没有起作用。这是什么神马情况呢?
接着我把没有标注@Inherited注解的自定义的注解标注在类级别上(不是方法级别上),把抽象父类改成下面这样:
- package com.xdf.annotation;
- @InheritedAnnotation(value="parent") //把自定义注解标注在父类上
- public abstract class AbstractParent {
- @InheritedAnnotation(value = "parent abstractMethod ")
- public abstract void abstractMethod();
- @InheritedAnnotation(value = "Parent's doExtends")
- public void doExtends() {
- System.out.println(" AbstractParent doExtends ...");
- }
- }
然后在测试类的main方法里加了如下测试代码:
- if(clazz.isAnnotationPresent(InheritedAnnotation.class)){
- InheritedAnnotation cla = clazz.getAnnotation(InheritedAnnotation.class);
- System.out.println("子类继承到父类类上Annotation,其信息如下:");
- System.out.println(cla.value());
- }else{
- System.out.println("子类没有继承到父类类上Annotation");
- }
这是出现情况了,运行main方法得到结果:
- 子类实现的抽象方法没有继承到父类抽象方法中的Annotation
- 子类doExtends方法继承到父类doExtends方法中的Annotation,其信息如下:
- Parent's doExtends
- 子类没有继承到父类类上Annotation
从运行结果中可以发现子类并没有继承父类类级别的注解,于是我又把@Inherited注解标注在自定义注解上,然后运行一下,得到如下结果:
- 子类实现的抽象方法没有继承到父类抽象方法中的Annotation
- 子类doExtends方法继承到父类doExtends方法中的Annotation,其信息如下:
- Parent's doExtends
- 子类继承到父类类上Annotation,其信息如下:
- parent
注意运行结果,子类继承了父类类级别的注解了。
这说明什么呢?
说明这种标有@Inherited注解的自定义的注解运用到类级别上和方法级别上是不一样的,如果把标有@Inherited注解的自宝义的注解标注在类级别上,子类则可以继承父类类级别的注解,反之,则不行。
from:http://xiangdefei.iteye.com/blog/1044199
相关推荐
帮助大家复习java基础知识其中有 hashCode 2 toString 2 finalize 2 用已学知识做出简单的...@Retention 注解 19 @Target 注解 19 @Documented注解 20 @Inherited 注解 20 异常-Exception 21 编译异常 22 异常处理 2
Java5.0定义的元注解: 1.@Target, 2.@Retention, 3.@Documented, 4.@Inherited 这些类型和它们所⽀持的类在java.lang.annotation包中可以找到。下⾯我们看⼀下每个元注解的作⽤和相应分参数的使⽤说明。 ...
近期接触了路由和模块间通信的内容,发现Java注解非常常用。 避免后面看各源码被其阻塞,大致了解了下,作此文记录之。 注解类型 @Target @Retention @Documented @Inherited @Target 用于描述注解的使用范围,可能...
由于无论在Java后台或者Android开发中我们经常遇到注解这个功能机制,例如常用的框架Java后台开发中,Spring、MyBatis等,Android的Dagger2,butterknife等,都是注解框架。今天我们就了解java是如何进行设置注解的...
元注解(JDK的元Annotation)4.1.@Target4.2.@Retention4.3.@Documented4.4.@Inherited4.5.@Result四、在程序中使用(解析)注解五、案例——简单的测试框架 一、注解是什么? 从JDK5开始,Java增加对元数据的支持...
目录 一 注解的定义 二 如何自定义注解 三 元注解 3.1 @Retention 3.2 @Target 3.3 @Documented ...七 注解实战之简易测试框架 补充1 :元数据 参考文章: 一 注解的定义 Java 注解是从 Java5 开
Java版水果管理系统源码 huihe_2020summer 2020假期spring学习 1.注解和反射 1.注解 Java 注解(Annotation)又称 Java 标注,是 JDK5.0 引入的一种注释机制。 Java 语言中的类、方法、变量、参数和包等都可以被标注...
@Retention:描述注解的生命周期,传入value参数指定 (runtime>class>sources) @Documented:是否生成注解在Javadoc种 @Inherited:子类可以继承父类的注解 自定义注解 @interface 注解名{} 属性为注解的参数:...
1.1 Java语言发展简史2 1.2 认识Java语言3 1.2.1 Java语言特性3 1.2.2 JavaApplet4 1.2.3 丰富的类库4 1.2.4 Java的竞争对手5 1.2.5 Java在应用领域的优势7 1.3 Java平台的体系结构7 1.3.1 JavaSE标准版8 1.3.2 ...
注解的目标,使用ElementType枚举,目标可以有多个,用{}表示@Retention :注解保留到什么时候,使用RetentionPolicy ,更改RetentionPolicy.CLASS @Documented :表示注解信息包含到生成的文档中@Inherited :...
@Inherited @Documented @Target({ElementType.FIELD, ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface AccessLimit { //限流唯一标识 String key() default ""; ...
@ComponentScan @Configuration @Conditional @Retention @Documented @Target @Inherited @Bean @Import等注解含义的总结和个人理解: 包扫描@ComponentScan+组件标注注解(@Controller/@Service/@Repository/@...
@Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = ...
25 JAVA8 与元数据.................................................................................................................................25 2.4. 垃圾回收与算法 .................................