详解Java注解的实现与使用方法
作者:lqh 时间:2023-10-31 12:33:20
详解Java注解的实现与使用方法
Java注解是java5版本发布的,其作用就是节省配置文件,增强代码可读性。在如今各种框架及开发中非常常见,特此说明一下。
如何创建一个注解
每一个自定义的注解都由四个元注解组成,这四个元注解由java本身提供:
@Target(ElementType.**)
这是一个枚举,它置顶是该自定义的注解使用的地方,像类、变量、方法等
@Retention(RetentionPolicy.**)作用是标明注解保存在什么级别,像在编译时、class文件中,vm运行中
@Documented 将此注解包含在 javadoc 中 ,它代表着此注解会被javadoc工具提取成文档。在doc文档中的内容会因为此注解的信息内容不同而不同
@Inherited : 在您定义注解后并使用于程序代码上时,预设上父类别中的注解并不会被继承至子类别中,您可以在定义注解时加上java.lang.annotation.Inherited 限定的Annotation,这让您定义的Annotation型别被继承下来。
介绍完理论,开始代码(talk is cheap,show your code)
package com.yasin.JavaLearn;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 这是一个类级别的注释,这个注释中有一个name字段,默认值是 yasin
* @author yasin
*
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Learn {
String name() default "yasin";
}
package com.yasin.JavaLearn;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 这是一个变量级别的注解,注解中有一个字段name,默认值是field
* @author yasin
*
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FiledLearn {
String name() default "field";
}
package com.yasin.JavaLearn;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 这是一个方法级别的注解
* @author yasin
*
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MethodLearn {
String name() default "method";
}
上面了我定义了三个注解,分别是常用的类、变量、方法三个级别的注解。
下面我定义一个类,使用这三个注解
package com.yasin.JavaLearn;
@Learn
public class Yasin {
@FiledLearn
public int level;
@FiledLearn(name="xq")
public String xq;
public String a;
@MethodLearn(name="test")
public void setMain(){
}
public void setA(){
}
}
下面就是如何使用这个注解了,注解的提取,都是通过class反射得到相应的变量和方法,在从变量和方法中获得注解。
package com.yasin.JavaLearn;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.xml.DOMConfigurator;
/**
* Hello world!
*
*/
public class App {
public static void main(String[] args) {
Learn learn = Yasin.class.getAnnotation(Learn.class);
System.out.println(learn.name());
Field[] fields = Yasin.class.getFields();//获取该类所有的字段
for(Field filed:fields){
if(filed.isAnnotationPresent(FiledLearn.class)){//校验该字段是否添加这个注解
System.out.println(filed.getName());
FiledLearn filedLearn = filed.getAnnotation(FiledLearn.class);
System.out.println(filedLearn.name());
}
}
Method[] methods = Yasin.class.getMethods();
for(Method method:methods){
if(method.isAnnotationPresent(MethodLearn.class)){//校验该方法是否有这个注解
System.out.println(method.getName());
MethodLearn methodLearn = method.getAnnotation(MethodLearn.class);
System.out.println(methodLearn.name());
}
}
}
}
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://blog.csdn.net/u013592964/article/details/70770337
标签:Java,注解
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java中struts2实现文件上传下载功能
2021-11-16 19:53:47
Android图片处理工具类BitmapUtils
2022-12-28 05:23:39
Java 实战项目锤炼之嘟嘟健身房管理系统的实现流程
2021-09-24 23:58:07
![](https://img.aspxhome.com/file/2023/0/125250_0s.png)
Unity3D动态对象优化代码分享
2023-12-22 09:38:24
java中maven下载和安装步骤说明
2022-03-05 23:07:59
Java必备知识之位运算及常见进制解读
2022-02-10 00:42:19
![](https://img.aspxhome.com/file/2023/5/92715_0s.jpg)
Feign调用可重试的最佳方案分享
2021-06-09 12:19:23
java 多线程的几种实现方法总结
2023-10-14 17:43:18
java实现Base64加密解密算法
2023-11-25 08:07:27
java中如何使用HttpClient调用接口
2021-10-13 11:19:14
Mybatis整合达梦数据库的完整步骤记录
2023-11-23 07:15:37
![](https://img.aspxhome.com/file/2023/5/59215_0s.png)
java实现上传图片尺寸修改和质量压缩
2023-04-04 03:42:55
springboot使用mybatis一对多的关联查询问题记录
2023-05-25 14:31:03
![](https://img.aspxhome.com/file/2023/0/72650_0s.png)
浅谈C#中List<T>对象的深度拷贝问题
2022-12-16 13:09:10
C#开发Winform程序调用存储过程
2023-01-11 05:23:15
![](https://img.aspxhome.com/file/2023/8/96298_0s.png)
Java实现用户管理系统
2023-08-26 17:18:21
Android 基于IntentService的文件下载的示例代码
2023-10-28 23:26:02
使用JPA双向多对多关联关系@ManyToMany
2021-12-04 03:37:12
![](https://img.aspxhome.com/file/2023/8/129118_0s.png)
C# WinForm快捷键设置技巧
2023-07-20 07:03:13
IDEA远程连接HBase及其Java API实战详解
2023-11-27 21:54:46
![](https://img.aspxhome.com/file/2023/9/60169_0s.png)