目标:

掌握@Target注释

掌握@Document注释

掌握@inherited注释

之前定义的annotation,如果没有明确声明,可以在任何地方使用:

package 类集;

@MyDefaultAnnotationReflect(key="MLDN",value="www.mldnjava.cn")  --此注释是上一节中定义的
public class SimpleBeanOne{
@MyDefaultAnnotationReflect(key="MLDN",value="www.mldnjava.cn")
public String toString(){
return "Hello LiXingHua!!!" ;
}
};

如果需要指定其使用范围,必须要使用@Target注释:

@Target

Target注释类型的必须元素如下:

ElementType枚举常量:

现在定义一个annotation,只能在类上使用,类型是Type

import java.lang.annotation.Target ;
import java.lang.annotation.ElementType ;
import java.lang.annotation.Retention ;
import java.lang.annotation.RetentionPolicy ;
@Target({ElementType.TYPE}) // 此注释只能用在类上
@Retention(value=RetentionPolicy.RUNTIME)
public @interface MyTargetAnntation{
public String key() default "LXH" ;
public String value() default "李兴华" ;
}

此时在simpleBean的类及方法的声明上使用此annotation。

@MyTargetAnntation(key="MLDN",value="www.mldnjava.cn")
public class SimpleBean{
@MyTargetAnntation(key="MLDN",value="www.mldnjava.cn")  --这里会报错,因为上面声明了只能在类中使用。
public String toString(){
return "Hello LiXingHua!!!" ;
}
};

现在如果想在类和方法都能使用,则必须设置多个范围。

import java.lang.annotation.Target ;
import java.lang.annotation.ElementType ;
import java.lang.annotation.Retention ;
import java.lang.annotation.RetentionPolicy ;
@Target({ElementType.TYPE,ElementType.METHOD}) // 此注释只能用在类和方法上
@Retention(value=RetentionPolicy.RUNTIME)
public @interface MyTargetAnntation{
public String key() default "LXH" ;
public String value() default "李兴华" ;
}

@Document

@Document可以在任何的annotation上使用,所有annotation默认都是使用@Document进行注释的。

而且在生成javadoc的使用可以通过@Document设置一些说明信息。

定义一个@Document的注释:

import java.lang.annotation.Documented ;
@Documented
public @interface MyDocumentedAnntation{
public String key() default "LXH" ;
public String value() default "李兴华" ;
}

成功之后,在使用此annotation的时候就可以增加一些信息上去了。

@MyDocumentedAnntation(key="MLDN",value="www.mldnjava.cn")
public class SimpleBeanDocumented{
/**
* 此方法在对象输出时调用,返回对象信息
*/
@MyDocumentedAnntation
(key="MLDN",value="www.mldnjava.cn")
public String toString(){
return "Hello LiXingHua!!!" ;
}
};

之后通过javadoc命令,生成java.doc文档。

打开文档,发现文档的内容把刚定义的类的信息加上去了。

2,

@Inhenited注释

此注释表示一个annotation是否可以被继承下来。

package org.lxh.demo16.inheriteddemo ;
import java.lang.annotation.Retention ;
import java.lang.annotation.RetentionPolicy ;
import java.lang.annotation.Documented ;
import java.lang.annotation.Inherited ;
@Documented
@Inherited
@Retention(value=RetentionPolicy.RUNTIME)
public @interface MyInheritedAnnotation{
public String name() ; }

定义一个父类,在父类使用此annotation。

package org.lxh.demo16.inheriteddemo ;
@MyInheritedAnnotation(name="李兴华")
public class Person{
};

定义子类

package org.lxh.demo16.inheriteddemo ;
public class Student extends Person{
};

按照所解释:使用的inherited声明的annotation是可以被子类继承下来的。用反射来检验一下:

import java.lang.annotation.Annotation ;
import org.lxh.demo16.inheriteddemo.MyInheritedAnnotation ;//注意,要把这个annotation包也导入。
public class ReflectInheritedDemo{
public static void main(String args[]) throws Exception{
Class<?> c = null ;
c = Class.forName("org.lxh.demo16.inheriteddemo.Student") ;
Annotation ann[] = c.getAnnotations() ; // 取得全部的Annotation
for(Annotation a:ann){ // 输出
System.out.println(a) ;
}
// 继续取得此Annotation设置的内容,这里前面讲过
if(c.isAnnotationPresent(MyInheritedAnnotation.class)){  //是否包含这种annotation。
MyInheritedAnnotation mda = null ;
mda = c.getAnnotation(MyInheritedAnnotation.class) ;//获取C这个class对象里面的这个annotation对象,
String name = mda.name() ; // 取出name的内容
System.out.println("name = " + name) ;
}
}
}

输出结果:

总结:

1,熟悉Document注释的作用。加入说明信息。

