{"id":290,"date":"2022-04-06T20:16:34","date_gmt":"2022-04-06T12:16:34","guid":{"rendered":"https:\/\/blog.lijiajia.top\/?p=290"},"modified":"2026-06-19T21:39:41","modified_gmt":"2026-06-19T13:39:41","slug":"spring%e6%a1%86%e6%9e%b6%e5%ad%a6%e4%b9%a0%ef%bc%9aioc%e6%93%8d%e4%bd%9cbean%e7%ae%a1%e7%90%86-xml%e8%87%aa%e5%8a%a8%e8%a3%85%e9%85%8d%e5%85%ab","status":"publish","type":"post","link":"https:\/\/blog.ttwow.top\/?p=290","title":{"rendered":"Spring\u6846\u67b6\u5b66\u4e60\uff1aIOC\u64cd\u4f5cBean\u7ba1\u7406-xml\u81ea\u52a8\u88c5\u914d(\u516b)"},"content":{"rendered":"<p>1\u3001\u4ec0\u4e48\u662f\u81ea\u52a8\u88c5\u914d<br \/>\n(1)\u6839\u636e\u6307\u5b9a\u88c5\u914d\u89c4\u5219\uff08\u5c5e\u6027\u540d\u79f0\u6216\u8005\u5c5e\u6027\u7c7b\u578b\uff09\uff0cSpring\u81ea\u52a8\u5c06\u5339\u914d\u7684\u5c5e\u6027\u503c\u8fdb\u884c\u6ce8\u5165<br \/>\n2\u3001\u6f14\u793a\u81ea\u52a8\u88c5\u914d\u8fc7\u7a0b<br \/>\n(1)\u6839\u636e\u5c5e\u6027\u540d\u79f0\u81ea\u52a8\u6ce8\u5165<\/p>\n<h2>\u4ee3\u7801\uff1a<\/h2>\n<p>Dept\uff1a<\/p>\n<pre><code class=\"language-java\">public class Dept {\n\n    @Override\n    public String toString() {\n        return &quot;Dept{}&quot;;\n    }\n}<\/code><\/pre>\n<p>Emp:<\/p>\n<pre><code class=\"language-java\">public class Emp {\n\/\/\u81ea\u52a8\u88c5\u914d\u662f \u67e5\u627e\u7684\u5c5e\u6027\u540d\uff1adept\n    private Dept dept;\n\n    public void setDept(Dept dept) {\n        this.dept = dept;\n    }\n\n    @Override\n    public String toString() {\n        return &quot;Emp{&quot; +\n                &quot;dept=&quot; + dept +\n                &#039;}&#039;;\n    }\n\n    public void test(){\n        System.out.println(dept);\n    }\n}<\/code><\/pre>\n<p>byName<\/p>\n<pre><code class=\"language-java\">    &lt;!--\u5b9e\u73b0\u81ea\u52a8\u88c5\u914d\n        bean\u6807\u7b7e\u5c5e\u6027 autowire \uff0c \u914d\u7f6e\u81ea\u52a8\u88c5\u914d\n        autowire\u4e24\u4e2a\u503c\uff1a\n            byName \u6839\u636e\u5c5e\u6027\u540d\u79f0\u6ce8\u5165\uff0c\u6ce8\u5165\u503cbean\u7684id\u548c\u7c7b\u5c5e\u6027\u540d\u79f0\u4e00\u6837\n            byType \u6839\u636e\u5c5e\u6027\u7c7b\u522b\u6ce8\u5165 \u627eDept\u7c7b\u578b\n            --&gt;\n    &lt;bean id=&quot;emp&quot; class=&quot;com.spring5.autowire.Emp&quot; autowire=&quot;byName&quot;&gt;\n        &lt;!--\u666e\u901a\u6ce8\u5165\u5c5e\u6027\u65b9\u5f0f--&gt;\n&lt;!--        &lt;property name=&quot;dept&quot; ref=&quot;dept&quot;&gt;&lt;\/property&gt;--&gt;\n    &lt;\/bean&gt;\n\n    &lt;bean id=&quot;dept&quot; class=&quot;com.spring5.autowire.Dept&quot;&gt;\n\n    &lt;\/bean&gt;<\/code><\/pre>\n<p>byType\uff1a\u591a\u4e2a\u7c7b\u578b\u65f6\uff0c\u65e0\u6cd5\u786e\u5b9a\u9700\u8981\u7684bean\u3002\u6240\u4ee5\u76f8\u540c\u7c7b\u578b\u7684bean\uff0c\u4e0d\u80fd\u5b9a\u4e49\u591a\u4e2a<\/p>\n<pre><code class=\"language-java\">    &lt;!--\u5b9e\u73b0\u81ea\u52a8\u88c5\u914d\n        bean\u6807\u7b7e\u5c5e\u6027 autowire \uff0c \u914d\u7f6e\u81ea\u52a8\u88c5\u914d\n        autowire\u4e24\u4e2a\u503c\uff1a\n            byName \u6839\u636e\u5c5e\u6027\u540d\u79f0\u6ce8\u5165\uff0c\u6ce8\u5165\u503cbean\u7684id\u548c\u7c7b\u5c5e\u6027\u540d\u79f0\u4e00\u6837\n            byType \u6839\u636e\u5c5e\u6027\u7c7b\u522b\u6ce8\u5165 \u627eDept\u7c7b\u578b\n            --&gt;\n    &lt;bean id=&quot;emp&quot; class=&quot;com.spring5.autowire.Emp&quot; autowire=&quot;byType&quot;&gt;\n        &lt;!--\u666e\u901a\u6ce8\u5165\u5c5e\u6027\u65b9\u5f0f--&gt;\n&lt;!--        &lt;property name=&quot;dept&quot; ref=&quot;dept&quot;&gt;&lt;\/property&gt;--&gt;\n    &lt;\/bean&gt;\n\n    &lt;bean id=&quot;dept&quot; class=&quot;com.spring5.autowire.Dept&quot;&gt;\n\n    &lt;\/bean&gt;\n<\/code><\/pre>\n<p>junit\u6d4b\u8bd5\uff1a<\/p>\n<pre><code class=\"language-java\">    @Test\n    public void test5(){\n\n        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(&quot;bean5.xml&quot;);\n        Emp emp = applicationContext.getBean(&quot;emp&quot;, Emp.class);\n\n        System.out.println(emp);\n    }<\/code><\/pre>\n<p>\u6d4b\u8bd5\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-java\">Emp{dept=Dept{}}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u4ec0\u4e48\u662f\u81ea\u52a8\u88c5\u914d (1)\u6839\u636e\u6307\u5b9a\u88c5\u914d\u89c4\u5219\uff08\u5c5e\u6027\u540d\u79f0\u6216\u8005\u5c5e\u6027\u7c7b\u578b\uff09\uff0cSpring\u81ea\u52a8\u5c06\u5339\u914d\u7684\u5c5e\u6027\u503c\u8fdb\u884c\u6ce8\u5165 2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[125],"tags":[68],"class_list":["post-290","post","type-post","status-publish","format-standard","hentry","category-java-backend","tag-spring"],"_links":{"self":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=290"}],"version-history":[{"count":1,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/290\/revisions"}],"predecessor-version":[{"id":2155,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/290\/revisions\/2155"}],"wp:attachment":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}