
{"id":2545,"date":"2020-11-29T13:59:06","date_gmt":"2020-11-29T05:59:06","guid":{"rendered":"http:\/\/127.0.0.2\/?p=2545"},"modified":"2020-12-24T16:01:52","modified_gmt":"2020-12-24T08:01:52","slug":"2020javaweb%e6%a1%86%e6%9e%b6%e6%95%99%e7%a8%8b%e4%b9%8bspring%e7%9a%84ioc%e4%b8%8edi%e4%bb%a5%e5%8f%8a%e6%b3%a8%e5%85%a5","status":"publish","type":"post","link":"https:\/\/www.9713job.com\/?p=2545","title":{"rendered":"2020javaweb\u6846\u67b6\u6559\u7a0b\u4e4bspring\u7684IOC\u4e0eDI\u4ee5\u53ca\u6ce8\u5165"},"content":{"rendered":"<h3>2020javaweb\u6846\u67b6\u6559\u7a0b\u4e4bspring\u7684IOC\u4e0eDI\u4ee5\u53ca\u6ce8\u5165<\/h3>\n<p><span style=\"color: #ff0000;\"><strong>IOC<\/strong><\/span><\/p>\n<p>Inverse Of Controll\uff1a\u63a7\u5236\u53cd\u8f6c<\/p>\n<p>\u53cd\u8f6c\u4e86\u4f9d\u8d56\u5173\u7cfb\u7684\u6ee1\u2f9c\u2f45\u5f0f\uff0c\u7531\u4e4b\u524d\u7684\u2f83\u2f30\u521b\u5efa\u4f9d\u8d56\u5bf9\u8c61\uff0c\u53d8\u4e3a\u7531\u2f2f\u2f1a\u63a8\u9001\u3002(\u53d8\u4e3b\u52a8\u4e3a\u88ab\u52a8\uff0c\u5373\u53cd\u8f6c)\u3002<br \/>\n\u89e3\u51b3\u4e86\u5177\u6709\u4f9d\u8d56\u5173\u7cfb\u7684\u7ec4\u4ef6\u4e4b\u95f4\u7684\u5f3a\u8026\u5408\uff0c\u4f7f\u5f97\u9879\u2f6c\u5f62\u6001\u66f4\u52a0\u7a33\u5065<\/p>\n<p><span style=\"color: #ff0000;\"><strong>DI<\/strong><\/span><\/p>\n<p>Dependency Injection \uff1a\u4f9d\u8d56\u6ce8\u2f0a<\/p>\n<p>\u5168\u65b0\u7684\u4f9d\u8d56\u6ee1\u2f9c\u2f45\u5f0f\uff0c\u4f53\u73b0\u5728\u7f16\u7801\u4e2d\u5c31\u662f\u5168\u65b0\u7684\u8d4b\u503c\u2f45\u5f0f ==&gt; \u5728\u2f2f\u2f1a\u4e2d\u4e3a\u5c5e\u6027\u63a8\u9001\u503c<\/p>\n<p>\u5982\uff1a &lt;property name=&#8221;userDAO&#8221; ref=&#8221;userDAO&#8221;&gt;&lt;\/property&gt;<\/p>\n<p><span style=\"color: #ff0000;\"><strong>IOC\u4e0eDI<\/strong><\/span><\/p>\n<p>\u5728spring\u4e2d\u5173\u4e8eIOC\u548cDI\u7684\u63cf\u8ff0\u662f\u8fd9\u6837\u7684: IOC(DI) \uff0c\u5373\uff0c\u662f\u2f00\u7801\u4e8b<br \/>\nIOC \u662f\u601d\u60f3 \uff1a\u6307\u5bfc\u6211\u4eec\u5728\u6ee1\u2f9c\u4f9d\u8d56\u65f6\uff0c\u5e94\u8be5\u6709\u53cd\u8f6c\u7684\u8bbe\u8ba1\u3002<br \/>\nDI \u662f\u2f3f\u6bb5 \uff1a\u5b9e\u9645\u64cd\u4f5c\u65f6\uff0c\u5c31\u662f\u5728\u2f00\u6b21\u6b21\u7684 \u6ce8\u2f0a<br \/>\n<!--more--><\/p>\n<p><span style=\"color: #ff0000;\"><strong>DI\u7684\u914d\u7f6e\u4f7f\u7528<\/strong><\/span><\/p>\n<p><span style=\"color: #ff0000;\">1.DI\u7684\u65b9\u5f0f<\/span><\/p>\n<p>set\u6ce8\u5165\uff1a\u501f\u52a9set\u65b9\u6cd5\u5b8c\u6210\u6ce8\u5165<\/p>\n<p>\u6784\u9020\u6ce8\u5165\uff1a\u501f\u52a9\u6784\u9020\u65b9\u6cd5\u5b8c\u6210\u6ce8\u5165<\/p>\n<p>\u81ea\u52a8\u6ce8\u5165\uff1aspring\u81ea\u52a8\u8bc6\u522b\u5c5e\u6027\uff0c\u5e76\u6ce8\u5165<\/p>\n<p><span style=\"color: #ff0000;\">2.set\u6ce8\u5165<\/span><\/p>\n<p>DI\u7c7b\uff0c\u5c5e\u6027\uff1a<\/p>\n<pre>private Integer age;\r\nprivate String name;\r\nprivate Boolean gender;\r\nprivate UsersDao usersDao;\r\nprivate List&lt;Object&gt; list;\r\nprivate Set&lt;String&gt; set;\r\nprivate Map&lt;String,Object&gt; map;\r\nprivate Properties properties;<\/pre>\n<pre>&lt;bean id=\"di04\" class=\"bean.DI\"&gt;\r\n    &lt;!--  jdk8\u4e2d\u57fa\u672c\u7c7b\u578b--&gt;\r\n    &lt;property name=\"age\" value=\"18\"\/&gt;\r\n    &lt;property name=\"name\" value=\"zhangsan\"\/&gt;\r\n    &lt;property name=\"gender\" value=\"false\"\/&gt;\r\n    &lt;!-- \u5f15\u7528\u7c7b\u578b --&gt;\r\n    &lt;property name=\"usersDao\" ref=\"userDAO\"\/&gt;\r\n    &lt;!-- List \u6216 \u6570\u7ec4 --&gt;\r\n    &lt;property name=\"list\"&gt;\r\n        &lt;list&gt;\r\n            &lt;value&gt;18&lt;\/value&gt;\r\n            &lt;ref bean=\"userDAO\"\/&gt;\r\n        &lt;\/list&gt;\r\n    &lt;\/property&gt;\r\n    &lt;!-- set\u96c6\u5408 --&gt;\r\n    &lt;property name=\"set\"&gt;\r\n        &lt;set&gt;\r\n            &lt;value&gt;\"admin\"&lt;\/value&gt;\r\n        &lt;\/set&gt;\r\n    &lt;\/property&gt;\r\n    &lt;!-- map \u96c6\u5408 --&gt;\r\n    &lt;property name=\"map\"&gt;\r\n        &lt;map&gt;\r\n            &lt;entry key=\"name\" value=\"admin\"\/&gt;\r\n            &lt;entry key=\"userDAO\" value-ref=\"userDAO\"\/&gt;\r\n        &lt;\/map&gt;\r\n    &lt;\/property&gt;\r\n    &lt;!-- properties --&gt;\r\n    &lt;property name=\"properties\"&gt;\r\n        &lt;props&gt;\r\n            &lt;prop key=\"url\"&gt;jdbc:mysql&lt;\/prop&gt;\r\n        &lt;\/props&gt;\r\n    &lt;\/property&gt;\r\n&lt;\/bean&gt;<\/pre>\n<p>&lt;!&#8211;<br \/>\n\u6ce8\u610f\uff1a\u5728\u6ce8\u2f0a\u8fc7\u7a0b\u4e2d\uff0c\u5176\u5b9e\u5c31\u662f\u2f00\u4e2a\u590d\u5236\u7684\u8fc7\u7a0b\uff0c a=b, a\u662f\u7ec4\u4ef6\u7684\u5c5e\u6027\uff0cb\u662f\u5982\u4e0a\u914d\u7f6e\u7684value\u4e2d\u7684\u503c\u3002<br \/>\n\u5219b\u662fString\uff0c\u2f7da\u53ef\u80fd\u662f Integer,Boolean,Double,Date,&#8230;.\uff1b\u5373\u5de6\u503c\u548c\u53f3\u503c\u7c7b\u578b\u4e0d\u5339\u914d\u3002<br \/>\nspring\u6709\u2f83\u52a8\u7684\u7c7b\u578b\u8f6c\u6362\u673a\u5236\uff0c\u53ef\u4ee5\u4fdd\u8bc1\u6211\u4eec\u7edd\u2f24\u591a\u6570\u7684\u7c7b\u578b\u8f6c\u6362\u3002<br \/>\n\u7ec6\u8282\uff1a\u201corg.springframework.beans.propertyeidtors\u201d\u5305\u4e0b\u5b9a\u4e49\u4e86\u2f24\u91cf\u7684Editor\u3002<br \/>\n&#8211;&gt;<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-2254\" src=\"\/wp-content\/uploads\/2020\/11\/29\/1.png\" alt=\"\" width=\"680\" \/><\/p>\n<p><strong><span style=\"color: #ff0000;\">\u6784\u9020\u548cset\u6ce8\u5165\u6df7\u7528<\/span><br \/>\n<\/strong><\/p>\n<p>\u5b9e\u4f53\u7c7b\u589e\u52a0\u4e00\u4e2a\u6784\u9020\u65b9\u6cd5\u4ee5\u53caget\/set\u65b9\u6cd5<\/p>\n<pre>&lt;bean id=\"UsersId\" class=\"bean.Users\"&gt;\r\n    &lt;!-- \u6784\u9020\u548cset\u6ce8\u5165\u6df7\u7528--&gt;\r\n    &lt;constructor-arg index=\"0\" type=\"java.lang.Integer\" value=\"4\"\/&gt;\r\n    &lt;property name=\"name\" value=\"zhangsan\"\/&gt;\r\n    &lt;property name=\"gender\" value=\"false\"\/&gt;\r\n&lt;\/bean&gt;<\/pre>\n<p><strong> \u5de5\u5382\uff1a<br \/>\n<\/strong><\/p>\n<pre>@Test\r\npublic void factoryBoot2(){\r\n    ApplicationContext context=new ClassPathXmlApplicationContext(\"applicationContext.xml\");\r\n    Users userId = context.getBean(\"UsersId\", Users.class);\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\"><strong>\u81ea\u52a8\u6ce8\u5165<\/strong><\/span><\/p>\n<p>autowire\u81ea\u52a8\u88c5\u8f7d<\/p>\n<p>byType\u5c06\u5de5\u5382\u4e2d\u548c\u5c5e\u6027\u7c7b\u578b\u4e00\u81f4\u7684\u7ec4\u4ef6\uff0c\u8d4b\u503c\u7ed9\u5bf9\u5e94\u7c7b\u578b<\/p>\n<p>byName\u5c06\u5de5\u5382\u4e2d\u548c\u540c\u540d\u4e00\u81f4\u7684\u7ec4\u4ef6\uff0c\u8d4b\u503c\u7ed9\u5bf9\u5e94\u7c7b\u578b<\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u7c7b\uff0c\u79c1\u6709dao\u63a5\u53e3\u5e76\u6dfb\u52a0get\/set\u65b9\u6cd5\uff0c\u7136\u540e\u5728\u914d\u7f6e\u6587\u4ef6\u6dfb\u52a0\u81ea\u52a8\u6ce8\u5165<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-2254\" src=\"\/wp-content\/uploads\/2020\/11\/29\/2.png\" alt=\"\" width=\"680\" \/><\/p>\n<p>\u7136\u540e\u8fdb\u5de5\u5382\u6dfb\u52a0\u65ad\u70b9\uff0c\u8fdb\u5165debug\u89c6\u56fe<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-2254\" src=\"\/wp-content\/uploads\/2020\/11\/29\/3.png\" alt=\"\" width=\"680\" \/><\/p>\n<p>\u627e\u5230singletonObjects<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-2254\" src=\"\/wp-content\/uploads\/2020\/11\/29\/4.png\" alt=\"\" width=\"680\" \/><\/p>\n<p><strong><span style=\"color: #ff0000;\">\u57fa\u4e8e\u7c7b\u578b \u2f83\u52a8\u6ce8\u2f0a\u65f6\uff0c\u5982\u679c\u5b58\u5728\u591a\u4e2a\u6b64\u7c7b\u578b\u7684bean\uff0c\u4f1a\u62a5\u9519<\/span><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>2020javaweb\u6846\u67b6\u6559\u7a0b\u4e4bspring\u7684IOC\u4e0eDI\u4ee5\u53ca\u6ce8\u5165 IOC Inverse Of Contro [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-2545","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2545","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2545"}],"version-history":[{"count":5,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2545\/revisions"}],"predecessor-version":[{"id":2551,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2545\/revisions\/2551"}],"wp:attachment":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2545"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}