详解Java注解的实现与使用方法
详解Java注解的实现与使用方法
Java注解是java5版本发布的,其作用就是节省配置文件,增强代码可读性。在如今各种框架及开发中非常常见,特此说明一下。
如何创建一个注解
每一个自定义的注解都由四个元注解组成,这四个元注解由java本身提供:
@Target(ElementType.**)
这是一个枚举,它置顶是该自定义的注解使用的地方,像类、变量、方法等
@Retention(RetentionPolicy.**)作用是标明注解保存在什么级别,像在编译时、class文件中,vm运行中
@Documented将此注解包含在javadoc中,它代表着此注解会被javadoc工具提取成文档。在doc文档中的内容会因为此注解的信息内容不同而不同
@Inherited:在您定义注解后并使用于程序代码上时,预设上父类别中的注解并不会被继承至子类别中,您可以在定义注解时加上java.lang.annotation.Inherited限定的Annotation,这让您定义的Annotation型别被继承下来。
介绍完理论,开始代码(talkischeap,showyourcode)
packagecom.yasin.JavaLearn;
importjava.lang.annotation.Documented;
importjava.lang.annotation.ElementType;
importjava.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
importjava.lang.annotation.Target;
/**
*这是一个类级别的注释,这个注释中有一个name字段,默认值是yasin
*@authoryasin
*
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public@interfaceLearn{
Stringname()default"yasin";
}
packagecom.yasin.JavaLearn;
importjava.lang.annotation.Documented;
importjava.lang.annotation.ElementType;
importjava.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
importjava.lang.annotation.Target;
/**
*这是一个变量级别的注解,注解中有一个字段name,默认值是field
*@authoryasin
*
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public@interfaceFiledLearn{
Stringname()default"field";
}
packagecom.yasin.JavaLearn;
importjava.lang.annotation.Documented;
importjava.lang.annotation.ElementType;
importjava.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
importjava.lang.annotation.Target;
/**
*这是一个方法级别的注解
*@authoryasin
*
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public@interfaceMethodLearn{
Stringname()default"method";
}
上面了我定义了三个注解,分别是常用的类、变量、方法三个级别的注解。
下面我定义一个类,使用这三个注解
packagecom.yasin.JavaLearn;
@Learn
publicclassYasin{
@FiledLearn
publicintlevel;
@FiledLearn(name="xq")
publicStringxq;
publicStringa;
@MethodLearn(name="test")
publicvoidsetMain(){
}
publicvoidsetA(){
}
}
下面就是如何使用这个注解了,注解的提取,都是通过class反射得到相应的变量和方法,在从变量和方法中获得注解。
packagecom.yasin.JavaLearn;
importjava.lang.reflect.Field;
importjava.lang.reflect.Method;
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.util.ArrayList;
importjava.util.Date;
importjava.util.HashMap;
importjava.util.Iterator;
importjava.util.List;
importjava.util.Map;
importorg.apache.commons.logging.Log;
importorg.apache.commons.logging.LogFactory;
importorg.apache.log4j.Logger;
importorg.apache.log4j.PropertyConfigurator;
importorg.apache.log4j.xml.DOMConfigurator;
/**
*Helloworld!
*
*/
publicclassApp{
publicstaticvoidmain(String[]args){
Learnlearn=Yasin.class.getAnnotation(Learn.class);
System.out.println(learn.name());
Field[]fields=Yasin.class.getFields();//获取该类所有的字段
for(Fieldfiled:fields){
if(filed.isAnnotationPresent(FiledLearn.class)){//校验该字段是否添加这个注解
System.out.println(filed.getName());
FiledLearnfiledLearn=filed.getAnnotation(FiledLearn.class);
System.out.println(filedLearn.name());
}
}
Method[]methods=Yasin.class.getMethods();
for(Methodmethod:methods){
if(method.isAnnotationPresent(MethodLearn.class)){//校验该方法是否有这个注解
System.out.println(method.getName());
MethodLearnmethodLearn=method.getAnnotation(MethodLearn.class);
System.out.println(methodLearn.name());
}
}
}
}
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!