从零开始写javaweb框架笔记16-搭建轻量级JAVAWEB框架-加载Controller,初始化框架

本文介绍了一种基于Java的轻量级框架的设计思路,包括ControllerHelper类的实现,用于处理请求与控制器之间的映射关系,以及HelperLoader类的使用,用于加载框架所需的辅助类。

1,加载Controller

     我们需要创建 一个ControllerHelper类,让它来处理下面的逻辑:
      通过ClassHelper我们可以获取所有定义了Controller注解的类,可以通过反射获取该类中所有带有Action注解的方法,获取Action注解中的请求表达式,进而获取请求方法与请求路径,封装一个请求对象(Request)和处理对象(Handler),最后将Request与Handler建立一个映射关系,存入一个Action Map中,并提供一个可以根据请求方法与请求路径获取处理对象的方法。

       首先定义一个名为Request的类,代码如下:

       

  1. package org.smart4j.framework.bean;  
  2.   
  3. import org.apache.commons.lang3.builder.EqualsBuilder;  
  4. import org.apache.commons.lang3.builder.HashCodeBuilder;  
  5.   
  6. /** 
  7.  * Created by jack on 2017/5/24. 
  8.  * 请求类,封装请求信息 
  9.  */  
  10. public class Request {  
  11.     /** 
  12.      * 请求方法 
  13.      */  
  14.     private String requestMethod;  
  15.     /** 
  16.      * 请求路径 
  17.      */  
  18.     private String requestPath;  
  19.   
  20.     public Request(String requestMethod, String requestPath) {  
  21.         this.requestMethod = requestMethod;  
  22.         this.requestPath = requestPath;  
  23.     }  
  24.   
  25.     public String getRequestMethod() {  
  26.         return requestMethod;  
  27.     }  
  28.   
  29.     public void setRequestMethod(String requestMethod) {  
  30.         this.requestMethod = requestMethod;  
  31.     }  
  32.   
  33.     public String getRequestPath() {  
  34.         return requestPath;  
  35.     }  
  36.   
  37.     public void setRequestPath(String requestPath) {  
  38.         this.requestPath = requestPath;  
  39.     }  
  40.   
  41.     @Override  
  42.     public int hashCode() {  
  43.         return HashCodeBuilder.reflectionHashCode(this);  
  44.     }  
  45.   
  46.     @Override  
  47.     public boolean equals(Object object) {  
  48.         return EqualsBuilder.reflectionEquals(this,object);  
  49.     }  
  50. }  


       然后编写一个Handler的类,代码如下:

  1. package org.smart4j.framework.bean;  
  2.   
  3. import java.lang.reflect.Method;  
  4.   
  5. /** 
  6.  * Created by jack on 2017/5/24. 
  7.  * 封装Action信息 
  8.  */  
  9. public class Handler {  
  10.     /** 
  11.      * Controller类 
  12.      */  
  13.     private Class<?> controllerClass;  
  14.     /** 
  15.      * Action方法 
  16.      */  
  17.     private Method actionMethod;  
  18.   
  19.     public Handler(Class<?> controllerClass, Method actionMethod) {  
  20.         this.controllerClass = controllerClass;  
  21.         this.actionMethod = actionMethod;  
  22.     }  
  23.   
  24.     public Class<?> getControllerClass() {  
  25.         return controllerClass;  
  26.     }  
  27.   
  28.     public void setControllerClass(Class<?> controllerClass) {  
  29.         this.controllerClass = controllerClass;  
  30.     }  
  31.   
  32.     public Method getActionMethod() {  
  33.         return actionMethod;  
  34.     }  
  35.   
  36.     public void setActionMethod(Method actionMethod) {  
  37.         this.actionMethod = actionMethod;  
  38.     }  
  39. }  


     最后是ControllerHelper的代码:

    

  1. package org.smart4j.framework.helper;  
  2.   
  3. import org.smart4j.framework.annotation.Action;  
  4. import org.smart4j.framework.bean.Handler;  
  5. import org.smart4j.framework.bean.Request;  
  6. import org.smart4j.framework.org.smart4j.framework.util.ArrayUtil;  
  7. import org.smart4j.framework.org.smart4j.framework.util.CollectionUtil;  
  8.   
  9. import java.lang.reflect.Method;  
  10. import java.util.HashMap;  
  11. import java.util.Map;  
  12. import java.util.Set;  
  13.   
  14. /** 
  15.  * Created by liangboqun on 2017/5/24. 
  16.  */  
  17. public final class ControllerHelper {  
  18.     /** 
  19.      * 用于存放请求与处理器的映射关系(简称Aciotn Map) 
  20.      */  
  21.     private static final Map<Request,Handler> ACTION_MAP = new HashMap<Request, Handler>();  
  22.     static {  
  23.         /** 
  24.          * 获取所有的Controller类 
  25.          */  
  26.         Set<Class<?>> controllerClassSet = ClassHelper.getControllerClassSet();  
  27.         if (CollectionUtil.isNotEmpty(controllerClassSet)){  
  28.             //遍历这些Controller  
  29.             for (Class<?> controllerClass : controllerClassSet) {  
  30.                 //获取Controller类中定义的方法  
  31.                 Method [] methods = controllerClass.getDeclaredMethods();  
  32.                 if (ArrayUtil.isNotEmpty(methods)){  
  33.                     //遍历Controller类中的方法  
  34.                     for (Method method : methods) {  
  35.                         //判断当前方法是否有Action注解  
  36.                         if (method.isAnnotationPresent(Action.class)){  
  37.                             //从Action注解中获取URL映射规则  
  38.                             Action action = method.getAnnotation(Action.class);  
  39.                             String mapping = action.value();  
  40.                             //验证URL映射规则  
  41.                             if (mapping.matches("\\w+:/\\w*")){  
  42.                                 String [] array = mapping.split(":");  
  43.                                 if (ArrayUtil.isNotEmpty(array) && array.length ==2){  
  44.                                     //获取请求方法与请求路径  
  45.                                     String requestMethod = array[0];  
  46.                                     String requestPath = array[1];  
  47.                                     Request request = new Request(requestMethod,requestPath);  
  48.                                     Handler handler = new Handler(controllerClass,method);  
  49.                                     //初始化Action Map  
  50.                                     ACTION_MAP.put(request,handler);  
  51.                                 }  
  52.                             }  
  53.                         }  
  54.                     }  
  55.                 }  
  56.             }  
  57.         }  
  58.     }  
  59.     /** 
  60.      * 获取Handler 
  61.      */  
  62.     public static Handler getHandler(String requestMethod,String requstPath){  
  63.         Request request = new Request(requestMethod,requstPath);  
  64.         return ACTION_MAP.get(request);  
  65.     }  
  66. }  

      可见,我们在ControllerHelper中封装了一个ActionMap,通过它来存放Request与Handler之间的映射关系,然后通过ClassHelper来获取所有带有Controller注解的类,接着遍历这些Controller类,从Action注解中提取URL,最后初始化Request与Handler之间的映射关系



    2,初始化框架

        通过上面的过程,我们创建了ClassHelper,BeanHelper,IocHelper,ControllerHelper这四个Helper类,需要通过一个入口程序来加载它们,实际上式加载它们的静态代码库,这个加载程序时HelperLoader,代码如下:

     

  1. package org.smart4j.framework;  
  2.   
  3. import org.smart4j.framework.helper.BeanHelper;  
  4. import org.smart4j.framework.helper.ClassHelper;  
  5. import org.smart4j.framework.helper.ControllerHelper;  
  6. import org.smart4j.framework.helper.IocHelper;  
  7. import org.smart4j.framework.org.smart4j.framework.util.ClassUtil;  
  8.   
  9. /** 
  10.  * Created by jack on 2017/5/24. 
  11.  * 加载相应类的,帮助类 
  12.  */  
  13. public final class HelperLoader {  
  14.     //初始化,加载类  
  15.     public static void init(){  
  16.         Class<?> [] classList = {ClassHelper.class, BeanHelper.class, IocHelper.class, ControllerHelper.class};  
  17.         for (Class<?> cls: classList) {  
  18.             ClassUtil.loadClass(cls.getName(),true);  
  19.         }  
  20.     }  
  21. }  

     现在可以直接调用HelperLoader的init方法来加载这些Helper类了。实际上当我们第一次访问类时,就会加载static块,这里只是为了加载更加集中,所以写了这个加载类。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值