方法handler是怎么加到springmvc中去的呢
(处理器映射器)
public class DispatcherServlet extends FrameworkServlet {
protected void initStrategies(ApplicationContext context) {
initMultipartResolver(context);
initLocaleResolver(context);
initThemeResolver(context);
initHandlerMappings(context); // 这个方法
initHandlerAdapters(context);
initHandlerExceptionResolvers(context);
initRequestToViewNameTranslator(context);
initViewResolvers(context);
initFlashMapManager(context);
}
private void initHandlerMappings(ApplicationContext context) {
this.handlerMappings = null;
if (this.detectAllHandlerMappings) {
// Find all HandlerMappings in the ApplicationContext, including ancestor contexts.
Map<String, HandlerMapping> matchingBeans =
BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerMapping.class, true, false);// 然后这里
if (!matchingBeans.isEmpty()) {
this.handlerMappings = new ArrayList<>(matchingBeans.values());
// We keep HandlerMappings in sorted order.
AnnotationAwareOrderComparator.sort(this.handlerMappings);
}
}
else {
try {
HandlerMapping hm = context.getBean(HANDLER_MAPPING_BEAN_NAME, HandlerMapping.class);
this.handlerMappings = Collections.singletonList(hm);
}
catch (NoSuchBeanDefinitionException ex) {
// Ignore, we'll add a default HandlerMapping later.
}
}
// Ensure we have at least one HandlerMapping, by registering
// a default HandlerMapping if no other mappings are found.
if (this.handlerMappings == null) {
this.handlerMappings = getDefaultStrategies(context, HandlerMapping.class);
if (logger.isTraceEnabled()) {
logger.trace("No HandlerMappings declared for servlet '" + getServletName() +
"': using default strategies from DispatcherServlet.properties");
}
}
}
Map<String, HandlerMapping> matchingBeans
可以看出路径与方法的映射被AbstractHandlerMethodMapping在启动的时候都注册到map当中,当使用的时候直接利用反射来创建
处理器适配器
DispServet
视图解析器
url视图 InterenalResourceView/jstlView
文档类视图
Json视图 JSONVIEW
xml视图 xMLVIEW
请求参数封装的实现原理
通过各种参数解析器来完成
@PathVariable 最终将url中传递的参数映射器中的对应的占位符的名称作为map的key,将url中实际传过来的值作为vaule
HandlerMethodArgumentResolver
class RequestParamMethodArgumentResolver extends AbstractNamedValueMethodArgumentResolver
if (arg == null) {
String[] paramValues = request.getParameterValues(name);
if (paramValues != null) {
arg = (paramValues.length == 1 ? paramValues[0] : paramValues);
}
}
springmvc中的拦截器aop思想
public void addInterceptor(HandlerInterceptor interceptor) {
initInterceptorList().add(interceptor);
}
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
if (!mappedHandler.**applyPreHandle**(processedRequest, response)) {
return;
}
// Actually invoke the handler.
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
if (asyncManager.isConcurrentHandlingStarted()) {
return;
}
applyDefaultViewName(processedRequest, mv);
mappedHandler.applyPostHandle(processedRequest, response, mv);
}
public class HandlerExecutionChain {
boolean **applyPreHandle**(HttpServletRequest request, HttpServletResponse response) throws Exception {
HandlerInterceptor[] interceptors = getInterceptors();
if (!ObjectUtils.isEmpty(interceptors)) {
for (int i = 0; i < interceptors.length; i++) {
HandlerInterceptor interceptor = interceptors[i];
if (!interceptor.preHandle(request, response, this.handler)) {
triggerAfterCompletion(request, response, null);
return false;
}
this.interceptorIndex = i;
}
}
return true;
}
}