{"id":1389,"date":"2024-01-30T00:28:58","date_gmt":"2024-01-29T16:28:58","guid":{"rendered":"http:\/\/zyg.luchers.cn\/?p=1389"},"modified":"2024-01-30T00:33:56","modified_gmt":"2024-01-29T16:33:56","slug":"springioc%e5%ae%b9%e5%99%a8%e4%b9%8b%e5%9f%ba%e4%ba%8exml%e7%ae%a1%e7%90%86bean%ef%bc%88%e5%ae%9e%e9%aa%8c%e4%b8%80%ef%bc%89","status":"publish","type":"post","link":"http:\/\/zyg.luchers.cn\/index.php\/2024\/01\/30\/springioc%e5%ae%b9%e5%99%a8%e4%b9%8b%e5%9f%ba%e4%ba%8exml%e7%ae%a1%e7%90%86bean%ef%bc%88%e5%ae%9e%e9%aa%8c%e4%b8%80%ef%bc%89\/","title":{"rendered":"springIoC\u5bb9\u5668\u4e4b\u57fa\u4e8eXML\u7ba1\u7406Bean\uff08\u5b9e\u9a8c\u4e00\uff09"},"content":{"rendered":"<h4>\u5b9e\u9a8c\u4e00\uff1a\u83b7\u53d6bean<\/h4>\n<h5>\u2460\u65b9\u5f0f\u4e00\uff1a\u6839\u636eid\u83b7\u53d6<\/h5>\n<p>\u7531\u4e8e id \u5c5e\u6027\u6307\u5b9a\u4e86 bean \u7684\u552f\u4e00\u6807\u8bc6\uff0c\u6240\u4ee5\u6839\u636e bean \u6807\u7b7e\u7684 id \u5c5e\u6027\u53ef\u4ee5\u7cbe\u786e\u83b7\u53d6\u5230\u4e00\u4e2a\u7ec4\u4ef6\u5bf9\u8c61\u3002\u4e0a\u4e2a\u5b9e\u9a8c\u4e2d\u6211\u4eec\u4f7f\u7528\u7684\u5c31\u662f\u8fd9\u79cd\u65b9\u5f0f\u3002<\/p>\n<h5>\u2461\u65b9\u5f0f\u4e8c\uff1a\u6839\u636e\u7c7b\u578b\u83b7\u53d6<\/h5>\n<pre><code class=\"language-java\">@Test\npublic void testHelloWorld1(){\n    ApplicationContext ac = new ClassPathXmlApplicationContext(&quot;beans.xml&quot;);\n    HelloWorld bean = ac.getBean(HelloWorld.class);\n    bean.sayHello();\n}<\/code><\/pre>\n<h5>\u2462\u65b9\u5f0f\u4e09\uff1a\u6839\u636eid\u548c\u7c7b\u578b<\/h5>\n<pre><code class=\"language-java\">@Test\npublic void testHelloWorld2(){\n    ApplicationContext ac = new ClassPathXmlApplicationContext(&quot;beans.xml&quot;);\n    HelloWorld bean = ac.getBean(&quot;helloworld&quot;, HelloWorld.class);\n    bean.sayHello();\n}<\/code><\/pre>\n<h5>\u2463\u6ce8\u610f\u7684\u5730\u65b9<\/h5>\n<p>\u5f53\u6839\u636e\u7c7b\u578b\u83b7\u53d6bean\u65f6\uff0c\u8981\u6c42IOC\u5bb9\u5668\u4e2d\u6307\u5b9a\u7c7b\u578b\u7684bean\u6709\u4e14\u53ea\u80fd\u6709\u4e00\u4e2a<\/p>\n<p>\u5f53IOC\u5bb9\u5668\u4e2d\u4e00\u5171\u914d\u7f6e\u4e86\u4e24\u4e2a\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;bean id=&quot;helloworldOne&quot; class=&quot;com.atguigu.spring6.bean.HelloWorld&quot;&gt;&lt;\/bean&gt;\n&lt;bean id=&quot;helloworldTwo&quot; class=&quot;com.atguigu.spring6.bean.HelloWorld&quot;&gt;&lt;\/bean&gt;<\/code><\/pre>\n<p>\u6839\u636e\u7c7b\u578b\u83b7\u53d6\u65f6\u4f1a\u629b\u51fa\u5f02\u5e38\uff1a<\/p>\n<blockquote>\n<p>org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.atguigu.spring6.bean.HelloWorld' available: expected single matching bean but found 2: helloworldOne,helloworldTwo<\/p>\n<\/blockquote>\n<h5>\u2464\u6269\u5c55\u77e5\u8bc6<\/h5>\n<p>\u5982\u679c\u7ec4\u4ef6\u7c7b\u5b9e\u73b0\u4e86\u63a5\u53e3\uff0c\u6839\u636e\u63a5\u53e3\u7c7b\u578b\u53ef\u4ee5\u83b7\u53d6 bean \u5417\uff1f<\/p>\n<blockquote>\n<p>\u53ef\u4ee5\uff0c\u524d\u63d0\u662fbean\u552f\u4e00<\/p>\n<\/blockquote>\n<p>\u5982\u679c\u4e00\u4e2a\u63a5\u53e3\u6709\u591a\u4e2a\u5b9e\u73b0\u7c7b\uff0c\u8fd9\u4e9b\u5b9e\u73b0\u7c7b\u90fd\u914d\u7f6e\u4e86 bean\uff0c\u6839\u636e\u63a5\u53e3\u7c7b\u578b\u53ef\u4ee5\u83b7\u53d6 bean \u5417\uff1f<\/p>\n<blockquote>\n<p>\u4e0d\u884c\uff0c\u56e0\u4e3abean\u4e0d\u552f\u4e00<\/p>\n<\/blockquote>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u6839\u636e\u7c7b\u578b\u6765\u83b7\u53d6bean\u65f6\uff0c\u5728\u6ee1\u8db3bean\u552f\u4e00\u6027\u7684\u524d\u63d0\u4e0b\uff0c\u5176\u5b9e\u53ea\u662f\u770b\uff1a\u300e\u5bf9\u8c61 <strong>instanceof<\/strong> \u6307\u5b9a\u7684\u7c7b\u578b\u300f\u7684\u8fd4\u56de\u7ed3\u679c\uff0c\u53ea\u8981\u8fd4\u56de\u7684\u662ftrue\u5c31\u53ef\u4ee5\u8ba4\u5b9a\u4e3a\u548c\u7c7b\u578b\u5339\u914d\uff0c\u80fd\u591f\u83b7\u53d6\u5230\u3002<\/p>\n<p>java\u4e2d\uff0cinstanceof\u8fd0\u7b97\u7b26\u7528\u4e8e\u5224\u65ad\u524d\u9762\u7684\u5bf9\u8c61\u662f\u5426\u662f\u540e\u9762\u7684\u7c7b\uff0c\u6216\u5176\u5b50\u7c7b\u3001\u5b9e\u73b0\u7c7b\u7684\u5b9e\u4f8b\u3002\u5982\u679c\u662f\u8fd4\u56detrue\uff0c\u5426\u5219\u8fd4\u56defalse\u3002\u4e5f\u5c31\u662f\u8bf4\uff1a\u7528instanceof\u5173\u952e\u5b57\u505a\u5224\u65ad\u65f6\uff0c instanceof \u64cd\u4f5c\u7b26\u7684\u5de6\u53f3\u64cd\u4f5c\u5fc5\u987b\u6709\u7ee7\u627f\u6216\u5b9e\u73b0\u5173\u7cfb<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b9e\u9a8c\u4e00\uff1a\u83b7\u53d6bean \u2460\u65b9\u5f0f\u4e00\uff1a\u6839\u636eid\u83b7\u53d6 \u7531\u4e8e id \u5c5e\u6027\u6307\u5b9a\u4e86 bean \u7684\u552f\u4e00\u6807\u8bc6\uff0c\u6240\u4ee5\u6839\u636e bean [&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-1389","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\/1389","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=1389"}],"version-history":[{"count":0,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/1389\/revisions"}],"wp:attachment":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1389"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}