Java元注解Retention代码示例介绍
作者:niuyongzhi 时间:2023-10-21 02:32:32
1.注解声明:通过@interface就可以声明一个注解。
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface BindView {
int value();
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Get {
String value() default "";
}
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface Queue {
String value() ;
}
2. @Target 元注解,注解的注解,它的取值定义在ElementType枚举类中。
@Target注解 用来定义我们自定义注解代码的什么位置。
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.ANNOTATION_TYPE})
public @interface Target {
ElementType[] value();
}
1)ElementType.FIELD 使用在成员变量上。
2)ElementType.METHOD 使用在成员方法上。
3)ElementType.PARAMETER 使用在方法参数上。
4)ElementType.TYPE 使用在类、接口上。
5)ElementType.ANNOTATION_TYPE 使用在注解上。
3.@Retention 元注解,取值定义在RetentionPolicy枚举类中。
用来定义注解生效的阶段:
1)SOURCE:注解只在源码阶段有效,不会编译到字节码中。
2)CLASS:注解在源码、字节码阶段有效,运行阶段不存在。
3)RUNTIME:注解在源码、字节码、运行阶段有效,也是最长用的。
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.ANNOTATION_TYPE})
public @interface Retention {
RetentionPolicy value();
}
public enum RetentionPolicy {
SOURCE,
CLASS,
RUNTIME;
private RetentionPolicy() {
}
}
2.注解的使用
@BindView(R.id.start_activity)
TextView startTextView;
@Get("http://www.baidu.com")
Call getPerson(@Queue("name") String name,@Queue("200")int price);
@Get("http://www.baidu.com")
Call getPerson();
注解的使用很简单。
注解单独存在没有任何意义,必须配合其他技术。
应用:
1)注解+Apt注解处理器,生产java代码 ,databinding、butterknife、dagger2 hilt
2)注解+代码埋点
3)注解+反射+ * retrofit xUtils lifecycle
以上应用会在后面的文章继续分享。如果通过反射来获取注解上的值,Retrofit框架原理。
来源:https://blog.csdn.net/niuyongzhi/article/details/125837112
标签:Java,Retention,元注解
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Ubuntu16.04下配置VScode的C/C++开发环境
2023-10-22 19:21:17
![](https://img.aspxhome.com/file/2023/6/123856_0s.jpg)
java根据模板导出PDF的详细实现过程
2022-04-07 21:48:52
![](https://img.aspxhome.com/file/2023/6/77346_0s.png)
Android自定义View实现拖动自动吸边效果
2021-11-18 03:00:38
![](https://img.aspxhome.com/file/2023/3/93273_0s.gif)
Java Grpc实例创建负载均衡详解
2022-03-07 17:50:15
![](https://img.aspxhome.com/file/2023/7/81947_0s.png)
Java虚拟机使用jvisualvm工具远程监控tomcat内存
2023-11-28 22:15:49
![](https://img.aspxhome.com/file/2023/2/60462_0s.png)
JAVA中的字符串常量池使用操作代码
2021-11-24 02:00:57
![](https://img.aspxhome.com/file/2023/2/66932_0s.png)
Java synchronized关键_动力节点Java学院整理
2023-11-10 11:08:53
最简单的MyBatis Plus的多表联接、分页查询实现方法
2022-07-10 05:21:49
![](https://img.aspxhome.com/file/2023/0/129270_0s.png)
Android开发之多媒体文件获取工具类实例【音频,视频,图片等】
2022-03-05 22:44:24
Android使用AIDL方式实现播放音乐案例
2022-01-04 23:58:40
OpenCV实现图像背景虚化效果原理详解
2021-12-06 02:25:59
![](https://img.aspxhome.com/file/2023/2/109782_0s.jpg)
Android获取设备传感器的方法
2022-10-07 10:50:16
![](https://img.aspxhome.com/file/2023/9/137219_0s.jpg)
C#利用ScriptControl动态执行JS和VBS脚本
2023-04-29 21:26:13
mybatis 字段名自动转小写的实现
2021-09-09 20:04:26
Android自定义View仿探探卡片滑动效果
2023-03-18 14:54:16
![](https://img.aspxhome.com/file/2023/0/103910_0s.gif)
SpringMVC源码解读之 HandlerMapping - AbstractDetectingUrlHandlerMapping系列初始化
2023-02-12 16:14:21
Android实现拍照或者选取本地图片
2022-06-16 03:58:31
![](https://img.aspxhome.com/file/2023/6/120066_0s.jpg)
c语言switch反汇编的实现
2023-06-29 03:38:17
![](https://img.aspxhome.com/file/2023/6/99286_0s.png)
Android自定义带水滴的进度条样式(带渐变色效果)
2023-03-02 06:45:02
![](https://img.aspxhome.com/file/2023/0/138130_0s.jpg)
unity shader实现玻璃折射效果
2022-04-16 08:22:36
![](https://img.aspxhome.com/file/2023/6/77226_0s.gif)