SpringMVC请求分发的简单实现
简介
- 将处理请求的类在系统启动的时候加载起来,相当于SpringMVC中的Controller
- 读取Controller中的配置并对应其处理的URL
- 通过调度Servlet进行拦截请求,并找到相应的Controller进行处理
主要代码
package com.meet58.base.servlet.annotation; public @interface ServletHandler { }
这里注解主要是声明这个类是一个ServletHandler类,用于处理请求的类,系统启动的时候就会加载这些类。
package com.meet58.base.servlet.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; import com.meet58.base.servlet.types.RequestMethod;
import com.meet58.base.servlet.types.ResponseType; @Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface HandlerMapping {
String value(); }
这个注解是配置处理请求的注解,定义了要处理的路径。
package com.meet58.base.servlet.mapping; import java.io.IOException; import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.util.ClassUtils; import com.meet58.base.servlet.annotation.ServletHandler; public class ServletHandlerMappingResolver { private static final String RESOURCE_PATTERN = "/**/*.class"; private String[] packagesToScan; private ResourcePatternResolver resourcePatternResolver; private static final TypeFilter[] ENTITY_TYPE_FILTERS = new TypeFilter[] {
new AnnotationTypeFilter(ServletHandler.class, false)}; public ServletHandlerMappingResolver(){
this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(new PathMatchingResourcePatternResolver());
} public ServletHandlerMappingResolver scanPackages(String[] packagesToScan){
try {
for (String pkg : packagesToScan) {
String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
ClassUtils.convertClassNameToResourcePath(pkg) + RESOURCE_PATTERN;
Resource[] resources; resources = this.resourcePatternResolver.getResources(pattern); MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(this.resourcePatternResolver);
for (Resource resource : resources) {
if (resource.isReadable()) {
MetadataReader reader = readerFactory.getMetadataReader(resource);
String className = reader.getClassMetadata().getClassName();
if (matchesFilter(reader, readerFactory)) {
ServletHandlerMappingFactory.addClassMapping(this.resourcePatternResolver.getClassLoader().loadClass(className));
}
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return this;
} public String[] getPackagesToScan() {
return packagesToScan;
} public void setPackagesToScan(String[] packagesToScan) {
this.packagesToScan = packagesToScan;
this.scanPackages(packagesToScan);
} private boolean matchesFilter(MetadataReader reader, MetadataReaderFactory readerFactory) throws IOException {
for (TypeFilter filter : ENTITY_TYPE_FILTERS) {
if (filter.match(reader, readerFactory)) {
return true;
}
}
return false;
} }
这段代码是Spring中如何扫描Hibernate持久化对象的代码,拿过来借鉴了一下,下面要处理的就是把要处理的URL和相对应的ServletHandler进行匹配:
package com.meet58.base.servlet.mapping; import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map; import org.apache.log4j.Logger; import com.meet58.base.servlet.annotation.HandlerMapping;
import com.meet58.base.servlet.context.ServletHandlerFactory; public class ServletHandlerMappingFactory { private static Logger logger = Logger.getLogger(ServletHandlerMappingFactory.class); private static Map<String, Method> servletHandlerMapping = new HashMap<String, Method>(); public static void addClassMapping(Class<?> clazz) {
String url = null;
HandlerMapping handlerMapping = clazz.getAnnotation(HandlerMapping.class);
if (handlerMapping != null) {
url = handlerMapping.value();
} else {
String classSimpleName = clazz.getSimpleName().toLowerCase();
url = "/" + classSimpleName.substring(0,
classSimpleName.indexOf("servlet"));
}
if (url != null) {
if(url.endsWith("/")){
url = url.substring(url.length() - 1);
}
ServletHandlerFactory.put(clazz);
logger.info(" Load servlet handler class:" + clazz.getName() + " url:" + url);
scanHandlerMethod(clazz,url);
}
} public static void scanHandlerMethod(Class<?> clazz,String classMapping) {
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
HandlerMapping handlerMapping = method.getAnnotation(HandlerMapping.class);
if (handlerMapping != null && handlerMapping.value() != null) {
String mapping = handlerMapping.value();
if(!mapping.startsWith("/")){
mapping = "/" + mapping;
}
mapping = classMapping + mapping;
addMethodMapping( mapping,method);
}
}
} public static void addMethodMapping(String url,Method method) {
logger.info(" Load servlet handler mapping, method:" + method.getName() + " for url:" + url);
Method handlerMethod = servletHandlerMapping.get(url);
if(handlerMethod != null){
throw new IllegalArgumentException(" url :" + url + " is already mapped by :" + handlerMethod);
}else{
servletHandlerMapping.put(url, method);
}
} public static Method getMethodMapping(String url) {
return servletHandlerMapping.get(url);
}
}
在这个类中扫描了每个ServletHandler类中的方法,并记录他们的要处理的URL,接下来就是通过容器实例化这些ServletHandler类了:
package com.meet58.base.servlet.context; import java.util.HashMap;
import java.util.Map; import org.apache.log4j.Logger; public class ServletHandlerFactory { private static Logger logger = Logger.getLogger(ServletHandlerFactory.class); private static Map<String,Object> classes = new HashMap<String,Object>(); public static void put(Class<?> clazz){
try {
logger.info("初始化ServletHandler类:"+ clazz.getName());
Object servlet = clazz.newInstance();
classes.put(clazz.getName(), servlet);
} catch (InstantiationException e) {
logger.error("初始化Servlet类:" + clazz.getName() + "失败:" + e.getMessage());
} catch (IllegalAccessException e) {
logger.error("初始化Servlet类:" + clazz.getName() + "失败:" + e.getMessage());
}
} @SuppressWarnings("unchecked")
public static <T> T get(String className){
return (T)classes.get(className);
}
}
在ServletHandler类处理完成,并知道他们分别处理哪些URL之后,就可以通过一个调度器进行对对应的URL进行请求的分发了:
package com.meet58.base.servlet; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List; 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 org.apache.log4j.Logger; import com.meet58.base.context.WebHttpRequestContext;
import com.meet58.base.servlet.context.ServletHandlerFactory;
import com.meet58.base.servlet.mapping.ServletHandlerMappingFactory;
import com.meet58.util.WebUtils; @WebServlet(urlPatterns = { "*.do" })
public class WebHttpDispatchServlet extends HttpServlet { private static final long serialVersionUID = 1L; private Logger logger = Logger.getLogger(this.getClass()); private List<String> excludeUrls = new ArrayList<String>(); @Override
public void init() throws ServletException {
// 屏蔽websocket地址
excludeUrls.add("/meet.do");
super.init();
} public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String url = request.getRequestURI().replace(
request.getContextPath(), "");
if (excludeUrls.contains(url)) {
return;
}
Method handlerMethod = ServletHandlerMappingFactory.getMethodMapping(url);
if (handlerMethod == null) {
response.sendError(404, "No handler found for " + url);
logger.error("No handler found for " + url);
return;
} Object servlet = ServletHandlerFactory.get(handlerMethod
.getDeclaringClass().getName()); if (servlet == null) {
response.sendError(404, "No handler class found for " + url);
logger.error("No handler class found for " + url);
return;
} Object result = invokeHandlerMethod(servlet, handlerMethod);
handleInvokeResult(result); // this.doService();
} catch (Throwable e) {
handlerException(e);
}
} public void handleInvokeResult(Object result) {
String location = "";
if (result instanceof String) {
if (((String) result).startsWith("redirect:")) {
location = ((String) result).substring("redirect:".length(),
((String) result).length());
WebUtils.redirect(location);
} else if (((String) result).startsWith("forward:")) {
location = ((String) result).substring("forward:".length(),
((String) result).length());
WebUtils.forward(location);
}
}
} public Object invokeHandlerMethod(Object object, Method method)
throws Throwable {
Object result = null;
if (method != null) {
try {
result = method.invoke(object);
} catch (InvocationTargetException e) {
throw e.getTargetException();
}
}
return result;
} public void handlerException(Throwable e) {
String message = e.getMessage() != null ? e.getMessage() : e.toString();
e.printStackTrace();
if (WebHttpRequestContext.isAsyncRequest()) {
WebUtils.writeFailure(message);
} else {
try {
WebHttpRequestContext.getResponse().sendError(500, message);
} catch (IOException e1) {
e1.printStackTrace();
}
}
} public String getMappingClass(String url) {
return null;
} }
这段代码中就是通过URL找到对应的处理方法来进行处理,并且捕获异常。
SpringMVC请求分发的简单实现的更多相关文章
- SpringMVC源码学习:容器初始化+MVC初始化+请求分发处理+参数解析+返回值解析+视图解析
目录 一.前言 二.初始化 1. 容器初始化 根容器查找的方法 容器创建的方法 加载配置文件信息 2. MVC的初始化 文件上传解析器 区域信息解析器 handler映射信息解析 3. Handler ...
- 2.5万字长文简单总结SpringMVC请求参数接收
这是公众号<Throwable文摘>发布的第22篇原创文章,暂时收录于专辑<架构与实战>.暂定下一篇发布的长文是<图文分析JUC同步器框架>,下一篇发布的短文是&l ...
- SpringMVC核心分发器DispatcherServlet分析[附带源码分析]
目录 前言 DispatcherServlet初始化过程 DispatcherServlet处理请求过程 总结 参考资料 前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不 ...
- SpringMVC核心分发器DispatcherServlet分析
本文将分析SpringMVC的核心分发器DispatcherServlet的初始化过程以及处理请求的过程,让读者了解这个入口Servlet的作用. DispatcherServlet初始化过程 在分析 ...
- java面试记录二:spring加载流程、springmvc请求流程、spring事务失效、synchronized和volatile、JMM和JVM模型、二分查找的实现、垃圾收集器、控制台顺序打印ABC的三种线程实现
注:部分答案引用网络文章 简答题 1.Spring项目启动后的加载流程 (1)使用spring框架的web项目,在tomcat下,是根据web.xml来启动的.web.xml中负责配置启动spring ...
- SpringMVC请求流程源码分析
一.SpringMVC使用 1.工程创建 创建maven工程. 添加java.resources目录. 引入Spring-webmvc 依赖. <dependency> <group ...
- Spring框架系列(5) - 深入浅出SpringMVC请求流程和案例
前文我们介绍了Spring框架和Spring框架中最为重要的两个技术点(IOC和AOP),那我们如何更好的构建上层的应用呢(比如web 应用),这便是SpringMVC:Spring MVC是Spri ...
- jQuery源码分析系列(33) : AJAX中的前置过滤器和请求分发器
jQuery1.5以后,AJAX模块提供了三个新的方法用于管理.扩展AJAX请求,分别是: 1.前置过滤器 jQuery. ajaxPrefilter 2.请求分发器 jQuery. ajaxTran ...
- 详解SpringMVC请求的时候是如何找到正确的Controller[附带源码分析]
目录 前言 源码分析 重要接口介绍 SpringMVC初始化的时候做了什么 HandlerExecutionChain的获取 实例 资源文件映射 总结 参考资料 前言 SpringMVC是目前主流的W ...
随机推荐
- 李洪强漫谈iOS开发[C语言-021]-运算符
- CC_UNUSED_PARAM 宏含义的解释
#define CC_UNUSED_PARAM(unusedparam) (void)unusedparam 这个宏完全没有执行任何命令,这样写的原因主要是历史遗留原因,ojb-c不存在纯虚函数并且传 ...
- org.dom4j.DocumentException unknown protocol h
待解析文件的路径中有空格,把空格去掉就好了
- comm命令——
comm命令 :对已经有序的文件进行比较——第一列只在文件1中出现的文件,第二列只在文件2中出现的文件,第三列在文件1和文件2中同事出现的文件 请注意前提条件: comm对文件 ...
- servlet向ajax传递list数据类型,ajax向servlet传递array数据类型
因工作需要, 1,后台向前台传递一个list 2,前台向后台传递类似于list的结构,但是因为javascript不支持list类型,所以只能使用二维数组代替 后台运行后的截图: ...
- BZOJ_2194_快速傅立叶之二_(FFT+卷积)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=2194 给出序列\(a[0],a[1],...,a[n-1]\)和\(b[0],b[1],... ...
- Extend ComboGrid Editors for DataGrid Of JQuery EasyUI
在JQueryEasyUI中为DataGrid自定义了一个ComboGrid编辑器.具体方法: 自己写一个扩展 $.extend($.fn.datagrid.defaults.editors, { c ...
- MsoShapeType /InlineShape枚举
指定形状的类型或形状范围. 名称 值 说明 msoAutoShape 1 自选图形. msoCallout 2 标注. msoCanvas 20 画布. msoChart 3 图. msoCommen ...
- C# 隐藏和覆盖
1.在C#里面,只有声明vitual.abstract和带override的方法能够被子类重写(override). 2.如果父类方法没有以上关键字,子类又有和父类一样的方法,那么,就默认隐藏父类方法 ...
- SSE 标准化向量
mov esi, this ; vector u movups xmm0, [esi] ; first vector in xmm0 movaps xmm2, xmm0 ; copy original ...