目标:

掌握@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. C#代码规范化(代码风格化)的几个函数

    近期由于适配Oracle的缘故,将旺财C#.NET代码生成器增加了风格化的几个函数,具体实现如下功能: 1.转换为Pascal风格,如User_Name/USER_NAME/UserName自动替换下 ...

  2. CSS 笔记——文本字体

    5. 文本字体 -> 文本 (1)text-indent 基本语法 text-indent : length 语法取值 length : 百分比数字 | 由浮点数字和单位标识符组成的长度值,允许 ...

  3. [Codeforces #174] Tutorial

    Link: Codeforces #174 传送门 A: 求原根的个数,有一条性质是原根个数为$\phi(\phi(n))$,多了一个不会证的性质 如果要确定哪些是原根的话还是要枚举,不过对于每个数不 ...

  4. poj 3225 Help with Intervals(线段树,区间更新)

    Help with Intervals Time Limit: 6000MS   Memory Limit: 131072K Total Submissions: 12474   Accepted:  ...

  5. Problem F: 调用函数,判断各位数字立方和是否等于它本身

    #include<stdio.h> #include<math.h> int is(int number)//定义函数 { ; ) { s=number%; sum=sum+p ...

  6. Visio显示不完整

    下面显示不完整的话,选中对象,菜单栏设置(点击对象,右键并没有段落选项)行距为单倍:右侧显示不完整,选中后右键设置环绕方式为负于文字上方,原来是嵌入型.

  7. linux UART串口驱动开发文档

    转:http://www.360doc.com/content/10/0417/18/829197_23519037.shtml linux UART串口驱动开发文档时间:2010-01-09 14: ...

  8. sql 改动表以及表字段

    用SQL语句加入删除改动字段 1.添加字段      alter table docdsp    add dspcode char(200)      alter table tbl add meet ...

  9. iOS:极光推送控制器跳转

    在前面已经做完了极光消息的推送,那么有消息了,如何跳转到需要的控制器呢?其实,主要还是在userInfo这个消息里面做判断来处理,具体如下: 下面这两个是远程推送时接收消息的方法,这是应用程序提供的方 ...

  10. 如何理解redo和undo

    redo和undo的区别    redo--> undo-->datafileinsert一条记录时, 表跟undo的信息都会放进 redo 中, 在commit 或之前, redo 的信 ...