深入annotation
目标:
掌握@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的更多相关文章
- Spring Enable annotation – writing a custom Enable annotation
原文地址:https://www.javacodegeeks.com/2015/04/spring-enable-annotation-writing-a-custom-enable-annotati ...
- 【java】细说 JAVA中 标注 注解(annotation)
Java注解是附加在代码中的一些元信息,用于一些工具在编译.运行时进行解析和使用,起到说明.配置的功能.注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用 下面我们来详细说说这个注解,到底是怎么一 ...
- Java学习之注解Annotation实现原理
前言: 最近学习了EventBus.BufferKinfe.GreenDao.Retrofit 等优秀开源框架,它们新版本无一另外的都使用到了注解的方式,我们使用在使用的时候也尝到不少好处,基于这种想 ...
- Java Annotation概述
@(Java)[Annotation|Java] Java Annotation概述 用途 编译器的相关信息,如用于检测错误和一些警告 编译时和部署时的处理,如一些软件用于自动生成代码之类的 运行时处 ...
- hibernate学习三(使用Annotation,注解)
一.新建一个工程hibernate_02_HelloWorld_Annotation(复制01工程并重命名); 二.新建一个实体类teacher.java,数据库中新建teacher表; import ...
- struts2使用annotation注意事项
struts2使用annotation注意事项 1.包名只能以.action .actions .struts .struts2结尾.如:com.cnbolgs.web.actions 2.类名只 ...
- Data Validate 之 Data Annotation
什么是Data Annotation ? 如何使用 ? 自定义Validate Attribute EF Db first中使用Data Annotation asp.net MVC中使用Data ...
- SpringMvc的xml配置与annotation配置的例子的区别
1.导入jar包时,要在xml配置基础上加 spring-aop-4.2.2.RELEASE.jar (注解的时候需要) 2.编写controller的时候要annotation需要做相关配置即红色部 ...
- spring3.0使用annotation完全代替XML(续)
从回帖的反应来看,大多数人还是不赞成完全代替XML的,这点倒是在意料之中.我个人还是倾向于用代码来取代XML的Bean定义,当然这更多的是关乎个人偏好,不代表与我观点不同的人就是错的. 先来说说代码相 ...
- spring3.0使用annotation完全代替XML(三)
很久之前写过两篇博客: spring3.0使用annotation完全代替XML spring3.0使用annotation完全代替XML(续) 用java config来代替XML,当时还遗留下一些 ...
随机推荐
- Oracle REGEXP
ORACLE中的支持正则表达式的函数主要有下面四个: 1,REGEXP_LIKE :与LIKE的功能相似 2,REGEXP_INSTR :与INSTR的功能相似 3,REGEXP_SUBSTR :与S ...
- 杭电oj 1001
#include<iostream> using namespace std; int main() { , sum; while (cin>>n) { sum = ; // ...
- 又见Python<3>:Pandas之Series对象的使用
Pandas是Python下最强大的数据分析和探索库,是基于Numpy库构建的,支持类似SQL的结构化数据的增.删.查.改,具有丰富的数据处理函数.Pandas有两大数据结构:Series和DataF ...
- [BZOJ5250][九省联考2018]秘密袭击(DP)
5250: [2018多省省队联测]秘密袭击 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 3 Solved: 0[Submit][Status][D ...
- AGC 012 C - Tautonym Puzzle
题面在这里! 神仙构造啊qwqwq. 窝一开始只想到一个字符串长度是 O(log(N)^2) 的做法:可以发现一段相同的长度为n的字符串的贡献是 2^(n-1)-1 ,可以把它看成类二进制,枚举用了多 ...
- [ARC057D]全域木
题意:求有多少个边权为$1\cdots\frac{n(n-1)}2$的完全图的最小生成树的边权为$a_{1\cdots n-1}$ 啊啊啊我tm真的是什么都不会啊 考虑kruskal的过程:每次选取跨 ...
- 【优先队列】POJ3614-Sunscreen
参考:❀ #include<iostream> #include<cstdio> #include<queue> #include<algorithm> ...
- Problem E: 零起点学算法34——3n+1问题
#include<stdio.h> #include<math.h> int main() { int n; n<=pow(,); ; scanf("%d&qu ...
- uboot显示logo的方式
转:http://blog.chinaunix.net/uid-22030783-id-3257554.html 本文使用uboot2010.03版本,2010.06版本同样适用,在XC2440板上实 ...
- python 调试模式pdb(转)
标准库的pdb pdb是Python自带的一个库,为Python程序提供了一种交互式的源代码调试功能,包含了现代调试器应有的功能,包括设置断点.单步调试.查看源码.查看程序堆栈等.如果读者具有C或C+ ...