深入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,当时还遗留下一些 ...
随机推荐
- Spring的事务传播性
事务是逻辑处理原子性的保证手段,通过使用事务控制,可以极大的避免出现逻辑处理失败导致的脏数据等问题.事务最重要的两个特性,是事务的传播级别和数据隔离级别.传播级别定义的是事务的控制范围,事务隔离级别定 ...
- spring面试题 对DI , AOP概念的理解
1.降低了组件之间的耦合性 ,实现了软件各层之间的解耦 2.可以使用容易提供的众多服务,如事务管理,消息服务等 3.容器提供单例模式支持 4.容器提供了AOP技术,利用它很容易实现如权限拦截,运行期监 ...
- js流程控制与函数
流程控制 1.条件语句 分支结构 单向分支 if (条件表达式){ code... } 双向分支 if (条件表达式){ code... }else{ code... } 多向分支 if (条件表达式 ...
- Codeforces Round #339 (Div. 1) C. Necklace 构造题
C. Necklace 题目连接: http://www.codeforces.com/contest/613/problem/C Description Ivan wants to make a n ...
- jQuery Ajax 参数解析
简单的例子: $.ajax({ type:"post", data:{a:acon,b:bcon} , url:"ajax.php", async:false ...
- 《Windows驱动开发技术详解》之StartIO例程
内容中包含 base64string 图片造成字符过多,拒绝显示
- [转载] Spring3.1 Cache注解
需要感慨一下,spring3.0时丢弃了2.5时的spring-modules-cache.jar,致使无法使用spring来方便的管理cache注解,好在3.1.M1中增加了对cache注解的支持, ...
- python logging模块用法
http://blog.csdn.net/zyz511919766/article/details/25136485/ import logging logging.debug('debug mess ...
- AQTime教程
1 简介 AQTime和MemProof都是AutomatedQA旗下的产品,AQTime比MemProof提供了更丰富强大的功能.该产品含有完整的性能和调试工具集,能够收集程序运行时关键的性能信息和 ...
- Git系列六之标签管理
1.Git标签管理 当版本仓库内的数据有个大的改善或者功能更新,我们经常会打一个类似于软件版本号的标签,这样通过标签就可以将版本库中的某个历史版本给记录下来,方便我们随时将特定历史时期的数据取出来用, ...