写在前面本文看下如何定义MVC部分内容。1正文首先定义requestmapping注解映射url-处理方法Target(value{ElementType.METHOD})Retention(RetentionPolicy.RUNTIME)publicinterfaceRequestMapping{Stringvalue()default;}简单期间这里暂时只支持用在方法上spring mvc是支持同时用在类和方法上的知道即可。接着我们还要定义DispatcherServlet作为拦截web请求的入口(只保留核心代码)// com.hc.minispring.web.v2.DispatcherServletpublicclassDispatcherServletextendsHttpServlet{Overridepublicvoidinit(ServletConfigconfig)throwsServletException{super.init(config);sContextConfigLocationconfig.getInitParameter(contextConfigLocation);URLxmlPathnull;try{xmlPaththis.getServletContext().getResource(sContextConfigLocation);}catch(MalformedURLExceptione){e.printStackTrace();}// 获取所有要扫描的包this.packageNamesXmlScanComponentHelper.getNodeValue(xmlPath);// 刷新beanRefresh();}protectedvoidRefresh(){// 扫描包获取所有的controller 实例initController();// 初始化url和对应method的映射initMapping();}protectedvoidinitController(){this.controllerNamesscanPackages(this.packageNames);for(StringcontrollerName:this.controllerNames){Objectobjnull;Class?clznull;try{clzClass.forName(controllerName);this.controllerClasses.put(controllerName,clz);}catch(ClassNotFoundExceptione){e.printStackTrace();}try{objclz.newInstance();this.controllerObjs.put(controllerName,obj);}catch(InstantiationExceptione){e.printStackTrace();}catch(IllegalAccessExceptione){e.printStackTrace();}}}protectedvoidinitMapping(){for(StringcontrollerName:this.controllerNames){// ...if(methods!null){for(Methodmethod:methods){// 判断方法上是否使用了RequestMapping注解使用了则获取映射的url以及对应的方法存储备用booleanisRequestMappingmethod.isAnnotationPresent(RequestMapping.class);if(isRequestMapping){StringmethodNamemethod.getName();Stringurlmappingmethod.getAnnotation(RequestMapping.class).value();// ...// 存储请求web路径和处理该web路径的方法将会在doGet方法中使用this.mappingMethods.put(urlmapping,method);}}}}}protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{StringsPathrequest.getServletPath();// ...try{// 根据请求路径获取处理方法反射执行并返回结果Methodmethodthis.mappingMethods.get(sPath);objthis.mappingObjs.get(sPath);objResultmethod.invoke(obj);}catch(SecurityExceptione){// ...}response.getWriter().append(objResult.toString());}}配置component-scan的配置文件如下?xml version1.0 encodingUTF-8 ?componentscomponent-scanbase-packagecom.hc.minispring.web.v2.test//components在com.hc.minispring.web.v2.test包中我配置了两个类publicclassHelloWorldBean{RequestMapping(/test)publicStringdoTest(){returnhello world for doGet!;}}publicclassHelloWorldBean1{RequestMapping(/test1)publicStringdoTest(){return1hello world for doGet!;}}最后配置下web.xml就行了?xml version1.0 encodingUTF-8?web-appxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:webhttp://xmlns.jcp.org/xml/ns/javaeexsi:schemaLocationhttp://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsdidWebApp_IDservletservlet-nameminisMVC/servlet-nameservlet-classcom.hc.minispring.web.v2.DispatcherServlet/servlet-classinit-paramparam-namecontextConfigLocation/param-nameparam-value/WEB-INF/v2/minisMVC-servletv2.xml/param-value/init-paramload-on-startup1/load-on-startup/servletservlet-mappingservlet-nameminisMVC/servlet-nameurl-pattern//url-pattern/servlet-mapping/web-app启动后访问C:\Users\dell9020curl http://localhost:8080/minispring_Web_exploded/test hello world for doGet! C:\Users\dell9020curl http://localhost:8080/minispring_Web_exploded/test1 1hello world for doGet! C:\Users\dell9020这样一个简单的mini springmvc就完成了。这样应用程序开发人员就不需要关心servlet啥的了只需要提供一个类似于/WEB-INF/v2/minisMVC-servletv2.xml的配置文件然后在自定义的component scan路径下写对应的业务代码就可以了。写在后面参考文章列表手把手带你写一个 MiniSpring 。