手寫註解實現SpringMVC底層原理(雖簡單卻五臟俱全《注重思路》)

手寫註解實現SpringMVC底層原理

鄙人小白一個,還沒畢業,就我的而言,搞IT這塊,講究思路。若對此文疑慮,評論來戰。(本人->無期)
java

 

   1.首先咱們來搭建架構,就建一個普通的javaweb項目就OK了,具體目錄以下:

 

  

  對於小白來講能夠細看後面web.xml的配置,對javaweb有點研究能夠忽略而事後面的web.xml配置。web

      2.先上代碼,運行起整個項目。再來聊聊思路。

    

          (1).Controller註解
     
package com.wuqi.annotation;
import java.lang.annotation.*;
/**
 * Created by wuqi on 2017/3/22.
 */
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Controller {
    String value() default "";
}
Controller
     (2).Quatifier註解
     
package com.wuqi.annotation;

import java.lang.annotation.*;

/**
 * Created by wuqi on 2017/3/25.
 */
@Target({ ElementType.FIELD }) // 表明註解的註解
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Quatifier {
    String value() default "";
}
Quatifier

 

      (3).RequestMapping註解
     
package com.wuqi.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * Created by Shock on 2017/3/22.
 */
@Target({ ElementType.METHOD }) // 在方法上的註解
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestMapping {
    String value() default "";
}
RequestMapping

 

          (4).Service註解
     
package com.wuqi.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * Created by Shock on 2017/3/22.
 */
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Service {
    String value() default "";
}
Service

 --------------------------------------------------------------------------------------------------------------------------架構

  

     (1).MyService接口
    
package com.wuqi.service.impl;

import java.util.Map;

/**
 * Created by wuqi on 2017/3/23.
 */
public interface MyService {

    int insert(Map map);

    int delete(Map map);

    int update(Map map);

    int select(Map map);

}
MyService

 

     (2).MyServiceImpl類
    
package com.wuqi.service.impl;
import com.wuqi.annotation.Service;

import java.util.Map;
/**
 * Created by wuqi on 2017/3/23.
 */
@Service("MyServiceImpl")
public class MyServiceImpl implements MyService {
    @Override
    public int insert(Map map) {
        System.out.println("MyServiceImpl:" + "insert");
        return 0;
    }

    @Override
    public int delete(Map map) {
        System.out.println("MyServiceImpl:" + "delete");
        return 0;
    }

    @Override
    public int update(Map map) {
        System.out.println("MyServiceImpl:" + "update");
        return 0;
    }

    @Override
    public int select(Map map) {
        System.out.println("MyServiceImpl:" + "select");
        return 0;
    }
}
MyServiceImpl

 

      (3).SpringmvcService接口
    
package com.wuqi.service.impl;

import java.util.Map;

/**
 * Created by wuqi on 2017/3/23.
 */
public interface SpringmvcService {
    int insert(Map map);

    int delete(Map map);

    int update(Map map);

    int select(Map map);
}
SpringmvcService

 

   (4).MyServiceImpl類
    
package com.wuqi.service.impl;
import com.wuqi.annotation.Service;

import java.util.Map;
/**
 * Created by wuqi on 2017/3/23.
 */
@Service("SpringmvcServiceImpl")
public class SpringmvcServiceImpl implements SpringmvcService{
    @Override
    public int insert(Map map) {
        System.out.println("SpringmvcServiceImpl:" + "insert");
        return 0;
    }
    @Override
    public int delete(Map map) {
        System.out.println("SpringmvcServiceImpl:" + "delete");
        return 0;
    }
    @Override
    public int update(Map map) {
        System.out.println("SpringmvcServiceImpl:" + "update");
        return 0;
    }
    @Override
    public int select(Map map) {
        System.out.println("SpringmvcServiceImpl:" + "select");
        return 0;
    }

}
SpringmvcServiceImpl

 --------------------------------------------------------------------------------------------------------------------------mvc

   

  (1).SpringmvcController類
  
package com.wuqi.controller;
import com.wuqi.annotation.*;
import com.wuqi.service.impl.MyService;
import com.wuqi.service.impl.SpringmvcService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Created by wuqi on 2017/3/23.
 */
@Controller("wuqi")
public class SpringmvcController {
    @Quatifier("MyServiceImpl")
    MyService myService;
    @Quatifier("SpringmvcServiceImpl")
    SpringmvcService smService;

    @RequestMapping("insert")
    public String insert(HttpServletRequest request, HttpServletResponse response, String param) {
        myService.insert(null);
        smService.insert(null);
        return null;
    }

    @RequestMapping("delete")
    public String delete(HttpServletRequest request, HttpServletResponse response, String param) {
        myService.delete(null);
        smService.delete(null);
        return null;
    }

    @RequestMapping("update")
    public String update(HttpServletRequest request, HttpServletResponse response, String param) {
        myService.update(null);
        smService.update(null);
        return null;
    }

    @RequestMapping("select")
    public String select(HttpServletRequest request, HttpServletResponse response, String param) {
        myService.select(null);
        smService.select(null);
        return null;
    }
}
SpringmvcController

  --------------------------------------------------------------------------------------------------------------------------app

  

      (1).DispatcherServlet類繼承 javax.servlet.http.HttpServlet類
  
package com.wuqi.servlet;

import com.wuqi.annotation.*;
import com.wuqi.controller.SpringmvcController;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Shock on 2017/3/23.
 */
public class DispatcherServlet extends HttpServlet{
    private static final long serialVersionUID = 1L;
    List<String> packageNames = new ArrayList<String>();
    // 全部類的實例,key是註解的value,value是全部類的實例
    Map<String, Object> instanceMap = new HashMap<String, Object>();
    Map<String, Object> handerMap = new HashMap<String, Object>();
    public DispatcherServlet() {
        super();
    }

