{"id":1387,"date":"2024-01-30T00:26:15","date_gmt":"2024-01-29T16:26:15","guid":{"rendered":"http:\/\/zyg.luchers.cn\/?p=1387"},"modified":"2024-01-30T00:31:21","modified_gmt":"2024-01-29T16:31:21","slug":"spring%e4%b9%8b%e5%ae%b9%e5%99%a8%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"http:\/\/zyg.luchers.cn\/index.php\/2024\/01\/30\/spring%e4%b9%8b%e5%ae%b9%e5%99%a8%e4%bb%8b%e7%bb%8d\/","title":{"rendered":"spring\u4e4b\u5bb9\u5668\u4ecb\u7ecd"},"content":{"rendered":"<h2>3\u3001\u5bb9\u5668\uff1aIoC<\/h2>\n<p>IoC \u662f Inversion of Control \u7684\u7b80\u5199\uff0c\u8bd1\u4e3a\u201c\u63a7\u5236\u53cd\u8f6c\u201d\uff0c\u5b83\u4e0d\u662f\u4e00\u95e8\u6280\u672f\uff0c\u800c\u662f\u4e00\u79cd\u8bbe\u8ba1\u601d\u60f3\uff0c\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u6cd5\u5219\uff0c\u80fd\u591f\u6307\u5bfc\u6211\u4eec\u5982\u4f55\u8bbe\u8ba1\u51fa\u677e\u8026\u5408\u3001\u66f4\u4f18\u826f\u7684\u7a0b\u5e8f\u3002<\/p>\n<p>Spring \u901a\u8fc7 IoC \u5bb9\u5668\u6765\u7ba1\u7406\u6240\u6709 Java \u5bf9\u8c61\u7684\u5b9e\u4f8b\u5316\u548c\u521d\u59cb\u5316\uff0c\u63a7\u5236\u5bf9\u8c61\u4e0e\u5bf9\u8c61\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\u3002\u6211\u4eec\u5c06\u7531 IoC \u5bb9\u5668\u7ba1\u7406\u7684 Java \u5bf9\u8c61\u79f0\u4e3a Spring Bean\uff0c\u5b83\u4e0e\u4f7f\u7528\u5173\u952e\u5b57 new \u521b\u5efa\u7684 Java \u5bf9\u8c61\u6ca1\u6709\u4efb\u4f55\u533a\u522b\u3002<\/p>\n<p>IoC \u5bb9\u5668\u662f Spring \u6846\u67b6\u4e2d\u6700\u91cd\u8981\u7684\u6838\u5fc3\u7ec4\u4ef6\u4e4b\u4e00\uff0c\u5b83\u8d2f\u7a7f\u4e86 Spring \u4ece\u8bde\u751f\u5230\u6210\u957f\u7684\u6574\u4e2a\u8fc7\u7a0b\u3002<\/p>\n<h3>3.1\u3001IoC\u5bb9\u5668<\/h3>\n<h4>3.1.1\u3001\u63a7\u5236\u53cd\u8f6c\uff08IoC\uff09<\/h4>\n<ul>\n<li>\n<p>\u63a7\u5236\u53cd\u8f6c\u662f\u4e00\u79cd\u601d\u60f3\u3002<\/p>\n<\/li>\n<li>\n<p>\u63a7\u5236\u53cd\u8f6c\u662f\u4e3a\u4e86\u964d\u4f4e\u7a0b\u5e8f\u8026\u5408\u5ea6\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u6269\u5c55\u529b\u3002<\/p>\n<\/li>\n<li>\n<p>\u63a7\u5236\u53cd\u8f6c\uff0c\u53cd\u8f6c\u7684\u662f\u4ec0\u4e48\uff1f<\/p>\n<\/li>\n<li>\n<ul>\n<li>\u5c06\u5bf9\u8c61\u7684\u521b\u5efa\u6743\u5229\u4ea4\u51fa\u53bb\uff0c\u4ea4\u7ed9\u7b2c\u4e09\u65b9\u5bb9\u5668\u8d1f\u8d23\u3002<\/li>\n<li>\u5c06\u5bf9\u8c61\u548c\u5bf9\u8c61\u4e4b\u95f4\u5173\u7cfb\u7684\u7ef4\u62a4\u6743\u4ea4\u51fa\u53bb\uff0c\u4ea4\u7ed9\u7b2c\u4e09\u65b9\u5bb9\u5668\u8d1f\u8d23\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u63a7\u5236\u53cd\u8f6c\u8fd9\u79cd\u601d\u60f3\u5982\u4f55\u5b9e\u73b0\u5462\uff1f<\/p>\n<\/li>\n<li>\n<ul>\n<li>DI\uff08Dependency Injection\uff09\uff1a\u4f9d\u8d56\u6ce8\u5165<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>3.1.2\u3001\u4f9d\u8d56\u6ce8\u5165<\/h4>\n<p>DI\uff08Dependency Injection\uff09\uff1a\u4f9d\u8d56\u6ce8\u5165\uff0c\u4f9d\u8d56\u6ce8\u5165\u5b9e\u73b0\u4e86\u63a7\u5236\u53cd\u8f6c\u7684\u601d\u60f3\u3002<\/p>\n<p><strong>\u4f9d\u8d56\u6ce8\u5165\uff1a<\/strong><\/p>\n<ul>\n<li><strong>\u6307Spring\u521b\u5efa\u5bf9\u8c61\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5c06\u5bf9\u8c61\u4f9d\u8d56\u5c5e\u6027\u901a\u8fc7\u914d\u7f6e\u8fdb\u884c\u6ce8\u5165<\/strong><\/li>\n<\/ul>\n<p>\u4f9d\u8d56\u6ce8\u5165\u5e38\u89c1\u7684\u5b9e\u73b0\u65b9\u5f0f\u5305\u62ec\u4e24\u79cd\uff1a<\/p>\n<ul>\n<li>\u7b2c\u4e00\u79cd\uff1aset\u6ce8\u5165<\/li>\n<li>\u7b2c\u4e8c\u79cd\uff1a\u6784\u9020\u6ce8\u5165<\/li>\n<\/ul>\n<p>\u6240\u4ee5\u7ed3\u8bba\u662f\uff1aIOC \u5c31\u662f\u4e00\u79cd\u63a7\u5236\u53cd\u8f6c\u7684\u601d\u60f3\uff0c \u800c DI \u662f\u5bf9IoC\u7684\u4e00\u79cd\u5177\u4f53\u5b9e\u73b0\u3002<\/p>\n<p><strong>Bean\u7ba1\u7406\u8bf4\u7684\u662f\uff1aBean\u5bf9\u8c61\u7684\u521b\u5efa\uff0c\u4ee5\u53caBean\u5bf9\u8c61\u4e2d\u5c5e\u6027\u7684\u8d4b\u503c\uff08\u6216\u8005\u53eb\u505aBean\u5bf9\u8c61\u4e4b\u95f4\u5173\u7cfb\u7684\u7ef4\u62a4\uff09\u3002<\/strong><\/p>\n<h4>3.1.3\u3001IoC\u5bb9\u5668\u5728Spring\u7684\u5b9e\u73b0<\/h4>\n<p>Spring \u7684 IoC \u5bb9\u5668\u5c31\u662f IoC\u601d\u60f3\u7684\u4e00\u4e2a\u843d\u5730\u7684\u4ea7\u54c1\u5b9e\u73b0\u3002IoC\u5bb9\u5668\u4e2d\u7ba1\u7406\u7684\u7ec4\u4ef6\u4e5f\u53eb\u505a bean\u3002\u5728\u521b\u5efa bean \u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u521b\u5efaIoC \u5bb9\u5668\u3002Spring \u63d0\u4f9b\u4e86IoC \u5bb9\u5668\u7684\u4e24\u79cd\u5b9e\u73b0\u65b9\u5f0f\uff1a<\/p>\n<p><strong>\u2460BeanFactory<\/strong><\/p>\n<p>\u8fd9\u662f IoC \u5bb9\u5668\u7684\u57fa\u672c\u5b9e\u73b0\uff0c\u662f Spring \u5185\u90e8\u4f7f\u7528\u7684\u63a5\u53e3\u3002\u9762\u5411 Spring \u672c\u8eab\uff0c\u4e0d\u63d0\u4f9b\u7ed9\u5f00\u53d1\u4eba\u5458\u4f7f\u7528\u3002<\/p>\n<p><strong>\u2461ApplicationContext<\/strong><\/p>\n<p>BeanFactory \u7684\u5b50\u63a5\u53e3\uff0c\u63d0\u4f9b\u4e86\u66f4\u591a\u9ad8\u7ea7\u7279\u6027\u3002\u9762\u5411 Spring \u7684\u4f7f\u7528\u8005\uff0c\u51e0\u4e4e\u6240\u6709\u573a\u5408\u90fd\u4f7f\u7528 ApplicationContext \u800c\u4e0d\u662f\u5e95\u5c42\u7684 BeanFactory\u3002<\/p>\n<p><strong>\u2462ApplicationContext\u7684\u4e3b\u8981\u5b9e\u73b0\u7c7b<\/strong><\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/01\/img005-1-167x300.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/01\/img005-1-167x300.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<table>\n<thead>\n<tr>\n<th>\u7c7b\u578b\u540d<\/th>\n<th>\u7b80\u4ecb<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>ClassPathXmlApplicationContext<\/td>\n<td>\u901a\u8fc7\u8bfb\u53d6\u7c7b\u8def\u5f84\u4e0b\u7684 XML \u683c\u5f0f\u7684\u914d\u7f6e\u6587\u4ef6\u521b\u5efa IOC \u5bb9\u5668\u5bf9\u8c61<\/td>\n<\/tr>\n<tr>\n<td>FileSystemXmlApplicationContext<\/td>\n<td>\u901a\u8fc7\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u8bfb\u53d6 XML \u683c\u5f0f\u7684\u914d\u7f6e\u6587\u4ef6\u521b\u5efa IOC \u5bb9\u5668\u5bf9\u8c61<\/td>\n<\/tr>\n<tr>\n<td>ConfigurableApplicationContext<\/td>\n<td>ApplicationContext \u7684\u5b50\u63a5\u53e3\uff0c\u5305\u542b\u4e00\u4e9b\u6269\u5c55\u65b9\u6cd5 refresh() \u548c close() \uff0c\u8ba9 ApplicationContext \u5177\u6709\u542f\u52a8\u3001\u5173\u95ed\u548c\u5237\u65b0\u4e0a\u4e0b\u6587\u7684\u80fd\u529b\u3002<\/td>\n<\/tr>\n<tr>\n<td>WebApplicationContext<\/td>\n<td>\u4e13\u95e8\u4e3a Web \u5e94\u7528\u51c6\u5907\uff0c\u57fa\u4e8e Web \u73af\u5883\u521b\u5efa IOC \u5bb9\u5668\u5bf9\u8c61\uff0c\u5e76\u5c06\u5bf9\u8c61\u5f15\u5165\u5b58\u5165 ServletContext \u57df\u4e2d\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>3\u3001\u5bb9\u5668\uff1aIoC IoC \u662f Inversion of Control \u7684\u7b80\u5199\uff0c\u8bd1\u4e3a\u201c\u63a7\u5236\u53cd\u8f6c\u201d\uff0c\u5b83\u4e0d\u662f\u4e00\u95e8\u6280 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-1387","post","type-post","status-publish","format-standard","hentry","category-spring-spring"],"_links":{"self":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/1387","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1387"}],"version-history":[{"count":0,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/1387\/revisions"}],"wp:attachment":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1387"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}