閒話很少說,直接上代碼!java
一、第一步,首先搭建以下架構,其中,annotation中放置本身編寫的註解,主要包括service controller qualifier RequestMapping面試
第二步:完成對應的annotation:架構
package com.chaoyue.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; /** * Controller註解 * @author 超越 * @Date 2016年11月29日,上午10:37:30 * @motto 人在一塊兒叫聚會,心在一塊兒叫團隊 * @Version 1.0 */ @Target({ ElementType.TYPE }) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Controller { String value() default ""; } package com.chaoyue.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; /** * Quatifier註解 * @author 超越 * @Date 2016年11月29日,上午10:47:52 * @motto 人在一塊兒叫聚會,心在一塊兒叫團隊 * @Version 1.0 */ @Target({ ElementType.FIELD }) // 表明註解的註解 @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Quatifier { String value() default ""; } package com.chaoyue.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; /** * RequestMapping註解 * @author 超越 * @Date 2016年11月29日,上午10:39:32 * @motto 人在一塊兒叫聚會,心在一塊兒叫團隊 * @Version 1.0 */ @Target({ ElementType.METHOD }) // 在方法上的註解 @Retention(RetentionPolicy.RUNTIME) @Documented public @interface RequestMapping { String value() default ""; }
package com.chaoyue.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; /** * 註解Service * @author 超越 * @Date 2016年11月29日,上午10:49:47 * @motto 人在一塊兒叫聚會,心在一塊兒叫團隊 * @Version 1.0 */ @Target({ ElementType.TYPE }) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Service { String value() default ""; }
二、第二步:編寫對應的servlet類,記得勾選init()方法,用來進行相應的實例化和註解反轉控制。併發
① 進行包掃描,就是初始化的時候先將整個項目中的包進行掃描,掃描各個文件分別存起來。mvc
scanPackage("com.chaoyue");//本身的項目,測試用的 因此 掃描包函數的地址寫死了app
存在List<String> packageNames=new ArrayList<String>();其中都是這樣:com.chaoyue.annotation.Controller.class,com.chaoyue.annotation.Quatifier.class, com.chaoyue.annotation.RequestMapping.class,有.class後綴。分佈式
②過濾和實例化 :因爲已經將全部的文件都存在了packageNames中了,那麼咱們必須將對應的Controller實例化才能夠進行相應函數調用,而後其中的全部文件並不必定都是對應的controller文件,因此要進行相應的過濾和處理ide
filterAndInstance();函數
過濾後的結果保存在: Map<String,Object> instanceMap=new HashMap<String,Object>();高併發
其中 String是註解的value, Object是所對應類的實例
③創建一個映射關係(地址映射,不一樣的地址映射到不一樣的方法):
handerMap();
結果: Map<String,Object> handerMap=new HashMap<String,Object>();
實例:
④ 反轉控制,根據註解,把service中的注入到controller中的service;
歡迎留言討論,帶着謙虛的心一塊兒學習!也能夠進我私人羣一塊兒交流學習!
void ioc() package com.chaoyue.servlet; 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.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.chaoyue.annotation.Controller; import com.chaoyue.annotation.Quatifier; import com.chaoyue.annotation.RequestMapping; import com.chaoyue.annotation.Service; import com.chaoyue.controller.SpringmvcController; @WebServlet("/DispatcherServlet") 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.chaoyue"); 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(); } } } } /** * 掃描包下的全部文件 * * @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.doGet(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(); } } }
package com.chaoyue.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.chaoyue.annotation.Controller; import com.chaoyue.annotation.Quatifier; import com.chaoyue.annotation.RequestMapping; import com.chaoyue.service.impl.MyService; import com.chaoyue.service.impl.SpringmvcServiceImpl; @Controller("chaoyue") public class SpringmvcController { @Quatifier("MyServiceImpl") MyService myService; @Quatifier("SpringmvcServiceImpl") SpringmvcServiceImpl 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; } }
package com.chaoyue.service.impl; import java.util.Map; public interface MyService { int insert(Map map); int delete(Map map); int update(Map map); int select(Map map); } package com.chaoyue.service.impl; import java.util.Map; import com.chaoyue.annotation.Service; @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; } }
package com.chaoyue.service.impl; import java.util.Map; public interface SpringmvcService { int insert(Map map); int delete(Map map); int update(Map map); int select(Map map); } package com.chaoyue.service.impl; import java.util.Map; 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; } }
歡迎留言討論,帶着謙虛的心一塊兒學習!也能夠進我私人羣一塊兒交流學習
歡迎加入QQ羣架構華山論劍:836442475【點擊進入】(大牛彙集地)一塊兒交流學習探討!
咱們提供免費的架構資料 以及免費的解答 不懂得問題均可以來問咱們老師,以後還會有職業生涯規劃,以及面試指導 咱們天天晚上八點也有公開課免費學習: 10年架構師分享經驗,Dubbo、Redis、Netty、zookeeper、Spring cloud、分佈式、高併發等架構技術