{"id":1629,"date":"2017-07-10T16:47:09","date_gmt":"2017-07-10T08:47:09","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=1629"},"modified":"2017-07-10T16:47:09","modified_gmt":"2017-07-10T08:47:09","slug":"%e8%af%a6%e8%a7%a3spring-mvc%e4%b8%addispatcherservlet%e7%9a%84%e4%bd%9c%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=1629","title":{"rendered":"\u8be6\u89e3Spring MVC\u4e2dDispatcherServlet\u7684\u4f5c\u7528"},"content":{"rendered":"<p>\n\tSpring MVC\u7684\u6838\u5fc3\u5c31\u662f<a href=\"http:\/\/baike.baidu.com\/item\/DispatcherServlet\" name=\"DispatcherServlet\" target=\"_blank\" title=\"DispatcherServlet\">DispatcherServlet<\/a>\u3002\u56e0\u6b64\u6211\u4eec\u5f88\u6709\u5fc5\u8981\u7cfb\u7edf\u6027\u7684\u5bf9\u5b83\u8fdb\u884c\u4e00\u4e0b\u7814\u7a76\u3002\u672c\u6587\u5c06\u5e26\u9886\u4f60\u89e3\u8bfbDispatcherServlet\u7684\u4f5c\u7528\u3002\n<\/p>\n<p>\n\tDispatcherServlet\u662f\u524d\u7aef\u63a7\u5236\u5668\u8bbe\u8ba1\u6a21\u5f0f\u7684\u5b9e\u73b0\uff0c\u63d0\u4f9bSpring Web MVC\u7684\u96c6\u4e2d\u8bbf\u95ee\u70b9\uff0c\u800c\u4e14\u8d1f\u8d23\u804c\u8d23\u7684\u5206\u6d3e\uff0c\u800c\u4e14\u4e0eSpring IoC\u5bb9\u5668\u65e0\u7f1d\u96c6\u6210\uff0c\u4ece\u800c\u53ef\u4ee5\u83b7\u5f97Spring\u7684\u6240\u6709\u597d\u5904\u3002\n<\/p>\n<p>\n\tDispatcherServlet\u4e3b\u8981\u7528\u4f5c\u804c\u8d23\u8c03\u5ea6\u5de5\u4f5c\uff0c\u672c\u8eab\u4e3b\u8981\u7528\u4e8e\u63a7\u5236\u6d41\u7a0b\uff0c\u4e3b\u8981\u804c\u8d23\u5982\u4e0b\uff1a\n<\/p>\n<ol>\n<li>\n\t\t\u6587\u4ef6\u4e0a\u4f20\u89e3\u6790\uff0c\u5982\u679c\u8bf7\u6c42\u7c7b\u578b\u662fmultipart\u5c06\u901a\u8fc7MultipartResolver\u8fdb\u884c\u6587\u4ef6\u4e0a\u4f20\u89e3\u6790\uff1b\n\t<\/li>\n<li>\n\t\t\u901a\u8fc7HandlerMapping\uff0c\u5c06\u8bf7\u6c42\u6620\u5c04\u5230\u5904\u7406\u5668\uff08\u8fd4\u56de\u4e00\u4e2aHandlerExecutionChain\uff0c\u5b83\u5305\u62ec\u4e00\u4e2a\u5904\u7406\u5668\u3001\u591a\u4e2aHandlerInterceptor\u62e6\u622a\u5668\uff09\uff1b\n\t<\/li>\n<li>\n\t\t\u901a\u8fc7HandlerAdapter\u652f\u6301\u591a\u79cd\u7c7b\u578b\u7684\u5904\u7406\u5668(HandlerExecutionChain\u4e2d\u7684\u5904\u7406\u5668)\uff1b\n\t<\/li>\n<li>\n\t\t\u901a\u8fc7ViewResolver\u89e3\u6790\u903b\u8f91\u89c6\u56fe\u540d\u5230\u5177\u4f53\u89c6\u56fe\u5b9e\u73b0\uff1b\n\t<\/li>\n<li>\n\t\t\u672c\u5730\u5316\u89e3\u6790\uff1b\n\t<\/li>\n<li>\n\t\t\u6e32\u67d3\u5177\u4f53\u7684\u89c6\u56fe\u7b49\uff1b\n\t<\/li>\n<li>\n\t\t\u5982\u679c\u6267\u884c\u8fc7\u7a0b\u4e2d\u9047\u5230\u5f02\u5e38\u5c06\u4ea4\u7ed9HandlerExceptionResolver\u6765\u89e3\u6790\u3002\n\t<\/li>\n<\/ol>\n<p>\n\t\u4ece\u4ee5\u4e0a\u6211\u4eec\u53ef\u4ee5\u770b\u51faDispatcherServlet\u4e3b\u8981\u8d1f\u8d23\u6d41\u7a0b\u7684\u63a7\u5236\uff08\u800c\u4e14\u5728\u6d41\u7a0b\u4e2d\u7684\u6bcf\u4e2a\u5173\u952e\u70b9\u90fd\u662f\u5f88\u5bb9\u6613\u6269\u5c55\u7684\uff09\u3002\n<\/p>\n<h2>\n\tDispatcherServlet\u5728web.xml\u4e2d\u7684\u914d\u7f6e<br \/>\n<\/h2>\n<pre class=\"brush:xml;\">\r\n&lt;servlet&gt;\r\n\t&lt;servlet-name&gt;chapter2&lt;\/servlet-name&gt;\r\n\t&lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;\/servlet-class&gt;\r\n\t&lt;load-on-startup&gt;1&lt;\/load-on-startup&gt;\r\n&lt;\/servlet&gt;\r\n&lt;servlet-mapping&gt;\r\n\t&lt;servlet-name&gt;chapter2&lt;\/servlet-name&gt;\r\n\t&lt;url-pattern&gt;\/&lt;\/url-pattern&gt;\r\n&lt;\/servlet-mapping&gt;<\/pre>\n<ul>\n<li>\n\t\tload-on-startup\uff1a\u8868\u793a\u542f\u52a8\u5bb9\u5668\u65f6\u521d\u59cb\u5316\u8be5Servlet\uff1b\n\t<\/li>\n<li>\n\t\turl-pattern\uff1a\u8868\u793a\u54ea\u4e9b\u8bf7\u6c42\u4ea4\u7ed9Spring Web MVC\u5904\u7406\uff0c &ldquo;\/&rdquo; \u662f\u7528\u6765\u5b9a\u4e49\u9ed8\u8ba4servlet\u6620\u5c04\u7684\u3002\u4e5f\u53ef\u4ee5\u5982&ldquo;*.html&rdquo;\u8868\u793a\u62e6\u622a\u6240\u6709\u4ee5html\u4e3a\u6269\u5c55\u540d\u7684\u8bf7\u6c42\u3002\n\t<\/li>\n<\/ul>\n<p>\n\t\u8be5DispatcherServlet\u9ed8\u8ba4\u4f7f\u7528<a href=\"http:\/\/baike.baidu.com\/item\/ApplicationContext\/1129418\" name=\"ApplicationContext\" target=\"_blank\" title=\"ApplicationContext\">WebApplicationContext<\/a>\u4f5c\u4e3a\u4e0a\u4e0b\u6587\uff0cSpring\u9ed8\u8ba4\u914d\u7f6e\u6587\u4ef6\u4e3a&ldquo;\/WEB-INF\/[servlet\u540d\u5b57]-servlet.xml&rdquo;\u3002<br \/>\n\t&nbsp;<br \/>\n\tDispatcherServlet\u4e5f\u53ef\u4ee5\u914d\u7f6e\u81ea\u5df1\u7684\u521d\u59cb\u5316\u53c2\u6570\uff0c\u8986\u76d6\u9ed8\u8ba4\u914d\u7f6e\uff1a\n<\/p>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:600px\">\n<tbody>\n<tr>\n<td>\n\t\t\t\t\u53c2\u6570\n\t\t\t<\/td>\n<td>\n\t\t\t\t\u63cf\u8ff0\n\t\t\t<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\tcontextClass\n\t\t\t<\/td>\n<td>\n\t\t\t\t\u5b9e\u73b0WebApplicationContext\u63a5\u53e3\u7684\u7c7b\uff0c\u5f53\u524d\u7684servlet\u7528\u5b83\u6765\u521b\u5efa\u4e0a\u4e0b\u6587\u3002\u5982\u679c\u8fd9\u4e2a\u53c2\u6570\u6ca1\u6709\u6307\u5b9a\uff0c \u9ed8\u8ba4\u4f7f\u7528XmlWebApplicationContext\u3002\n\t\t\t<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\tcontextConfigLocation\n\t\t\t<\/td>\n<td>\n\t\t\t\t\u4f20\u7ed9\u4e0a\u4e0b\u6587\u5b9e\u4f8b\uff08\u7531contextClass\u6307\u5b9a\uff09\u7684\u5b57\u7b26\u4e32\uff0c\u7528\u6765\u6307\u5b9a\u4e0a\u4e0b\u6587\u7684\u4f4d\u7f6e\u3002\u8fd9\u4e2a\u5b57\u7b26\u4e32\u53ef\u4ee5\u88ab\u5206\u6210\u591a\u4e2a\u5b57\u7b26\u4e32\uff08\u4f7f\u7528\u9017\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\uff09 \u6765\u652f\u6301\u591a\u4e2a\u4e0a\u4e0b\u6587\uff08\u5728\u591a\u4e0a\u4e0b\u6587\u7684\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u540c\u4e00\u4e2abean\u88ab\u5b9a\u4e49\u4e24\u6b21\uff0c\u540e\u9762\u4e00\u4e2a\u4f18\u5148\uff09\u3002\n\t\t\t<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\tnamespace\n\t\t\t<\/td>\n<td>\n\t\t\t\tWebApplicationContext\u547d\u540d\u7a7a\u95f4\u3002\u9ed8\u8ba4\u503c\u662f[server-name]-servlet\u3002\n\t\t\t<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\n\t\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u521d\u59cb\u5316\u53c2\u6570\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;servlet&gt;\r\n\t&lt;servlet-name&gt;chapter2&lt;\/servlet-name&gt;\r\n\t&lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;\/servlet-class&gt;\r\n\t&lt;load-on-startup&gt;1&lt;\/load-on-startup&gt;\r\n\t&lt;init-param&gt;\r\n\t\t&lt;param-name&gt;contextConfigLocation&lt;\/param-name&gt;\r\n\t\t&lt;param-value&gt;classpath:spring-servlet-config.xml&lt;\/param-value&gt;\r\n\t&lt;\/init-param&gt;\r\n&lt;\/servlet&gt;<\/pre>\n<p>\n\t\u5982\u679c\u4f7f\u7528\u5982\u4e0a\u914d\u7f6e\uff0cSpring Web MVC\u6846\u67b6\u5c06\u52a0\u8f7d&ldquo;classpath:spring-servlet-config.xml&rdquo;\u6765\u8fdb\u884c\u521d\u59cb\u5316\u4e0a\u4e0b\u6587\u800c\u4e0d\u662f&ldquo;\/WEB-INF\/[servlet\u540d\u5b57]-servlet.xml&rdquo;\u3002\n<\/p>\n<h2>\n\t\u4e0a\u4e0b\u6587\u5173\u7cfb<br \/>\n<\/h2>\n<p>\n\t\u96c6\u6210Web\u73af\u5883\u7684\u901a\u7528\u914d\u7f6e\uff1a\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;context-param&gt;\r\n      &lt;param-name&gt;contextConfigLocation&lt;\/param-name&gt;\r\n      &lt;param-value&gt;\r\n          classpath:spring-common-config.xml,\r\n          classpath:spring-budget-config.xml\r\n      &lt;\/param-value&gt;\r\n&lt;\/context-param&gt;\r\n&lt;listener&gt;  \r\n\t&lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;\/listener-class&gt;\r\n&lt;\/listener&gt;<\/pre>\n<p>\n\t\u5982\u4e0a\u914d\u7f6e\u662fSpring\u96c6\u6210Web\u73af\u5883\u7684\u901a\u7528\u914d\u7f6e\uff1b\u4e00\u822c\u7528\u4e8e\u52a0\u8f7d\u9664Web\u5c42\u7684Bean\uff08\u5982DAO\u3001Service\u7b49\uff09\uff0c\u4ee5\u4fbf\u4e8e\u4e0e\u5176\u4ed6\u4efb\u4f55Web\u6846\u67b6\u96c6\u6210\u3002\n<\/p>\n<ul>\n<li>\n\t\tcontextConfigLocation\uff1a\u8868\u793a\u7528\u4e8e\u52a0\u8f7dBean\u7684\u914d\u7f6e\u6587\u4ef6\uff1b\n\t<\/li>\n<li>\n\t\tcontextClass\uff1a\u8868\u793a\u7528\u4e8e\u52a0\u8f7dBean\u7684ApplicationContext\u5b9e\u73b0\u7c7b\uff0c\u9ed8\u8ba4WebApplicationContext\u3002\n\t<\/li>\n<\/ul>\n<p>\n\t\u521b\u5efa\u5b8c\u6bd5\u540e\u4f1a\u5c06\u8be5\u4e0a\u4e0b\u6587\u653e\u5728ServletContext\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\nservletContext.setAttribute(\r\nWebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,\r\nthis.context);<\/pre>\n<p>\n\tContextLoaderListener\u521d\u59cb\u5316\u7684\u4e0a\u4e0b\u6587\u548cDispatcherServlet\u521d\u59cb\u5316\u7684\u4e0a\u4e0b\u6587\u5173\u7cfb\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"ContextLoaderListener\u521d\u59cb\u5316\u7684\u4e0a\u4e0b\u6587\u548cDispatcherServlet\u521d\u59cb\u5316\u7684\u4e0a\u4e0b\u6587\u5173\u7cfb\" src=\"http:\/\/img.bbs.csdn.net\/upload\/201707\/10\/1499675067_861263.jpg\" style=\"width: 860px;height: 624px\" \/>\n<\/p>\n<p>\n\t\u4ece\u56fe\u4e2d\u53ef\u4ee5\u770b\u51fa\uff1a<br \/>\n\tContextLoaderListener\u521d\u59cb\u5316\u7684\u4e0a\u4e0b\u6587\u52a0\u8f7d\u7684Bean\u662f\u5bf9\u4e8e\u6574\u4e2a\u5e94\u7528\u7a0b\u5e8f\u5171\u4eab\u7684\uff0c\u4e0d\u7ba1\u662f\u4f7f\u7528\u4ec0\u4e48\u8868\u73b0\u5c42\u6280\u672f\uff0c\u4e00\u822c\u5982DAO\u5c42\u3001Service\u5c42Bean\uff1b<br \/>\n\tDispatcherServlet\u521d\u59cb\u5316\u7684\u4e0a\u4e0b\u6587\u52a0\u8f7d\u7684Bean\u662f\u53ea\u5bf9Spring Web MVC\u6709\u6548\u7684Bean\uff0c\u5982Controller\u3001HandlerMapping\u3001HandlerAdapter\u7b49\u7b49\uff0c\u8be5\u521d\u59cb\u5316\u4e0a\u4e0b\u6587\u5e94\u8be5\u53ea\u52a0\u8f7dWeb\u76f8\u5173\u7ec4\u4ef6\u3002\n<\/p>\n<h2>\n\tDispatcherServlet\u521d\u59cb\u5316\u987a\u5e8f<br \/>\n<\/h2>\n<p>\n\t\u7ee7\u627f\u4f53\u7cfb\u7ed3\u6784\u5982\u4e0b\u6240\u793a\uff1a\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"DispatcherServlet \u7ee7\u627f\u4f53\u7cfb\u7ed3\u6784\" src=\"http:\/\/img.bbs.csdn.net\/upload\/201707\/10\/1499675246_587851.jpg\" style=\"width: 317px;height: 100px\" \/>\n<\/p>\n<h2>\n\tHttpServletBean\u7ee7\u627fHttpServlet<br \/>\n<\/h2>\n<p>\n\t\u5728Web\u5bb9\u5668\u542f\u52a8\u65f6\u5c06\u8c03\u7528\u5b83\u7684init\u65b9\u6cd5\uff0c\u8be5\u521d\u59cb\u5316\u65b9\u6cd5\u7684\u4e3b\u8981\u4f5c\u7528\uff1a\u5c06Servlet\u521d\u59cb\u5316\u53c2\u6570\uff08init-param\uff09\u8bbe\u7f6e\u5230\u8be5\u7ec4\u4ef6\u4e0a\uff08\u5982contextAttribute\u3001contextClass\u3001namespace\u3001contextConfigLocation\uff09\uff0c\u901a\u8fc7BeanWrapper\u7b80\u5316\u8bbe\u503c\u8fc7\u7a0b\uff0c\u65b9\u4fbf\u540e\u7eed\u4f7f\u7528\uff1b\u63d0\u4f9b\u7ed9\u5b50\u7c7b\u521d\u59cb\u5316\u6269\u5c55\u70b9\uff0cinitServletBean()\uff0c\u8be5\u65b9\u6cd5\u7531FrameworkServlet\u8986\u76d6\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\npublic abstract class HttpServletBean extends HttpServlet implements EnvironmentAware{\r\n\t@Override\r\n    public final void init() throws ServletException {\r\n        \/\/\u7701\u7565\u90e8\u5206\u4ee3\u7801\r\n        \/\/1\u3001\u5982\u4e0b\u4ee3\u7801\u7684\u4f5c\u7528\u662f\u5c06Servlet\u521d\u59cb\u5316\u53c2\u6570\u8bbe\u7f6e\u5230\u8be5\u7ec4\u4ef6\u4e0a\r\n\t\t\/\/\u5982contextAttribute\u3001contextClass\u3001namespace\u3001contextConfigLocation\uff1b\r\n        try {\r\n\t\t\tPropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);\r\n\t\t\tBeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);\r\n\t\t\tResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());\r\n\t\t\tbw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, this.environment));\r\n\t\t\tinitBeanWrapper(bw);\r\n\t\t\tbw.setPropertyValues(pvs, true);\r\n        }catch (BeansException ex) {\r\n           \/\/&hellip;&hellip;&hellip;&hellip;\u7701\u7565\u5176\u4ed6\u4ee3\u7801\r\n        }\r\n        \/\/2\u3001\u63d0\u4f9b\u7ed9\u5b50\u7c7b\u521d\u59cb\u5316\u7684\u6269\u5c55\u70b9\uff0c\u8be5\u65b9\u6cd5\u7531FrameworkServlet\u8986\u76d6\r\n        initServletBean();\r\n        if (logger.isDebugEnabled()) {\r\n\t\t\tlogger.debug(&quot;Servlet &#039;&quot; + getServletName() + &quot;&#039; configured successfully&quot;);\r\n        }\r\n    }\r\n    \/\/&hellip;&hellip;&hellip;&hellip;\u7701\u7565\u5176\u4ed6\u4ee3\u7801\r\n}<\/pre>\n<h2>\n\tFrameworkServlet\u7ee7\u627fHttpServletBean<br \/>\n<\/h2>\n<p>\n\t\u901a\u8fc7initServletBean()\u8fdb\u884cWeb\u4e0a\u4e0b\u6587\u521d\u59cb\u5316\uff0c\u8be5\u65b9\u6cd5\u4e3b\u8981\u8986\u76d6\u4e00\u4e0b\u4e24\u4ef6\u4e8b\u60c5\uff1a\n<\/p>\n<ul>\n<li>\n\t\t\u521d\u59cb\u5316web\u4e0a\u4e0b\u6587\uff1b\n\t<\/li>\n<li>\n\t\t\u63d0\u4f9b\u7ed9\u5b50\u7c7b\u521d\u59cb\u5316\u6269\u5c55\u70b9\uff1b\n\t<\/li>\n<\/ul>\n<pre class=\"brush:java;\">\r\npublic abstract class FrameworkServlet extends HttpServletBean {\r\n\t@Override\r\n    protected final void initServletBean() throws ServletException {\r\n        \/\/\u7701\u7565\u90e8\u5206\u4ee3\u7801\r\n        try {\r\n            \/\/1\u3001\u521d\u59cb\u5316Web\u4e0a\u4e0b\u6587\r\n            this.webApplicationContext = initWebApplicationContext();\r\n            \/\/2\u3001\u63d0\u4f9b\u7ed9\u5b50\u7c7b\u521d\u59cb\u5316\u7684\u6269\u5c55\u70b9\r\n\t\t\tinitFrameworkServlet();\r\n        }\r\n        \/\/\u7701\u7565\u90e8\u5206\u4ee3\u7801\r\n    }\r\n}\r\nprotected WebApplicationContext initWebApplicationContext() {\r\n\t\/\/ROOT\u4e0a\u4e0b\u6587\uff08ContextLoaderListener\u52a0\u8f7d\u7684\uff09\r\n\tWebApplicationContext rootContext =\r\n\tWebApplicationContextUtils.getWebApplicationContext(getServletContext());\r\n\tWebApplicationContext wac = null;\r\n\tif (this.webApplicationContext != null) {\r\n\t   \/\/ 1\u3001\u5728\u521b\u5efa\u8be5Servlet\u6ce8\u5165\u7684\u4e0a\u4e0b\u6587\r\n\t   wac = this.webApplicationContext;\r\n\t   if (wac instanceof ConfigurableWebApplicationContext) {\r\n\t\t  ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;\r\n\t\t  if (!cwac.isActive()) {\r\n\t\t\t  if (cwac.getParent() == null) {\r\n\t\t\t\t  cwac.setParent(rootContext);\r\n\t\t\t  }\r\n\t\t\t  configureAndRefreshWebApplicationContext(cwac);\r\n\t\t  }\r\n\t   }\r\n    }\r\n    if (wac == null) {\r\n\t\t \/\/2\u3001\u67e5\u627e\u5df2\u7ecf\u7ed1\u5b9a\u7684\u4e0a\u4e0b\u6587\r\n\t\twac = findWebApplicationContext();\r\n    }\r\n   if (wac == null) {\r\n\t\t\/\/3\u3001\u5982\u679c\u6ca1\u6709\u627e\u5230\u76f8\u5e94\u7684\u4e0a\u4e0b\u6587\uff0c\u5e76\u6307\u5b9a\u7236\u4eb2\u4e3aContextLoaderListener\r\n\t   wac = createWebApplicationContext(rootContext);\r\n   }\r\n   if (!this.refreshEventReceived) {\r\n\t\t \/\/4\u3001\u5237\u65b0\u4e0a\u4e0b\u6587\uff08\u6267\u884c\u4e00\u4e9b\u521d\u59cb\u5316\uff09\r\n\t   onRefresh(wac);\r\n   }\r\n   if (this.publishContext) {\r\n\t   \/\/ Publish the context as a servlet context attribute.\r\n\t   String attrName = getServletContextAttributeName();\r\n\t   getServletContext().setAttribute(attrName, wac);\r\n\t   \/\/\u7701\u7565\u90e8\u5206\u4ee3\u7801\r\n   }\r\n   return wac;\r\n}<\/pre>\n<p>\n\t\u4eceinitWebApplicationContext\uff08\uff09\u65b9\u6cd5\u53ef\u4ee5\u770b\u51fa\uff0c\u57fa\u672c\u4e0a\u5982\u679cContextLoaderListener\u52a0\u8f7d\u4e86\u4e0a\u4e0b\u6587\u5c06\u4f5c\u4e3a\u6839\u4e0a\u4e0b\u6587\uff08DispatcherServlet\u7684\u7236\u5bb9\u5668\uff09\u3002<br \/>\n\t&nbsp;<br \/>\n\t\u6700\u540e\u8c03\u7528\u4e86onRefresh()\u65b9\u6cd5\u6267\u884c\u5bb9\u5668\u7684\u4e00\u4e9b\u521d\u59cb\u5316\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u7531\u5b50\u7c7b\u5b9e\u73b0\uff0c\u6765\u8fdb\u884c\u6269\u5c55\u3002\n<\/p>\n<h2>\n\tDispatcherServlet\u7ee7\u627fFrameworkServlet<br \/>\n<\/h2>\n<p>\n\tDispatcherServlet\u7ee7\u627fFrameworkServlet\uff0c\u5e76\u5b9e\u73b0\u4e86onRefresh()\u65b9\u6cd5\u63d0\u4f9b\u4e00\u4e9b\u524d\u7aef\u63a7\u5236\u5668\u76f8\u5173\u7684\u914d\u7f6e\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npublic class DispatcherServlet extends FrameworkServlet {\r\n     \/\/\u5b9e\u73b0\u5b50\u7c7b\u7684onRefresh()\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u59d4\u6258\u4e3ainitStrategies()\u65b9\u6cd5\u3002\r\n    @Override\r\n    protected void onRefresh(ApplicationContext context) {\r\n       initStrategies(context);\r\n    }\r\n    \/\/\u521d\u59cb\u5316\u9ed8\u8ba4\u7684Spring Web MVC\u6846\u67b6\u4f7f\u7528\u7684\u7b56\u7565\uff08\u5982HandlerMapping\uff09\r\n    protected void initStrategies(ApplicationContext context) {\r\n       initMultipartResolver(context);\r\n       initLocaleResolver(context);\r\n       initThemeResolver(context);\r\n       initHandlerMappings(context);\r\n       initHandlerAdapters(context);\r\n       initHandlerExceptionResolvers(context);\r\n       initRequestToViewNameTranslator(context);\r\n       initViewResolvers(context);\r\n       initFlashMapManager(context);\r\n    }\r\n}<\/pre>\n<p>\n\t\u4ece\u5982\u4e0a\u4ee3\u7801\u53ef\u4ee5\u770b\u51fa\uff0cDispatcherServlet\u542f\u52a8\u65f6\u4f1a\u8fdb\u884c\u6211\u4eec\u9700\u8981\u7684Web\u5c42Bean\u7684\u914d\u7f6e\uff0c\u5982HandlerMapping\u3001HandlerAdapter\u7b49\uff0c\u800c\u4e14\u5982\u679c\u6211\u4eec\u6ca1\u6709\u914d\u7f6e\uff0c\u8fd8\u4f1a\u7ed9\u6211\u4eec\u63d0\u4f9b\u9ed8\u8ba4\u7684\u914d\u7f6e\u3002<br \/>\n\t&nbsp;<br \/>\n\t\u4ece\u5982\u4e0a\u4ee3\u7801\u6211\u4eec\u53ef\u4ee5\u770b\u51fa\uff0c\u6574\u4e2aDispatcherServlet\u521d\u59cb\u5316\u7684\u8fc7\u7a0b\u548c\u505a\u4e86\u4e9b\u4ec0\u4e48\u4e8b\u60c5\uff0c\u5177\u4f53\u4e3b\u8981\u505a\u4e86\u5982\u4e0b\u4e24\u4ef6\u4e8b\u60c5\uff1a\n<\/p>\n<ol>\n<li>\n\t\t\u521d\u59cb\u5316Spring Web MVC\u4f7f\u7528\u7684Web\u4e0a\u4e0b\u6587\uff0c\u5e76\u4e14\u53ef\u80fd\u6307\u5b9a\u7236\u5bb9\u5668\u4e3a\uff08ContextLoaderListener\u52a0\u8f7d\u4e86\u6839\u4e0a\u4e0b\u6587\uff09\uff1b\n\t<\/li>\n<li>\n\t\t\u521d\u59cb\u5316DispatcherServlet\u4f7f\u7528\u7684\u7b56\u7565\uff0c\u5982HandlerMapping\u3001HandlerAdapter\u7b49\u3002\n\t<\/li>\n<\/ol>\n<p>\n\t\u670d\u52a1\u5668\u542f\u52a8\u65f6\u7684\u65e5\u5fd7\u5206\u6790\u4e5f\u53ef\u4ee5\u770b\u51faDispatcherServlet\u7684\u52a0\u8f7d\u987a\u5e8f\uff08\u6b64\u5904\u52a0\u4e0a\u4e86ContextLoaderListener\u4ece\u800c\u542f\u52a8ROOT\u4e0a\u4e0b\u6587\u5bb9\u5668\uff09\u3002\n<\/p>\n<h2>\n\tDispatcherServlet\u9ed8\u8ba4\u914d\u7f6e<br \/>\n<\/h2>\n<p>\n\tDispatcherServlet\u7684\u9ed8\u8ba4\u914d\u7f6e\u5728DispatcherServlet.properties\uff08\u548cDispatcherServlet\u7c7b\u5728\u4e00\u4e2a\u5305\u4e0b\uff09\u4e2d\uff0c\u800c\u4e14\u662f\u5f53Spring\u914d\u7f6e\u6587\u4ef6\u4e2d\u6ca1\u6709\u6307\u5b9a\u914d\u7f6e\u65f6\u4f7f\u7528\u7684\u9ed8\u8ba4\u7b56\u7565\uff1a\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"DispatcherServlet\u7684\u9ed8\u8ba4\u914d\u7f6e\u5728DispatcherServlet.properties\" src=\"http:\/\/img.bbs.csdn.net\/upload\/201707\/10\/1499675918_248912.jpg\" style=\"width: 419px;height: 732px\" \/>\n<\/p>\n<p>\n\t\u4eceDispatcherServlet.properties\u914d\u7f6e\u53ef\u4ee5\u770b\u51faDispatcherServlet\u5728\u542f\u52a8\u65f6\u4f1a\u81ea\u52a8\u6ce8\u518c\u8fd9\u4e9b\u7279\u6b8a\u7684Bean\uff0c\u65e0\u9700\u6211\u4eec\u6ce8\u518c\uff0c\u5982\u679c\u6211\u4eec\u6ce8\u518c\u4e86\uff0c\u9ed8\u8ba4\u7684\u5c06\u4e0d\u4f1a\u6ce8\u518c\u3002<br \/>\n\t\u56e0\u6b64\u524d\u9762\u7684\u4f8b\u5b50\u4e2d\u7684BeanNameUrlHandlerMapping\u3001SimpleControllerHandlerAdapter\u662f\u4e0d\u9700\u8981\u6ce8\u518c\u7684\uff0cDispatcherServlet\u9ed8\u8ba4\u4f1a\u6ce8\u518c\u8fd9\u4e24\u4e2aBean\u3002<br \/>\n\t\u4eceDispatcherServlet.properties\u53ef\u4ee5\u770b\u51fa\u6709\u8bb8\u591a\u7279\u6b8a\u7684Bean\uff0c\u90a3\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u770b\u770bSpring Web MVC\u4e3b\u8981\u6709\u54ea\u4e9b\u7279\u6b8a\u7684Bean\u3002\n<\/p>\n<h2>\n\tDispatcherServlet\u4e2d\u4f7f\u7528\u7684\u7279\u6b8a\u7684Bean<br \/>\n<\/h2>\n<p>\n\tDispatcherServlet\u9ed8\u8ba4\u4f7f\u7528WebApplicationContext\u4f5c\u4e3a\u4e0a\u4e0b\u6587\uff0c\u56e0\u6b64\u6211\u4eec\u6765\u770b\u4e00\u4e0b\u8be5\u4e0a\u4e0b\u6587\u4e2d\u6709\u54ea\u4e9b\u7279\u6b8a\u7684Bean\uff1a\n<\/p>\n<ol>\n<li>\n\t\tController\uff1a\u5904\u7406\u5668\/\u9875\u9762\u63a7\u5236\u5668\uff0c\u505a\u7684\u662fMVC\u4e2d\u7684C\u7684\u4e8b\u60c5\uff0c\u4f46\u63a7\u5236\u903b\u8f91\u8f6c\u79fb\u5230\u524d\u7aef\u63a7\u5236\u5668\u4e86\uff0c\u7528\u4e8e\u5bf9\u8bf7\u6c42\u8fdb\u884c\u5904\u7406\uff1b\n\t<\/li>\n<li>\n\t\tHandlerMapping\uff1a\u8bf7\u6c42\u5230\u5904\u7406\u5668\u7684\u6620\u5c04\uff0c\u5982\u679c\u6620\u5c04\u6210\u529f\u8fd4\u56de\u4e00\u4e2aHandlerExecutionChain\u5bf9\u8c61\uff08\u5305\u542b\u4e00\u4e2aHandler\u5904\u7406\u5668\uff08\u9875\u9762\u63a7\u5236\u5668\uff09\u5bf9\u8c61\u3001\u591a\u4e2aHandlerInterceptor\u62e6\u622a\u5668\uff09\u5bf9\u8c61\uff1b\u5982BeanNameUrlHandlerMapping\u5c06URL\u4e0eBean\u540d\u5b57\u6620\u5c04\uff0c\u6620\u5c04\u6210\u529f\u7684Bean\u5c31\u662f\u6b64\u5904\u7684\u5904\u7406\u5668\uff1b\n\t<\/li>\n<li>\n\t\tHandlerAdapter\uff1aHandlerAdapter\u5c06\u4f1a\u628a\u5904\u7406\u5668\u5305\u88c5\u4e3a\u9002\u914d\u5668\uff0c\u4ece\u800c\u652f\u6301\u591a\u79cd\u7c7b\u578b\u7684\u5904\u7406\u5668\uff0c\u5373\u9002\u914d\u5668\u8bbe\u8ba1\u6a21\u5f0f\u7684\u5e94\u7528\uff0c\u4ece\u800c\u5f88\u5bb9\u6613\u652f\u6301\u5f88\u591a\u7c7b\u578b\u7684\u5904\u7406\u5668\uff1b\u5982SimpleControllerHandlerAdapter\u5c06\u5bf9\u5b9e\u73b0\u4e86Controller\u63a5\u53e3\u7684Bean\u8fdb\u884c\u9002\u914d\uff0c\u5e76\u4e14\u6389\u5904\u7406\u5668\u7684handleRequest\u65b9\u6cd5\u8fdb\u884c\u529f\u80fd\u5904\u7406\uff1b\n\t<\/li>\n<li>\n\t\tViewResolver\uff1aViewResolver\u5c06\u628a\u903b\u8f91\u89c6\u56fe\u540d\u89e3\u6790\u4e3a\u5177\u4f53\u7684View\uff0c\u901a\u8fc7\u8fd9\u79cd\u7b56\u7565\u6a21\u5f0f\uff0c\u5f88\u5bb9\u6613\u66f4\u6362\u5176\u4ed6\u89c6\u56fe\u6280\u672f\uff1b\u5982InternalResourceViewResolver\u5c06\u903b\u8f91\u89c6\u56fe\u540d\u6620\u5c04\u4e3ajsp\u89c6\u56fe\uff1b\n\t<\/li>\n<li>\n\t\tLocalResover\uff1a\u672c\u5730\u5316\u89e3\u6790\uff0c\u56e0\u4e3aSpring\u652f\u6301\u56fd\u9645\u5316\uff0c\u56e0\u6b64LocalResover\u89e3\u6790\u5ba2\u6237\u7aef\u7684Locale\u4fe1\u606f\u4ece\u800c\u65b9\u4fbf\u8fdb\u884c\u56fd\u9645\u5316\uff1b\n\t<\/li>\n<li>\n\t\tThemeResovler\uff1a\u4e3b\u9898\u89e3\u6790\uff0c\u901a\u8fc7\u5b83\u6765\u5b9e\u73b0\u4e00\u4e2a\u9875\u9762\u591a\u5957\u98ce\u683c\uff0c\u5373\u5e38\u89c1\u7684\u7c7b\u4f3c\u4e8e\u8f6f\u4ef6\u76ae\u80a4\u6548\u679c\uff1b\n\t<\/li>\n<li>\n\t\tMultipartResolver\uff1a\u6587\u4ef6\u4e0a\u4f20\u89e3\u6790\uff0c\u7528\u4e8e\u652f\u6301\u6587\u4ef6\u4e0a\u4f20\uff1b\n\t<\/li>\n<li>\n\t\tHandlerExceptionResolver\uff1a\u5904\u7406\u5668\u5f02\u5e38\u89e3\u6790\uff0c\u53ef\u4ee5\u5c06\u5f02\u5e38\u6620\u5c04\u5230\u76f8\u5e94\u7684\u7edf\u4e00\u9519\u8bef\u754c\u9762\uff0c\u4ece\u800c\u663e\u793a\u7528\u6237\u53cb\u597d\u7684\u754c\u9762\uff08\u800c\u4e0d\u662f\u7ed9\u7528\u6237\u770b\u5230\u5177\u4f53\u7684\u9519\u8bef\u4fe1\u606f\uff09\uff1b\n\t<\/li>\n<li>\n\t\tRequestToViewNameTranslator\uff1a\u5f53\u5904\u7406\u5668\u6ca1\u6709\u8fd4\u56de\u903b\u8f91\u89c6\u56fe\u540d\u7b49\u76f8\u5173\u4fe1\u606f\u65f6\uff0c\u81ea\u52a8\u5c06\u8bf7\u6c42URL\u6620\u5c04\u4e3a\u903b\u8f91\u89c6\u56fe\u540d\uff1b\n\t<\/li>\n<li>\n\t\tFlashMapManager\uff1a\u7528\u4e8e\u7ba1\u7406FlashMap\u7684\u7b56\u7565\u63a5\u53e3\uff0cFlashMap\u7528\u4e8e\u5b58\u50a8\u4e00\u4e2a\u8bf7\u6c42\u7684\u8f93\u51fa\uff0c\u5f53\u8fdb\u5165\u53e6\u4e00\u4e2a\u8bf7\u6c42\u65f6\u4f5c\u4e3a\u8be5\u8bf7\u6c42\u7684\u8f93\u5165\uff0c\u901a\u5e38\u7528\u4e8e\u91cd\u5b9a\u5411\u573a\u666f\uff0c\u540e\u8fb9\u4f1a\u7ec6\u8ff0\u3002\n\t<\/li>\n<\/ol>\n<p>\n\t\u5230\u8fd9\u91cc<a href=\"http:\/\/baike.baidu.com\/item\/spring MVC\" name=\"spring MVC\" target=\"_blank\" title=\"spring MVC\">Spring MVC<\/a>\u6838\u5fc3\u7684<a href=\"http:\/\/baike.baidu.com\/item\/DispatcherServlet\" name=\"DispatcherServlet\" target=\"_blank\" title=\"DispatcherServlet\">DispatcherServlet<\/a>\u6211\u4eec\u5df2\u7ecf\u5b66\u4e60\u5b8c\u4e86\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u9700\u8981\u628a\u4e0a\u8fb9\u63d0\u5230\u7684\u7279\u6b8aBean\u6328\u4e2a\u51fb\u7834\uff0c\u90a3\u9996\u5148\u4ece\u63a7\u5236\u5668\u5f00\u59cb\u5427\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=1629\">\u8be6\u89e3Spring MVC\u4e2dDispatcherServlet\u7684\u4f5c\u7528<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>Spring MVC\u7684\u6838\u5fc3\u5c31\u662fDispatcherServlet\u3002\u56e0\u6b64\u6211\u4eec\u5f88\u6709\u5fc5\u8981\u7cfb\u7edf\u6027\u7684\u5bf9\u5b83\u8fdb\u884c\u4e00\u4e0b\u7814\u7a76\u3002\u672c [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1366,898,1350,1360,1367],"class_list":["post-1629","post","type-post","status-publish","format-standard","hentry","category-java","tag-beannameurlhandlermapping","tag-dispatcherservlet","tag-spring-mvc","tag-spring-mvc-"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1629","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1629"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1629\/revisions"}],"predecessor-version":[{"id":1630,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1629\/revisions\/1630"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}