写出一个你自己的MVC框架-基于对springMVC源码实现和理解(5):数据初始化(四)

来源:互联网 发布:notepad格式化php代码 编辑:程序博客网 时间:2024/05/21 10:34

DispatcherServlet中的数据初始化:

重写init():

[java] view plaincopy
  1. @Override  
  2.     public void init() throws ServletException {  
  3.   
  4.         logger.info("=====================MyDispatcherServlet init=====================");  
  5.   
  6.         logger.info("============================加载配置文件:===========================");  
  7.         loadConfigFile();  
  8.   
  9.         logger.info("=============================初始化参数============================");  
  10.         initParameter();  
  11.   
  12.         logger.info("=============================加载控制器============================");  
  13.         loadController();  
  14.   
  15.         logger.info("========================预映射URL和requestMap======================");  
  16.         mapMethod();  
  17.   
  18.         logger.info("=============================加载拦截器============================");  
  19.         loadInterceptor();  
  20.   
  21.         logger.info("=============================初始化完毕============================");  
  22.   
  23.     }  
加载配置文件:
[java] view plaincopy
  1. /* 
  2.      * 加载配置文件 
  3.      */  
  4.     private void loadConfigFile() {  
  5.         String mvcConfigLocation = getInitParameter("mvcConfigLocation");  
  6.         logger.info(mvcConfigLocation);  
  7.         InputStream inputStream = this.getServletContext().getResourceAsStream(  
  8.                 mvcConfigLocation);  
  9.   
  10.         p = new Properties();  
  11.   
  12.         try {  
  13.             p.load(inputStream);  
  14.         } catch (IOException e1) {  
  15.             e1.printStackTrace();  
  16.         }  
  17.     }  

初始化部分参数:
[java] view plaincopy
  1. /* 
  2.      * 初始化部分参数 
  3.      */  
  4.     private void initParameter() {  
  5.         viewPath = p.getProperty("myview.path");  
  6.         logger.info("映射view目录:" + viewPath);  
  7.     }  
加载控制器:
[java] view plaincopy
  1. /* 
  2.      * 加载控制器 
  3.      */  
  4.     private void loadController() {  
  5.   
  6.         String controllerPath = p.getProperty("controller.annotated.path");  
  7.         String filePath = "";  
  8.         String classPath = this.getClass().getClassLoader().getResource("")  
  9.                 .getPath();  
  10.   
  11.         filePath = classPath + controllerPath;  
  12.         List<String> allClassName = new ArrayList<String>();  
  13.   
  14.         MvcUtil.getAllClassName(classPath, filePath, allClassName);  
  15.   
  16.         for (String s : allClassName) {  
  17.             try {  
  18.   
  19.                 Class<?> c = Class.forName(s);  
  20.                 if (c.isAnnotationPresent(MyController.class)) {  
  21.                     cs.add(c);  
  22.                     logger.info("加载controller:" + c.getName());  
  23.                 }  
  24.             } catch (ClassNotFoundException e) {  
  25.                 e.printStackTrace();  
  26.             }  
  27.         }  
  28.     }  
 这里用了反射的方式加载被Controller注解的类,用到的工具类中的方法getAllClassName():
[java] view plaincopy
  1. public static List<String> getAllClassName(String classPath ,String filePath ,List<String> allClassName) {   
  2.         File dir = new File(filePath);  
  3.         File[] fs = dir.listFiles(); //包括子目录  
  4.           
  5.         String className = "";  
  6.           
  7.         for (File f : fs) {   
  8.             if (f.isDirectory()) {   
  9.                 getAllClassName(classPath,f.getAbsolutePath(),allClassName);   
  10.             } else {   
  11.                 className = f.getPath().replace(classPath.substring(1).replace("/""\\"), "").replace("\\", ".").replace(".class", "");  
  12.                 allClassName.add(className);         
  13.             }   
  14.         }   
  15.         return allClassName;  
  16.     }  
映射控制器方法并实例化hs中:
[java] view plaincopy
  1. /* 
  2.      * 映射控制器方法 
  3.      */  
  4.     private void mapMethod() {  
  5.   
  6.         Method[] ms = null;  
  7.         String rm = null;  
  8.         for (Class<?> c : cs) {  
  9.             ms = c.getMethods();  
  10.             String mappingUrl = "";  
  11.             for (Method m : ms) {  
  12.                 if (m.isAnnotationPresent(MyRequestMapping.class)) {  
  13.                     mappingUrl = this.getServletContext().getContextPath()  
  14.                             + m.getAnnotation(MyRequestMapping.class).value()  
  15.                                     .trim();  
  16.                     rm = m.getAnnotation(MyRequestMapping.class).method()  
  17.                             .trim().toUpperCase();  
  18.                     logger.info("映射url:" + mappingUrl);  
  19.                       
  20.                     try {  
  21.                         hs.put(mappingUrl + rm, new Handler(c.newInstance(), m, rm));// 先直接拼接字符串当key,以后再优化  
  22.                     } catch (InstantiationException e) {  
  23.                         e.printStackTrace();  
  24.                     } catch (IllegalAccessException e) {  
  25.                         e.printStackTrace();  
  26.                     }  
  27.                 }  
  28.             }  
  29.         }  
  30.     }  
加载拦截器存放在os中:
[java] view plaincopy
  1. /* 
  2.      * 加载控制器 
  3.      */  
  4.     private void loadInterceptor() {  
  5.         String controllerPath = p.getProperty("interception.path").trim();  
  6.         String filePath = "";  
  7.         String classPath = this.getClass().getClassLoader().getResource("")  
  8.                 .getPath();  
  9.   
  10.         filePath = classPath + controllerPath;  
  11.         List<String> allClassName = new ArrayList<String>();  
  12.   
  13.         MvcUtil.getAllClassName(classPath, filePath, allClassName);  
  14.   
  15.         String[] mappingPath = {};  
  16.         String interceptorMethod = "";  
  17.         int index = 0;  
  18.         for (String s : allClassName) {  
  19.             try {  
  20.                 Class<?> c = Class.forName(s);  
  21.                 if (c.isAnnotationPresent(MyInterceptor.class)) {  
  22.                     mappingPath = c.getAnnotation(MyInterceptor.class)  
  23.                             .mappingPath();  
  24.                     interceptorMethod = c.getAnnotation(MyInterceptor.class)  
  25.                             .interceptionMethod();  
  26.                     index = c.getAnnotation(MyInterceptor.class).index();  
  27.   
  28.                     os.add(new Obstruct(  
  29.                             InterceptorFactory.createInterceptor(c),  
  30.                             mappingPath, interceptorMethod, index));  
  31.                     logger.info("加载interceptor:" + c.getName());  
  32.                 }  
  33.             } catch (ClassNotFoundException e) {  
  34.                 e.printStackTrace();  
  35.             }  
  36.         }  
  37.         Collections.sort(os, new ComparatorObstructUtil());  
  38.     }  
至此数据初始化过程结束
0 0