{"id":11916,"date":"2023-11-28T22:37:43","date_gmt":"2023-11-28T14:37:43","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=11916"},"modified":"2023-12-11T14:40:19","modified_gmt":"2023-12-11T06:40:19","slug":"spring-zhongwebapp1icationcontext-de-zuo-yong-shi","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/spring-zhongwebapp1icationcontext-de-zuo-yong-shi\/","title":{"rendered":"Spring\u4e2dWebApp1icationContext\u7684\u4f5c\u7528\u662f\uff1f"},"content":{"rendered":"<p><code>WebApplicationContext<\/code> \u662f Spring \u6846\u67b6\u4e2d\u7684\u4e00\u4e2a\u7279\u6b8a\u7684\u5e94\u7528\u4e0a\u4e0b\u6587\uff08ApplicationContext\uff09\uff0c\u4e13\u95e8\u7528\u4e8e Web \u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u3002\u5b83\u662f <code>ApplicationContext<\/code> \u7684\u5b50\u63a5\u53e3\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e9b\u989d\u5916\u7684\u529f\u80fd\u548c\u914d\u7f6e\u9009\u9879\uff0c\u4ee5\u652f\u6301\u5728 Web \u73af\u5883\u4e2d\u7684\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u3002<\/p>\n<p>\u4e3b\u8981\u4f5c\u7528\u5305\u62ec\uff1a<\/p>\n<ol>\n<li>\n<strong>Web \u7279\u5b9a\u7684 Bean \u5b9a\u4e49\uff1a<\/strong> <code>WebApplicationContext<\/code> \u652f\u6301\u5728 Spring \u914d\u7f6e\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e0e Web \u76f8\u5173\u7684 Bean\uff0c\u4f8b\u5982\u63a7\u5236\u5668\uff08Controller\uff09\u3001\u89c6\u56fe\u89e3\u6790\u5668\uff08ViewResolver\uff09\u3001\u5904\u7406\u5668\u6620\u5c04\uff08HandlerMapping\uff09\u7b49\u3002\n<\/li>\n<li>\n<strong>\u652f\u6301\u56fd\u9645\u5316\u548c\u4e3b\u9898\uff1a<\/strong> <code>WebApplicationContext<\/code> \u63d0\u4f9b\u4e86\u4e0e Web \u76f8\u5173\u7684\u56fd\u9645\u5316\uff08Internationalization\uff09\u548c\u4e3b\u9898\uff08Theme\uff09\u652f\u6301\uff0c\u4f7f\u5f97\u5728 Web \u5e94\u7528\u4e2d\u66f4\u5bb9\u6613\u5730\u5b9e\u73b0\u591a\u8bed\u8a00\u548c\u4e3b\u9898\u5207\u6362\u7684\u529f\u80fd\u3002\n<\/li>\n<li>\n<strong>Servlet API \u96c6\u6210\uff1a<\/strong> <code>WebApplicationContext<\/code> \u80fd\u591f\u4e0e Servlet API \u8fdb\u884c\u96c6\u6210\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u83b7\u53d6 ServletContext\u3001HttpServletRequest\u3001HttpServletResponse \u7b49\u5bf9\u8c61\uff0c\u4ee5\u4fbf\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u8fdb\u884c\u4e0e Web \u76f8\u5173\u7684\u64cd\u4f5c\u3002\n<\/li>\n<li>\n<strong>\u4e8b\u4ef6\u4f20\u64ad\uff1a<\/strong> <code>WebApplicationContext<\/code> \u901a\u8fc7\u5b9e\u73b0 <code>ApplicationEventPublisher<\/code> \u63a5\u53e3\uff0c\u652f\u6301\u5728 Web \u5e94\u7528\u4e2d\u53d1\u5e03\u548c\u76d1\u542c\u5e94\u7528\u4e8b\u4ef6\u3002\n<\/li>\n<li>\n<strong>Web \u4f5c\u7528\u57df Bean\uff1a<\/strong> <code>WebApplicationContext<\/code> \u652f\u6301\u7279\u5b9a\u4e8e Web \u7684\u4f5c\u7528\u57df\uff0c\u5982\u8bf7\u6c42\uff08Request\uff09\u3001\u4f1a\u8bdd\uff08Session\uff09\u3001\u5168\u5c40\u4f1a\u8bdd\uff08Global Session\uff09\u7b49\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>@RequestScope<\/code>\u3001<code>@SessionScope<\/code>\u3001<code>@ApplicationScope<\/code> \u7b49\u6ce8\u89e3\u5b9a\u4e49\u76f8\u5e94\u7684 Bean\u3002\n<\/li>\n<\/ol>\n<p>\u5728\u5178\u578b\u7684 Spring MVC \u5e94\u7528\u4e2d\uff0c\u901a\u5e38\u4f1a\u914d\u7f6e\u4e00\u4e2a <code>WebApplicationContext<\/code> \u6765\u7ba1\u7406 Web \u76f8\u5173\u7684\u7ec4\u4ef6\uff0c\u800c\u8fd9\u4e2a <code>WebApplicationContext<\/code> \u53ef\u80fd\u662f\u7531 <code>DispatcherServlet<\/code> \u8d1f\u8d23\u521d\u59cb\u5316\u548c\u7ba1\u7406\u7684\u3002\u5728 Spring Boot \u4e2d\uff0c\u4f7f\u7528 <code>SpringApplication<\/code> \u521b\u5efa\u7684\u5e94\u7528\u901a\u5e38\u5df2\u7ecf\u5305\u542b\u4e86\u4e00\u4e2a <code>WebApplicationContext<\/code>\u3002<\/p>\n<pre><code class=\"language-java\">\/\/ \u5178\u578b\u7684Spring MVC\u914d\u7f6e\n@Configuration\n@EnableWebMvc\npublic class WebConfig extends WebMvcConfigurerAdapter {\n    \/\/ \u914d\u7f6e\u4e0eWeb\u76f8\u5173\u7684\u7ec4\u4ef6\uff0c\u5982\u63a7\u5236\u5668\u3001\u89c6\u56fe\u89e3\u6790\u5668\u7b49\n}\n<\/code><\/pre>\n<p>\u603b\u4f53\u800c\u8a00\uff0c<code>WebApplicationContext<\/code> \u662f\u4e3a Web \u5e94\u7528\u63d0\u4f9b\u4e13\u95e8\u652f\u6301\u7684\u4e0a\u4e0b\u6587\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e9b\u4e0e Web \u5f00\u53d1\u76f8\u5173\u7684\u7279\u6027\u548c\u529f\u80fd\uff0c\u4f7f\u5f97\u5728 Web \u73af\u5883\u4e2d\u66f4\u52a0\u65b9\u4fbf\u5730\u4f7f\u7528 Spring \u6846\u67b6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>WebApplicationContext \u662f Spring \u6846\u67b6\u4e2d\u7684\u4e00\u4e2a\u7279\u6b8a\u7684\u5e94\u7528\u4e0a\u4e0b\u6587\uff08Applicati [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[],"class_list":["post-11916","post","type-post","status-publish","format-standard","hentry","category-spring"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11916","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=11916"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11916\/revisions"}],"predecessor-version":[{"id":37204,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11916\/revisions\/37204"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=11916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=11916"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=11916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}