{"id":1146,"date":"2017-03-16T18:41:34","date_gmt":"2017-03-16T10:41:34","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=1146"},"modified":"2017-03-16T18:41:34","modified_gmt":"2017-03-16T10:41:34","slug":"%e8%af%a6%e8%a7%a3-spring-%e4%b8%ad-contextloaderlistener-%e7%9a%84%e4%bd%9c%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=1146","title":{"rendered":"\u8be6\u89e3 Spring \u4e2d ContextLoaderListener \u7684\u4f5c\u7528"},"content":{"rendered":"<p>\n\t\u6700\u8fd1\u516c\u53f8\u6765\u4e86\u65b0\u540c\u4e8b\uff0c\u521a\u6bd5\u4e1a\u4e0d\u4e45\u3002\u95ee\u5230\u6211 web.xml \u4e2d\u7684&nbsp;ContextLoaderListener \u662f\u5e72\u4ec0\u4e48\u7528\u7684\uff1f\u6211\u4e00\u65f6\u6123\u4f4f\u4e86\uff0c\u56e0\u4e3a\u6211\u4e5f\u4e0d\u6e05\u695a\u4e86\u3002\u5173\u4e8e&nbsp;ContextLoaderListener \u7684\u77e5\u8bc6\uff0c\u6211\u4eec\u8fd9\u91cc\u4e00\u8d77\u518d\u8ba4\u8bc6\u4e00\u4e0b\uff01\n<\/p>\n<p>\n\t\u6240\u8c13ContextLoaderListener\uff0c\u5c31\u662f\u5728web\u90e8\u7f72\u63cf\u8ff0\u7b26\u5373web.xml\u91cc\u9762\u7ecf\u5e38\u914d\u7f6e\u7684\u4e00\u4e2a\u76d1\u542c\u5668\uff0c\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;listener&gt;  \r\n    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;\/listener-class&gt;  \r\n&lt;\/listener&gt; <\/pre>\n<h2>\n\tContextLoaderListener \u53ef\u4ee5\u4e0d\u914d\u7f6e\u5417<br \/>\n<\/h2>\n<p>\n\t\u7b54\u6848\u662f\u80af\u5b9a\u7684\uff1a\u53ef\u4ee5\u4e0d\u505a\u4efb\u4f55\u914d\u7f6e\uff01\n<\/p>\n<p>\n\t\u67e5\u770b\u4e86ContextLoaderListener \u6e90\u4ee3\u7801\uff0c\u53d1\u73b0\u5b83\u7ee7\u627f\u81eaContextLoader\uff0c\u5e76\u4e14\u5b9e\u73b0ServletContextListener\u63a5\u53e3\n<\/p>\n<p>\n\tContextLoaderListener \u6e90\u4ee3\u7801\u5f88\u7b80\u5355\uff0c\u6838\u5fc3\u662f\u5b9e\u73b0\u4e86 ServletContextListener \u7684contextInitialized\u548ccontextDestroyed\u65b9\u6cd5\n<\/p>\n<p>\n\t\u6211\u4eec\u770b\u4e0b\u7c7b\u56fe\u7ed3\u6784\uff0c\u53ea\u5217\u51fa\u4e00\u90e8\u5206\u5c5e\u6027\u548c\u65b9\u6cd5\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"ContextLoaderListener \u7c7b\u56fe\" src=\"http:\/\/img.blog.csdn.net\/20140208153557312\" style=\"width: 764px;height: 348px\" \/>\n<\/p>\n<p>\n\tContextLoaderListener\u76d1\u542c\u5668\u7684\u4f5c\u7528\u5c31\u662f\u542f\u52a8Web\u5bb9\u5668\u65f6\uff0c\u81ea\u52a8\u88c5\u914dApplicationContext\u7684\u914d\u7f6e\u4fe1\u606f\u3002\u56e0\u4e3a\u5b83\u5b9e\u73b0\u4e86ServletContextListener\u8fd9\u4e2a\u63a5\u53e3\uff0c\u5728web.xml\u914d\u7f6e\u8fd9\u4e2a\u76d1\u542c\u5668\uff0c\u542f\u52a8\u5bb9\u5668\u65f6\uff0c\u5c31\u4f1a\u9ed8\u8ba4\u6267\u884c\u5b83\u5b9e\u73b0\u7684\u65b9\u6cd5\u3002\u81f3\u4e8eApplicationContext.xml\u8fd9\u4e2a\u914d\u7f6e\u6587\u4ef6\u90e8\u7f72\u5728\u54ea\uff0c\u5982\u4f55\u914d\u7f6e\u591a\u4e2axml\u6587\u4ef6\uff0c\u4e66\u4e0a\u90fd\u6ca1\u600e\u4e48\u8be6\u7ec6\u8bf4\u660e\u3002\u73b0\u5728\u7684\u65b9\u6cd5\u5c31\u662f\u67e5\u770b\u5b83\u7684API\u6587\u6863\u3002\u5728ContextLoaderListener\u4e2d\u5173\u8054\u4e86ContextLoader\u8fd9\u4e2a\u7c7b\uff0c\u6240\u4ee5\u6574\u4e2a\u52a0\u8f7d\u914d\u7f6e\u8fc7\u7a0b\u7531ContextLoader\u6765\u5b8c\u6210\u3002\u5b83\u7684API\u8bf4\u660e\u5982\u4e0b\uff1a\n<\/p>\n<p>\n\t\u7b2c\u4e00\u6bb5\u8bf4\u660eContextLoader\u53ef\u4ee5\u7531 ContextLoaderListener\u548cContextLoaderServlet\u751f\u6210\u3002\u5982\u679c\u67e5\u770bContextLoaderServlet\u7684API\uff0c\u53ef\u4ee5\u770b\u5230\u5b83\u4e5f\u5173\u8054\u4e86ContextLoader\u8fd9\u4e2a\u7c7b\u800c\u4e14\u5b83\u5b9e\u73b0\u4e86HttpServlet\u8fd9\u4e2a\u63a5\u53e3\u3002\n<\/p>\n<p>\n\t\u7b2c\u4e8c\u6bb5\uff0cContextLoader\u521b\u5efa\u7684\u662f XmlWebApplicationContext\u8fd9\u6837\u4e00\u4e2a\u7c7b\uff0c\u5b83\u5b9e\u73b0\u7684\u63a5\u53e3\u662fWebApplicationContext-&gt;ConfigurableWebApplicationContext-&gt;ApplicationContext-&gt;BeanFactory\u8fd9\u6837\u4e00\u6765spring\u4e2d\u7684\u6240\u6709bean\u90fd\u7531\u8fd9\u4e2a\u7c7b\u6765\u521b\u5efa\n<\/p>\n<p>\n\t\u7b2c\u4e09\u6bb5,\u8bb2\u5982\u4f55\u90e8\u7f72applicationContext\u7684xml\u6587\u4ef6\u3002<br \/>\n\t\u5982\u679c\u5728web.xml\u4e2d\u4e0d\u5199\u4efb\u4f55\u53c2\u6570\u914d\u7f6e\u4fe1\u606f\uff0c\u9ed8\u8ba4\u7684\u8def\u5f84\u662f\/WEB-INF\/applicationContext.xml\uff0c\u5728WEB-INF\u76ee\u5f55\u4e0b\u521b\u5efa\u7684xml\u6587\u4ef6\u7684\u540d\u79f0\u5fc5\u987b\u662fapplicationContext.xml\uff1b<br \/>\n\t\u5982\u679c\u662f\u8981\u81ea\u5b9a\u4e49\u6587\u4ef6\u540d\u53ef\u4ee5\u5728web.xml\u91cc\u52a0\u5165contextConfigLocation\u8fd9\u4e2acontext\u53c2\u6570\uff1a\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;context-param&gt;  \r\n\t&lt;param-name&gt;contextConfigLocation&lt;\/param-name&gt;  \r\n\t&lt;param-value&gt;  \r\n\t\t\/WEB-INF\/classes\/applicationContext-*.xml   \r\n\t&lt;\/param-value&gt;  \r\n&lt;\/context-param&gt;<\/pre>\n<p>\n\t\u5728&lt;param-value&gt; &lt;\/param-value&gt;\u91cc\u6307\u5b9a\u76f8\u5e94\u7684xml\u6587\u4ef6\u540d\uff0c\u5982\u679c\u6709\u591a\u4e2axml\u6587\u4ef6\uff0c\u53ef\u4ee5\u5199\u5728\u4e00\u8d77\u5e76\u4e00&ldquo;,&rdquo;\u53f7\u5206\u9694\u3002\u4e0a\u9762\u7684applicationContext-*.xml\u91c7\u7528\u901a\u914d\u7b26\uff0c\u6bd4\u5982\u8fd9\u90a3\u4e2a\u76ee\u5f55\u4e0b\u6709applicationContext-ibatis-base.xml\uff0capplicationContext-action.xml\uff0capplicationContext-ibatis-dao.xml\u7b49\u6587\u4ef6\uff0c\u90fd\u4f1a\u4e00\u540c\u88ab\u8f7d\u5165\u3002\n<\/p>\n<p>\n\t\u7531\u6b64\u53ef\u89c1applicationContext.xml\u7684\u6587\u4ef6\u4f4d\u7f6e\u5c31\u53ef\u4ee5\u6709\u4e24\u79cd\u9ed8\u8ba4\u5b9e\u73b0\uff1a\n<\/p>\n<p>\n\t\u7b2c\u4e00\u79cd\uff1a\u76f4\u63a5\u5c06\u4e4b\u653e\u5230\/WEB-INF\u4e0b\uff0c\u4e4b\u5728web.xml\u4e2d\u58f0\u660e\u4e00\u4e2alistener\uff1b\n<\/p>\n<p>\n\t\u7b2c\u4e8c\u79cd\uff1a\u5c06\u4e4b\u653e\u5230classpath\u4e0b\uff0c\u4f46\u662f\u6b64\u65f6\u8981\u5728web.xml\u4e2d\u52a0\u5165&lt;context-param&gt;\uff0c\u7528\u5b83\u6765\u6307\u660e\u4f60\u7684applicationContext.xml\u7684\u4f4d\u7f6e\u4ee5\u4f9bweb\u5bb9\u5668\u6765\u52a0\u8f7d\u3002\u6309\u7167Struts2 \u6574\u5408spring\u7684\u5b98\u65b9\u7ed9\u51fa\u7684\u6863\u6848\uff0c\u5199\u6210\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;\/WEB-INF\/applicationContext-*.xml,classpath*:applicationContext-*.xml&lt;\/param-value&gt; \r\n&lt;\/context-param&gt;\r\n<\/pre>\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=1146\">\u8be6\u89e3 Spring \u4e2d ContextLoaderListener \u7684\u4f5c\u7528<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u516c\u53f8\u6765\u4e86\u65b0\u540c\u4e8b\uff0c\u521a\u6bd5\u4e1a\u4e0d\u4e45\u3002\u95ee\u5230\u6211 web.xml \u4e2d\u7684&nbsp;ContextLoaderListen [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[896,898,69,551,897],"class_list":["post-1146","post","type-post","status-publish","format-standard","hentry","category-java","tag-contextloaderlistener","tag-dispatcherservlet","tag-java","tag-spring","tag-web-xml"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1146","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=1146"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1146\/revisions"}],"predecessor-version":[{"id":1147,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1146\/revisions\/1147"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}