2,属性Target作用,并使用Target注释指定注释的使用位置。

3,如果一个annotation要想被子类继承下来,则使用inherited注释说明。

4,反射机制对于操作annotation是最重要的。

深入annotation的更多相关文章

  1. Spring Enable annotation – writing a custom Enable annotation

    原文地址:https://www.javacodegeeks.com/2015/04/spring-enable-annotation-writing-a-custom-enable-annotati ...

  2. 【java】细说 JAVA中 标注 注解(annotation)

    Java注解是附加在代码中的一些元信息,用于一些工具在编译.运行时进行解析和使用,起到说明.配置的功能.注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用 下面我们来详细说说这个注解,到底是怎么一 ...

  3. Java学习之注解Annotation实现原理

    前言: 最近学习了EventBus.BufferKinfe.GreenDao.Retrofit 等优秀开源框架,它们新版本无一另外的都使用到了注解的方式,我们使用在使用的时候也尝到不少好处,基于这种想 ...

  4. Java Annotation概述

    @(Java)[Annotation|Java] Java Annotation概述 用途 编译器的相关信息,如用于检测错误和一些警告 编译时和部署时的处理,如一些软件用于自动生成代码之类的 运行时处 ...

  5. hibernate学习三(使用Annotation,注解)

    一.新建一个工程hibernate_02_HelloWorld_Annotation(复制01工程并重命名); 二.新建一个实体类teacher.java,数据库中新建teacher表; import ...

  6. struts2使用annotation注意事项

    struts2使用annotation注意事项 1.包名只能以.action .actions  .struts  .struts2结尾.如:com.cnbolgs.web.actions 2.类名只 ...

  7. Data Validate 之 Data Annotation

    什么是Data Annotation ? 如何使用 ? 自定义Validate Attribute EF  Db first中使用Data Annotation asp.net MVC中使用Data ...

  8. SpringMvc的xml配置与annotation配置的例子的区别

    1.导入jar包时,要在xml配置基础上加 spring-aop-4.2.2.RELEASE.jar (注解的时候需要) 2.编写controller的时候要annotation需要做相关配置即红色部 ...

  9. spring3.0使用annotation完全代替XML(续)

    从回帖的反应来看,大多数人还是不赞成完全代替XML的,这点倒是在意料之中.我个人还是倾向于用代码来取代XML的Bean定义,当然这更多的是关乎个人偏好,不代表与我观点不同的人就是错的. 先来说说代码相 ...

  10. spring3.0使用annotation完全代替XML(三)

    很久之前写过两篇博客: spring3.0使用annotation完全代替XML spring3.0使用annotation完全代替XML(续) 用java config来代替XML,当时还遗留下一些 ...

随机推荐

  1. hdu 1529 Cashier Employment(差分约束)

    Cashier Employment Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  2. [转]spring4.x注解概述

    1. 背景 注解可以减少代码的开发量,spring提供了丰富的注解功能,因项目中用到不少注解,因此下定决心,经spring4.x中涉及到的注解罗列出来,供查询使用. 2. spring注解图     ...

  3. BZOJ 4032: [HEOI2015]最短不公共子串 后缀自动机 暴力

    4032: [HEOI2015]最短不公共子串 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4032 Description 在虐各种最 ...

  4. Codeforces Gym 100269A Arrangement of Contest 水题

    Problem A. Arrangement of Contest 题目连接: http://codeforces.com/gym/100269/attachments Description Lit ...

  5. Inno Setup入门(十七)——Inno Setup类参考(3)

        标签 标签(Label)是用来显示文本的主要组件之一,也是窗口应用程序中最常用的组件之一,通过对标签的使用,将能够给用户提供更加详细的信息. Pascal脚本中的标签由类TlLabel实现,该 ...

  6. (转)DLL中导出函数的两种方式(dllexport与.def文件)

    DLL中导出函数的两种方式(dllexport与.def文件)http://www.cnblogs.com/enterBeijingThreetimes/archive/2010/08/04/1792 ...

  7. (转)C++ STL中的vector的内存分配与释放

    C++ STL中的vector的内存分配与释放http://www.cnblogs.com/biyeymyhjob/archive/2012/09/12/2674004.html 1.vector的内 ...

  8. GCC 内联汇编(GCC内嵌ARM汇编规则)

    转:http://smileleeboo.howbbs.com/posts/list/3127/81062.html 更多文档参见:http://pan.baidu.com/s/1eQ7nd8Q 有时 ...

  9. 判断UISrollview的滑动方向

    很常用的一个功能,就记录下来了. -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { historyY = scrollVi ...

  10. 你可能从未听过的 Linux 发行版

    Hanthana Linux 官方主页:http://www.hanthana.org Hanthana Linux 基于 Fedora,主要面向 IT 教育,默认包含额外的编/解码器及多媒体播放器. ...