在C/S中,客户端会向服务器发出各种请求,而服务器就要根据请求做出对应的响应。
实际上就是客户机上执行某一个方法,将方法返回值,通过字节流的方式传输给服务器,服务器找到该请求对应的响应方法,并执行,将结果再次通过字节流的方式传输给客户机!
下面搭建一个简单的Request和Response分发器:
类标识的注解,只有带有该标识,才进行之后方法的扫描,否则不进行:
1 importjava.lang.annotation.ElementType;2 importjava.lang.annotation.Retention;3 importjava.lang.annotation.RetentionPolicy;4 importjava.lang.annotation.Target;5
6 @Retention(RetentionPolicy.RUNTIME)7 @Target(ElementType.TYPE)8 public @interfaceService {9 }
方法的注解, 必须对注解中的action赋值,往后我们是要将action的值作为map中的键:
1 importjava.lang.annotation.ElementType;2 importjava.lang.annotation.Retention;3 importjava.lang.annotation.RetentionPolicy;4 importjava.lang.annotation.Target;5
6 @Retention(RetentionPolicy.RUNTIME)7 @Target(ElementType.METHOD)8 public @interfaceActioner {9 String action();10 }
方法参数的注解,同样要对其name赋值,为了以后能够找到对应的参数,完成赋值:
1 importjava.lang.annotation.ElementType;2 importjava.lang.annotation.Retention;3 importjava.lang.annotation.RetentionPolicy;4 importjava.lang.annotation.Target;5
6 @Retention(RetentionPolicy.RUNTIME)7 @Target(ElementType.PARAMETER)8 public @interfaceAParameter {9 String name();10 }
我们需要将方法抽象成一个类,封装起来:
1 importjava.lang.reflect.Method;2 importjava.lang.reflect.Parameter;3 importjava.util.List;4
5 public classActionDefination {6 private Class> klass; //该方法所对应的类
7 private Object object; //执行该方法的对象
8 private Method method; //该方法
9 private List paramerterList; //该方法的所有参数
10
11 protected ActionDefination(Class> klass, Object object, Method method, ListparamerterList) {12 this.klass =klass;13 this.object =object;14 this.method =method;15 this.paramerterList =paramerterList;16 }17
18 protected Class>getKlass() {19 returnklass;20 }21
22 protectedObject getObject() {23 returnobject;24 }25
26 protectedMethod getMethod