Java注解探究,自定义注解封装简易网络请求框架
注解的定义:提供一种为程序设置元数据的方法。
基本原则:注解不能干扰程序代码的运行,无论增加或删除注解,代码都能够正常运行。
按照使用分类,可以分为系统注解和自定义注解。
A:系统注解分为标准注解和元注解
1.标准注解 我们都见过好多,比如下面的:
@Override 复写父类方法或者实现接口方法的提示*
@Deprecated 方法过时的提示
@SuppressWarnings 解除编译器的警告,它参数如下:
deprecation 使用了过时的方法或者类是的警告
unchecked 执行了未检查的转换时的警告
failthrouth 当switch语句直接通往下一种情况,而没有break时的警告
path 在类路径,源文件路径等中有不存在的路径时的警告
serial 当在可序列化的类上缺少id
finally 任何finally子句不能正常完成的警告
all 关于以上所有警告的情况
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
2.元注解 负责注解其他注解的注解,常见如下:
@Retention 注释的生命周期,其参数RetentPoicy 分类如下:
Source 注解只在源码中存在
Class 注解在源码和字节码中存在
Runtime 注解在源码,字节码和运行时都存在
- 1
- 2
- 3
- 4
示例代码:
@Retention(RetentionPolicy.RUNTIME)//元注解,标记注解生命周期
- 1
@Target 注解可以作用在什么地方,其参数如下:
ElementType.Constructor 构造方法中
ElementType.Field 成员变量和枚举常量
ElementType.Local_Variable 局部变量
ElementType.Method 方法
ElementType.package 包
ElementType.parameter 方法参数
ElementType.type 接口,类,枚举,注解
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
实例代码:
@Target(ElementType.METHOD)//元注解,标记作用范围在方法中
- 1
@Documented将注释体现在doc文档中
示例代码:
@Documented //元注解,标记生产文档时不清除
- 1
@Inhertied 允许子类继承父类的注解
B 注解注解元素数据类型
所有基本数据类型
String
void
Class
枚举
Annotation
- 1
- 2
- 3
- 4
- 5
- 6
- 7
C 类中注解提取方法
getAnnotation 返回该类上指定类型的注解
- 1
- 2
//前面说注解可以target在哪个位置,就用哪个目标字段去获取
//比如前面@Target(ElementType.METHOD),getAnnotation就用Method去获取,Method.getAnnotation(),下面是都以此类推
- 1
- 2
getAnnotations 返回该类上的所有注解
IsAnnotationPresent 判断该类上是否包含指定类型的注解
getDeclaredAnnotations 忽略继承的注解,获取所有注解
- 1
- 2
- 3
- 4
概念性的东西扯得太多了,话不多说,代码撸起来,自定义注解,
首先查看java 测试类的代码:
public class TestAnotation {
//创建线程池来执行任务
static ThreadPoolExecutor executor=new
ThreadPoolExecutor(4, //核心线程数
12, //最大线程数
60, //线程空闲周期
TimeUnit.SECONDS, //周期单位s
new SynchronousQueue<>()//创建一个任务队列
);
public static void main(String[] args) {
// TODO Auto-generated method stub
requestFromNet();
}
@RequestUrl(Constant.NETURL)
@RequstType(Type.GET)
public static void requestFromNet(){
executor.execute(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
String result=RequestNet.parseRequest(new TestAnotation());
System.out.println("网络请求成功!!!结果:=="+result);
}
});
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
注意:上面用到了两个自定义注解(网络请求地址和请求类型),下面是注解的内容
自定义网址注解:
@Target(ElementType.METHOD)//元注解,标记该注解作用范围
@Retention(RetentionPolicy.RUNTIME)////元注解,标记该注解生命周期
@Documented //元注解,标记生成javadoc时解释说明该注解
public @interface RequestUrl {
//定义网络URL类型为String
String value () default "";//默认请求网址为空
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
自定义网络请求类型注解:
/**
* 自定义注解
* 注解类型支持所有的基本数据类型,包括枚举
* @author Administrator
*
*/
@Target(ElementType.METHOD)//元注解,标记作用范围在方法中
@Documented //元注解,标记生产文档时不清除
@Retention(RetentionPolicy.RUNTIME)//元注解,标记注解生命周期,有运行时,class
public @interface RequstType {
//用枚举指定请求类型
enum Type{GET,POST};
//返回类型是Type中的一个,默认是GET
Type value() default Type.POST;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
注解的本质是接口,它的实现是用java反射来实现的,先面试看注解使用代码
public class RequestNet {
public static String doGetRequest(String url){
String result="";
if(null==url){
try {
throw new Throwable( "empty netUrl!!!");
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
HttpURLConnection conneciton = null;
try {
URL mURL = new URL(url);
conneciton=(HttpURLConnection) mURL.openConnection();
InputStream inputs= conneciton.getInputStream();
String line="";
BufferedReader reader = new BufferedReader(new InputStreamReader(inputs));
StringBuilder stringBuilder = new StringBuilder();
while((line=reader.readLine())!=null){
stringBuilder.append(line+"\r\n");
}
result=stringBuilder.toString();
System.out.println("test 网络请求结果:"+result);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("test 网络异常原因:"+e.toString());
}finally{
//下面是谷歌官方推荐的,详见谷歌官方文档
if(conneciton!=null){
conneciton.disconnect();
}
}
return result;
}
public static String doPostRequest(String url){
return null;
}
//解析传入的对象中含有的注解
//注解底层就是反射
public static String parseRequest(Object obj){
String url="";
String result="";
//第一步。获取class文件
Class clazz= obj.getClass();
System.out.println("test class 名字:"+clazz.getSimpleName());
//第二部,注解都是在方法上的,获取所有的方法
Method[] methods=clazz.getMethods();
//第三步:遍历方法
for (Method method : methods) {
System.out.println("test 方法名字:"+method.getName());
//判断每个方法上的注解是否含有
if(method.isAnnotationPresent(RequestUrl.class)){
//拿出方法上的这个注解
RequestUrl urlAnotation=method.getAnnotation(RequestUrl.class);
//拿出注解传入的值
url=urlAnotation.value();
System.out.println("test 网址的值11:"+url);
}
if(method.isAnnotationPresent(RequstType.class)){
RequstType requestType=method.getAnnotation(RequstType.class);
if(requestType.value().equals(Type.GET)){
//执行GET网络请求
result=doGetRequest(url);
}
if(requestType.value().equals(Type.POST)){
//执行POST网络请求
result=doPostRequest(url);
}
}
}
return result;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
辅助类Constant中存储着网络请求网址
/**
* 网络参数标记
* @author Administrator
*/
public class Constant {
public static final String NETURL="http://118.26.64.162:9832/app/"
+ "rest/app/articleCategory/list/news";
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
最后查看运行情况如下图:
版权声明:本文为博主原创文章,未经博主允许不得转载。