深入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,当时还遗留下一些 ...
随机推荐
- ProxyStrike运行bug解决办法
ProxyStrike运行bug解决办法 由于curl中参数CURLOPT_SSL_VERIFYHOST的值取消原有的值1,导致ProxyStrike无法正常运行.所以,要运行该工具,需要手动修改/ ...
- Xamarin 中Visual Studio创建项目提示错误
Xamarin 中Visual Studio创建项目提示错误 错误信息:Object reference not set to an instance of an object 出现这种情况,是由于没 ...
- 【UOJ #79】一般图最大匹配 带花树模板
http://uoj.ac/problem/79 带花树模板,做法详见cyb的论文或fhq的博客. 带花树每次对一个未盖点bfs增广,遇到奇环就用并查集缩环变成花(一个点),同时记录每个点的Next( ...
- BZOJ 4403 序列统计(Lucas)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4403 [题目大意] 给定三个正整数N.L和R,统计长度在1到N之间, 元素大小都在L到 ...
- 【置换群】【枚举约数】hdu6038 Function
把b数组的所有置换群求出来,用数组记录一下每个大小所出现的次数. 然后求a的置换群,对每个置换群求能被其整除的b的置换群的大小总和(只有这些才能满足构造出一个f,且不自相矛盾),然后把它们全都乘起来就 ...
- 【插头dp】CDOJ1690 这是一道比CCCC简单题难的简单题
最裸的插头dp,可参见大白书. #include<cstdio> #include<cstring> using namespace std; #define MOD 1000 ...
- Java学习笔记(7)
构造函数: 构造函数的作用:给对应的对象进行初始化. 构造函数定义的格式: 修饰符 函数名(形式参数){ 函数体... } 构造函数要注意的细节: 构造函数是没有返回值类型的 构造函数的函数名必须 ...
- ncnn阅读 - CMakeLists.txt
CMAKE_TOOLCHAIN_FILE This variable is specified on the command line when cross-compiling with CMake. ...
- hibernate使用原生SQL查询
以下是Demo测试Hibernate 原生SQL查询: import java.util.Iterator; import java.util.List; import java.util.Map; ...
- [移动应用安全]移动应用安全培训PPT
这是年初给公司做的移动应用安全培训,主要是针对开发的兄弟们,让大家对目前的移动领域的安全形势有个了解,以及不正确的开发所带来的危害,另外还配了个详细的word版的移动应用开发指南,后面一起附上:).