前言
上节课我们学习了 Spring MVC 框架的使用,为了更好地理解这个框架,本节课我们来仿写一个 Spring MVC 框架,用到的技术比较简单,只需要 XML 解析+反射就可以完成,不需要 JDK 动态代理。
自己手写框架的前提是必须理解框架的底层原理和运行机制,所以我们还是先来回顾一下 Spring MVC 的实现原理。
Spring MVC 实现原理spring mvc
核心组件spring mvc
-
DispatcherServlet:前端控制器,负责调度其他组件的执行,可降低不同组件之间的耦合性,是整个 Spring MVC 的核心模块。
-
Handler:处理器,完成具体业务逻辑,相当于 Servlet 或 Action。
-
HandlerMapping:DispatcherServlet 是通过 HandlerMapping 将请求映射到不同的 Handler。
-
HandlerInterceptor:处理器拦截器,是一个接口,如果我们需要做一些拦截处理,可以来实现这个接口。spring mvc
-
HandlerExecutionChain:处理器执行链,包括两部分内容:Handler 和 HandlerInterceptor(系统会有一个默认的 HandlerInterceptor,如果需要额外拦截处理,可以添加拦截器设置)。spring mvc
-
HandlerAdapter:处理器适配器,Handler 执行业务方法之前,需要进行一系列的操作包括表单数据的验证,数据类型的转换,将表单数据封装到 POJO 等等,这一系列的操作,都是由 HandlerAdapter 来完成,DispatcherServlet 通过 HandlerAdapter 执行不同的 Handler。spring mvc
-
ModelAndView:装载了模型数据和视图信息,作为 Handler 的处理结果,返回给 DispatcherServlet。spring mvc
-
ViewResolver:视图解析器,DispatcherServlet 通过它将逻辑视图解析成物理视图,最终将渲染结果响应给客户端。spring mvc
扫描二维码关注公众号,回复: 9262751 查看本文章
以上就是 Spring MVC 的核心组件。那么这些组件之间是如何进行交互的呢?
工作流程spring mvc
-
客户端请求被 DispatcherServlet(前端控制器)接收。
-
根据 Handler Mapping映射到 Handler。
-
生成 Handler 和 HandlerInterceptor(如果有则生成)。
-
Handler 和 HandlerInterceptor 以 HandlerExecutionChain 的形式一并返回给 DispatcherServlet。spring mvc
-
DispatcherServlet 通过 HandlerAdapter 调用 Handler 的方法做业务逻辑处理。
-
返回一个 ModelAndView 对象给 DispatcherServlet。spring mvc
-
DispatcherServlet 将获取的 ModelAndView 对象传给 ViewResolver 视图解析器,将逻辑视图解析成物理视图 View。
-
ViewResolver 返回一个 View 给 DispatcherServlet。spring mvc
-
DispatcherServlet 根据 View 进行视图渲染(将模型数据填充到视图中)。
-
DispatcherServlet 将渲染后的视图响应给客户端。spring mvc
通过以上的分析,大致可以将 Spring MVC 流程理解如下:
首先需要一个前置控制器 DispatcherServlet,作为整个流程的核心,由它去调用其他组件,共同完成业务。
主要组件有两个:spring mvc
一是 Controller,调用其业务方法 Method,执行业务逻辑。
二是 ViewResolver 视图解析器,将业务方法的返回值解析为物理视图+模型数据,返回客户端。spring mvc
我们自己写框架就按照这个思路来。spring mvc
初始化工作spring mvc
- 根据 Spring IoC 容器的特性,需要将参与业务的对象全部创建并保存到容器中,供流程调用。首先需要创建 Controller 对象,HTTP 请求是通过注解找到对应的 Controller 对象,所以我们需要将所有的 Controller 与其注解建立关联,很显然,使用 key-value 结构的 Map 集合来保存最合适不过了,这样就模拟了 IoC 容器。
- Controller 的 Method 也是通过注解与 HTTP 请求映射的,同样的,我们需要将所有的 Method 与其注解建立关联, HTTP 直接通过注解的值找到对应的 Method,这里也用 Map 集合保存。
- 实例化视图解析器。spring mvc
初始化工作完成,接下来处理 HTTP 请求,业务流程如下:
-
DispatcherServlet 接收请求,通过映射从 IoC 容器中获取对应的 Controller 对象。
-
根据映射获取 Controller 对象对应的 Method。
-
调用 Method,获取返回值。spring mvc
-
将返回值传给视图解析器,返回物理视图。spring mvc
-
完成页面跳转。spring mvc
思路捋清楚了,接下来开始写代码,我们需要创建下面这四个类:
-
MyDispatcherServlet:模拟 DispatcherServlet。
-
MyController:模拟 Controller 注解。spring mvc
-
MyRequestMapping:模拟 RequestMapping 注解。
-
MyViewResolver:模拟 ViewResolver 视图解析器。spring mvc
首先创建 MyDispatcherServlet,init 方法完成初始化:spring mvc
1、将 Controller 与注解进行关联,保存到 iocContainer 中,哪些 Controller 是需要添加到 iocContainer 中的?
必须同时满足两点:spring mvc
- springmvc.xml 中配置扫描的类。spring mvc
- 类定义处添加了注解。spring mvc
注意这两点必须同时满足。spring mvc
代码思路:spring mvc
- 解析 springmvc.xml。
- 获取 component-scan 标签配置的包下的所有类。
- 判断若这些类添加了 @MyController 注解,则创建实例对象,并且保存到 iocContainer。
- @MyRequestMapping 的值为键,Controller 对象为值。
2、将 Controller 中的 Method 与注解进行关联,保存到 handlerMapping 中。
代码思路:spring mvc
- 遍历 iocContainer 中的 Controller 实例对象。
- 遍历每一个 Controlle r对象的 Method。
- 判断 Method 是否添加了 @MyRequestMapping 注解,若添加,则进行映射并保存。
- 保存到 handlerMapping 中,@MyRequestMapping 的值为键,Method 为值。
3、实例化 ViewResolver。spring mvc
代码思路:spring mvc
- 解析 springmvc.xml。
- 根据 bean 标签的 class 属性获取需要实例化的 MyViewResolver。
- 通过反射创建实例化对象,同时获取 prefix 和 suffix 属性,以及 setter 方法。
- 通过反射调用 setter 方法给属性赋值,完成 MyViewResolver 的实例化。
doPost 方法处理 HTTP 请求的流程:spring mvc
1、解析 HTTP,分别得到 Controller 和 Method 对应的 uri。
2、通过 uri 分别在 iocContainer 和 handlerMapping 中获取对应的 Controller 以及 Method。
3、通过反射调用 Method,执行业务方法,获取结果。
4、将结果传给 MyViewResolver 进行解析,返回真正的物理视图(JSP 页面)。
5、完成 JSP 页面跳转。spring mvc
代码实现:spring mvc
1、创建 MyController 注解,作用目标为类。
/**
* 自定义Controller注解
* @author southwind
*
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyController {
String value() default "";
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
2、创建 MyRequestMapping 注解,作用目标为类和方法。
/**
* 自定义RequestMapping注解
* @author southwind
*
*/
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyRequestMapping {
String value() default "";
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
3、创建 MyDispatcherServlet,核心控制器,init 完成初始化工作,doPost 处理 HTTP 请求。
/** * DispatcherServlet * @author southwind * */ public class MyDispatcherServlet extends HttpServlet{
<span class="token comment">//模拟IOC容器,保存Controller实例对象</span> <span class="token keyword">private</span> Map<span class="token generics function"><span class="token punctuation"><</span>String<span class="token punctuation">,</span>Object<span class="token punctuation">></span></span> iocContainer <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics function"><span class="token punctuation"><</span>String<span class="token punctuation">,</span>Object<span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//保存handler映射</span> <span class="token keyword">private</span> Map<span class="token generics function"><span class="token punctuation"><</span>String<span class="token punctuation">,</span>Method<span class="token punctuation">></span></span> handlerMapping <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics function"><span class="token punctuation"><</span>String<span class="token punctuation">,</span>Method<span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//自定视图解析器</span> <span class="token keyword">private</span> MyViewResolver myViewResolver<span class="token punctuation">;</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">init</span><span class="token punctuation">(</span>ServletConfig config<span class="token punctuation">)</span> <span class="token keyword">throws</span> ServletException <span class="token punctuation">{</span> <span class="token comment">// TODO Auto-generated method stub</span> <span class="token comment">//扫描Controller,创建实例对象,并存入iocContainer</span> <span class="token function">scanController</span><span class="token punctuation">(</span>config<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//初始化handler映射</span> <span class="token function">initHandlerMapping</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//加载视图解析器</span> <span class="token function">loadViewResolver</span><span class="token punctuation">(</span>config<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">/** * 扫描Controller * @param config */</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">scanController</span><span class="token punctuation">(</span>ServletConfig config<span class="token punctuation">)</span><span class="token punctuation">{</span> SAXReader reader <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SAXReader</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> <span class="token comment">//解析springmvc.xml</span> String path <span class="token operator">=</span> config<span class="token punctuation">.</span><span class="token function">getServletContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getRealPath</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">"\\WEB-INF\\classes\\"</span><span class="token operator">+</span>config<span class="token punctuation">.</span><span class="token function">getInitParameter</span><span class="token punctuation">(</span><span class="token string">"contextConfigLocation"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Document document <span class="token operator">=</span> reader<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>path<span class="token punctuation">)</span><span class="token punctuation">;</span> Element root <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getRootElement</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Iterator iter <span class="token operator">=</span> root<span class="token punctuation">.</span><span class="token function">elementIterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span>iter<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> Element ele <span class="token operator">=</span> <span class="token punctuation">(</span>Element<span class="token punctuation">)</span> iter<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>ele<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">"component-scan"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> String packageName <span class="token operator">=</span> ele<span class="token punctuation">.</span><span class="token function">attributeValue</span><span class="token punctuation">(</span><span class="token string">"base-package"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//获取base-package包下的所有类名</span> List<span class="token generics function"><span class="token punctuation"><</span>String<span class="token punctuation">></span></span> list <span class="token operator">=</span> <span class="token function">getClassNames</span><span class="token punctuation">(</span>packageName<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span>String str<span class="token operator">:</span>list<span class="token punctuation">)</span><span class="token punctuation">{</span> Class <span class="token class-name">clazz</span> <span class="token operator">=</span> Class<span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//判断是否有MyController注解</span> <span class="token keyword">if</span><span class="token punctuation">(</span>clazz<span class="token punctuation">.</span><span class="token function">isAnnotationPresent</span><span class="token punctuation">(</span>MyController<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment">//获取Controller中MyRequestMapping注解的value</span> MyRequestMapping annotation <span class="token operator">=</span> <span class="token punctuation">(</span>MyRequestMapping<span class="token punctuation">)</span> clazz<span class="token punctuation">.</span><span class="token function">getAnnotation</span><span class="token punctuation">(</span>MyRequestMapping<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String value <span class="token operator">=</span> annotation<span class="token punctuation">.</span><span class="token function">value</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//Controller实例对象存入iocContainer</span> iocContainer<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>value<span class="token punctuation">,</span> clazz<span class="token punctuation">.</span><span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// TODO Auto-generated catch block</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment">/** * 获取包下的所有类名 * @param packageName * @return */</span> <span class="token keyword">public</span> List<span class="token generics function"><span class="token punctuation"><</span>String<span class="token punctuation">></span></span> <span class="token function">getClassNames</span><span class="token punctuation">(</span>String packageName<span class="token punctuation">)</span><span class="token punctuation">{</span> List<span class="token generics function"><span class="token punctuation"><</span>String<span class="token punctuation">></span></span> classNameList <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics function"><span class="token punctuation"><</span>String<span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String packagePath <span class="token operator">=</span> packageName<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">"."</span><span class="token punctuation">,</span> <span class="token string">"/"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ClassLoader loader <span class="token operator">=</span> Thread<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getContextClassLoader</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> URL url <span class="token operator">=</span> loader<span class="token punctuation">.</span><span class="token function">getResource</span><span class="token punctuation">(</span>packagePath<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>url <span class="token operator">!=</span> null<span class="token punctuation">)</span><span class="token punctuation">{</span> File file <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>url<span class="token punctuation">.</span><span class="token function">getPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> File<span class="token punctuation">[</span><span class="token punctuation">]</span> childFiles <span class="token operator">=</span> file<span class="token punctuation">.</span><span class="token function">listFiles</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span>File childFile <span class="token operator">:</span> childFiles<span class="token punctuation">)</span><span class="token punctuation">{</span> String className <span class="token operator">=</span> packageName<span class="token operator">+</span><span class="token string">"."</span><span class="token operator">+</span>childFile<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">".class"</span><span class="token punctuation">,</span> <span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span> classNameList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>className<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> classNameList<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">/** * 初始化handler映射 */</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">initHandlerMapping</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">for</span><span class="token punctuation">(</span>String str<span class="token operator">:</span>iocContainer<span class="token punctuation">.</span><span class="token function">keySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> Class <span class="token class-name">clazz</span> <span class="token operator">=</span> iocContainer<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Method<span class="token punctuation">[</span><span class="token punctuation">]</span> methods <span class="token operator">=</span> clazz<span class="token punctuation">.</span><span class="token function">getMethods</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>Method method <span class="token operator">:</span> methods<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">//判断方式是否添加MyRequestMapping注解</span> <span class="token keyword">if</span><span class="token punctuation">(</span>method<span class="token punctuation">.</span><span class="token function">isAnnotationPresent</span><span class="token punctuation">(</span>MyRequestMapping<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment">//获取Method中MyRequestMapping注解的value</span> MyRequestMapping annotation <span class="token operator">=</span> method<span class="token punctuation">.</span><span class="token function">getAnnotation</span><span class="token punctuation">(</span>MyRequestMapping<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String value <span class="token operator">=</span> annotation<span class="token punctuation">.</span><span class="token function">value</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//method存入methodMapping</span> handlerMapping<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>value<span class="token punctuation">,</span> method<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment">/** * 加载自定义视图解析器 * @param config */</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">loadViewResolver</span><span class="token punctuation">(</span>ServletConfig config<span class="token punctuation">)</span><span class="token punctuation">{</span> SAXReader reader <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SAXReader</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> <span class="token comment">//解析springmvc.xml</span> String path <span class="token operator">=</span> config<span class="token punctuation">.</span><span class="token function">getServletContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getRealPath</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">"\\WEB-INF\\classes\\"</span><span class="token operator">+</span>config<span class="token punctuation">.</span><span class="token function">getInitParameter</span><span class="token punctuation">(</span><span class="token string">"contextConfigLocation"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Document document <span class="token operator">=</span> reader<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>path<span class="token punctuation">)</span><span class="token punctuation">;</span> Element root <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getRootElement</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Iterator iter <span class="token operator">=</span> root<span class="token punctuation">.</span><span class="token function">elementIterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span>iter<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> Element ele <span class="token operator">=</span> <span class="token punctuation">(</span>Element<span class="token punctuation">)</span> iter<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>ele<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">"bean"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> String className <span class="token operator">=</span> ele<span class="token punctuation">.</span><span class="token function">attributeValue</span><span class="token punctuation">(</span><span class="token string">"class"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Class <span class="token class-name">clazz</span> <span class="token operator">=</span> Class<span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span>className<span class="token punctuation">)</span><span class="token punctuation">;</span> Object obj <span class="token operator">=</span> clazz<span class="token punctuation">.</span><span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//获取setter方法</span> Method prefixMethod <span class="token operator">=</span> clazz<span class="token punctuation">.</span><span class="token function">getMethod</span><span class="token punctuation">(</span><span class="token string">"setPrefix"</span><span class="token punctuation">,</span> String<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Method suffixMethod <span class="token operator">=</span> clazz<span class="token punctuation">.</span><span class="token function">getMethod</span><span class="token punctuation">(</span><span class="token string">"setSuffix"</span><span class="token punctuation">,</span> String<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Iterator beanIter <span class="token operator">=</span> ele<span class="token punctuation">.</span><span class="token function">elementIterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//获取property值</span> Map<span class="token generics function"><span class="token punctuation"><</span>String<span class="token punctuation">,</span>String<span class="token punctuation">></span></span> propertyMap <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics function"><span class="token punctuation"><</span>String<span class="token punctuation">,</span>String<span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span>beanIter<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> Element beanEle <span class="token operator">=</span> <span class="token punctuation">(</span>Element<span class="token punctuation">)</span> beanIter<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String name <span class="token operator">=</span> beanEle<span class="token punctuation">.</span><span class="token function">attributeValue</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String value <span class="token operator">=</span> beanEle<span class="token punctuation">.</span><span class="token function">attributeValue</span><span class="token punctuation">(</span><span class="token string">"value"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> propertyMap<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span> value<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">for</span><span class="token punctuation">(</span>String str<span class="token operator">:</span>propertyMap<span class="token punctuation">.</span><span class="token function">keySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment">//反射机制调用setter方法,完成赋值。</span> <span class="token keyword">if</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">"prefix"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> prefixMethod<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> propertyMap<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">"suffix"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> suffixMethod<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> propertyMap<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> myViewResolver <span class="token operator">=</span> <span class="token punctuation">(</span>MyViewResolver<span class="token punctuation">)</span> obj<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// TODO Auto-generated catch block</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">doGet</span><span class="token punctuation">(</span>HttpServletRequest req<span class="token punctuation">,</span> HttpServletResponse resp<span class="token punctuation">)</span> <span class="token keyword">throws</span> ServletException<span class="token punctuation">,</span> IOException <span class="token punctuation">{</span> <span class="token comment">// TODO Auto-generated method stub</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">doPost</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> resp<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">doPost</span><span class="token punctuation">(</span>HttpServletRequest req<span class="token punctuation">,</span> HttpServletResponse resp<span class="token punctuation">)</span> <span class="token keyword">throws</span> ServletException<span class="token punctuation">,</span> IOException <span class="token punctuation">{</span> <span class="token comment">// TODO Auto-generated method stub</span> <span class="token comment">//获取请求</span> String handlerUri <span class="token operator">=</span> req<span class="token punctuation">.</span><span class="token function">getRequestURI</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">"/"</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">//获取Controller实例</span> Object obj <span class="token operator">=</span> iocContainer<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>handlerUri<span class="token punctuation">)</span><span class="token punctuation">;</span> String methodUri <span class="token operator">=</span> req<span class="token punctuation">.</span><span class="token function">getRequestURI</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">"/"</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">//获取业务方法</span> Method method <span class="token operator">=</span> handlerMapping<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>methodUri<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> <span class="token comment">//反射机制调用业务方法</span> String value <span class="token operator">=</span> <span class="token punctuation">(</span>String<span class="token punctuation">)</span> method<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//视图解析器将逻辑视图转换为物理视图</span> String result <span class="token operator">=</span> myViewResolver<span class="token punctuation">.</span><span class="token function">jspMapping</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//页面跳转</span> req<span class="token punctuation">.</span><span class="token function">getRequestDispatcher</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forward</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> resp<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// TODO Auto-generated catch block</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
4、创建视图解析器 MyViewResolver。
/** * 自定义视图解析器 * @author southwind * */ public class MyViewResolver { private String prefix; private String suffix; public String getPrefix() { return prefix; } public void setPrefix(String prefix) { this.prefix = prefix; } public String getSuffix() { return suffix; } public void setSuffix(String suffix) { this.suffix = suffix; }
<span class="token keyword">public</span> String <span class="token function">jspMapping</span><span class="token punctuation">(</span>String value<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>prefix<span class="token operator">+</span>value<span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>suffix<span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
5、创建 TestController,处理业务请求。
@MyController
@MyRequestMapping(value = "/testController")
public class TestController {
@MyRequestMapping(value = "/test")
public String test(){
System.out.println("执行test相关业务");
return "index";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
6、测试。spring mvc
跳转 index.jsp,同时控制台打印业务日志,访问成功。spring mvc
总结spring mvc
本节课我们讲解了 Spring MVC 的底层原理,同时仿照 Spring MVC 手写了一个简单的框架,目的不是让大家自己去写框架,在实际开发中我们也不需要自己写框架,直接使用成熟的第三方框架即可。
手写框架的目的在于让大家更透彻地理解 Spring MVC 的底层流程,学习优秀框架的编程思想,理解了原理,才能更熟练地应用。
源码spring mvc
https://github.com/southwind9801/SpringMVCImitate.git
本场 Chat 内容来自达人课《案例上手 Spring 全家桶》,从 Spring 到 Spring Boot 再到 Spring Cloud,结合 3 个项目实战案例,真正做到学以致用。
-
Spring 技术零基础轻松入门;
-
68 讲更全面地覆盖 Spring 全家桶核心模块;
-
100+ 段代码示例,理解 Spring 全家桶要领;
-
3 大项目实战,掌握 Spring 全家桶实际应用;
-
精选 70 道 Spring 高频面试题检验学习成果;
-
免费赠送 16+ 小时的 Spring 实战视频;
-
进入专业的 Spring 技术交流社群;
</div>