    public void init(ServletConfig config) throws ServletException {
        // 包掃描,獲取包中的文件
        scanPackage("com.wuqi");
        try {
            filterAndInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 創建映射關係
        handerMap();
        // 實現注入
        ioc();
    }

    private void filterAndInstance() throws Exception {
        if (packageNames.size() <= 0) {
            return;
        }
        for (String className : packageNames) {
            Class<?> cName = Class.forName(className.replace(".class", "").trim());
            if (cName.isAnnotationPresent(Controller.class)) {
                Object instance = cName.newInstance();
                Controller controller = (Controller) cName.getAnnotation(Controller.class);
                String key = controller.value();
                instanceMap.put(key, instance);
            } else if (cName.isAnnotationPresent(Service.class)) {
                Object instance = cName.newInstance();
                Service service = (Service) cName.getAnnotation(Service.class);
                String key = service.value();
                instanceMap.put(key, instance);
            } else {
                continue;
            }
        }
    }

    private void ioc() {

        if (instanceMap.isEmpty())
            return;
        for (Map.Entry<String, Object> entry : instanceMap.entrySet()) {
            // 拿到裏面的全部屬性
            Field fields[] = entry.getValue().getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);// 可訪問私有屬性
                if (field.isAnnotationPresent(Quatifier.class));
                Quatifier quatifier = field.getAnnotation(Quatifier.class);
                String value = quatifier.value();
                field.setAccessible(true);
                try {
                    field.set(entry.getValue(), instanceMap.get(value));
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        SpringmvcController wuqi = (SpringmvcController) instanceMap.get("wuqi");
        System.out.print(wuqi);
    }

    /**
     * 掃描包下的全部文件
     *
     * @param Package
     */
    private void scanPackage(String Package) {
        URL url = this.getClass().getClassLoader().getResource("/" + replaceTo(Package));// 將全部的.轉義獲取對應的路徑
        String pathFile = url.getFile();
        File file = new File(pathFile);
        String fileList[] = file.list();
        for (String path : fileList) {
            File eachFile = new File(pathFile + path);
            if (eachFile.isDirectory()) {
                scanPackage(Package + "." + eachFile.getName());
            } else {
                packageNames.add(Package + "." + eachFile.getName());
            }
        }
    }

    /**
     * 創建映射關係
     */
    private void handerMap() {
        if (instanceMap.size() <= 0)
            return;
        for (Map.Entry<String, Object> entry : instanceMap.entrySet()) {
            if (entry.getValue().getClass().isAnnotationPresent(Controller.class)) {
                Controller controller = (Controller) entry.getValue().getClass().getAnnotation(Controller.class);
                String ctvalue = controller.value();
                Method[] methods = entry.getValue().getClass().getMethods();
                for (Method method : methods) {
                    if (method.isAnnotationPresent(RequestMapping.class)) {
                        RequestMapping rm = (RequestMapping) method.getAnnotation(RequestMapping.class);
                        String rmvalue = rm.value();
                        handerMap.put("/" + ctvalue + "/" + rmvalue, method);
                    } else {
                        continue;
                    }
                }
            } else {
                continue;
            }

        }
    }

    private String replaceTo(String path) {
        return path.replaceAll("\\.", "/");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String url = req.getRequestURI();
        String context = req.getContextPath();
        String path = url.replace(context, "");
        Method method = (Method) handerMap.get(path);
        SpringmvcController controller = (SpringmvcController) instanceMap.get(path.split("/")[1]);
        try {
            method.invoke(controller, new Object[] { req, resp, null });
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
DispatcherServlet

 

全部的代碼已經貼上,還有個web.xmlide

  
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
           version="3.1">
    <servlet>
        <servlet-name>testServlet</servlet-name>
        <servlet-class>com.wuqi.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <!-- ... -->
    <servlet-mapping>
        <servlet-name>testServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>
web.xml

 

  3.好了代碼已經貼上了,如今須要來講說思路,由於有代碼了,因此用代碼來將思路,這樣更容易理解代碼的含義。以後能夠根據本身的程度去試着寫。

  •     首先咱們須要掃描包中的全部文件(DispatcherServlet -> init(ServletConfig config) -> scanPackage("com.wuqi")),也就是含有註解的文件。而後將該包下的全部文件都存入packageNames集合中。
  •        這時咱們拿到了包下全部的文件,但咱們只須要含有咱們指定註解的那部分文件,所以須要過濾出咱們想要的文件便可,而且在過濾的過程當中,咱們能夠將過濾出來的類經過Class.forName來直接實例化並儲存起來。存放到instanceMap集合中,併爲其設置對應的key值,該key值就是類註解的value。
  •           而後遍歷instanceMap集合中的全部對象,獲取指定註解的對象,並經過反射獲取該對象的全部的方法,遍歷全部的方法,將指定註解的方法存入handerMap,key爲拼接字符串("/" + 對象變量名 + "/" + 方法名),value爲方法(Method)。
  •           而後咱們能夠遍歷instanceMap集合中的全部對象,經過反射獲取對象的全部屬性值(字段)集合,而後遍歷屬性值集合,將屬性值含有指定註解的,經過Field的set方法爲該屬性值賦值,這時就將對象注入了。(也就是往Controller中注入Service對象)
  •          最後就能夠經過反射的invoke方法調用某個對象的某個方法。(此時對象存於instanceMap,而方法都存於handerMap

 

借鑑 -> http://blog.csdn.net/chaoyueygw/article/details/53393952該博文

this

相關文章
相關標籤/搜索