{"id":1132,"date":"2024-03-08T14:50:54","date_gmt":"2024-03-08T06:50:54","guid":{"rendered":"http:\/\/ericw.top\/?p=1132"},"modified":"2024-03-08T14:50:54","modified_gmt":"2024-03-08T06:50:54","slug":"di-%e5%85%a5%e9%97%a8%e6%a1%88%e4%be%8b","status":"publish","type":"post","link":"http:\/\/ericw.top\/?p=1132","title":{"rendered":"DI \u5165\u95e8\u6848\u4f8b"},"content":{"rendered":"\n<p><strong>DI\u5165\u95e8\u6848\u4f8b\u601d\u8def\u5206\u6790<\/strong><\/p>\n\n\n\n<p>1\u3001\u57fa\u4e8eIoC\u7ba1\u7406bean<\/p>\n\n\n\n<p>2\u3001Service\u4e2d\u4f7f\u7528new\u5f62\u5f0f\u521b\u5efa\u7684Dao\u5bf9\u8c61\u662f\u5426\u4fdd\u5b58<\/p>\n\n\n\n<p>3\u3001Service\u4e2d\u9700\u8981\u7684Dao\u5bf9\u8c61\u5982\u4f55\u8fdb\u5165\u5230Service\u4e2d<\/p>\n\n\n\n<p>4\u3001Service\u4e0eDao\u95f4\u7684\u5173\u7cfb\u5982\u4f55\u63cf\u8ff0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc-0\">\u5165\u95e8\u6848\u4f8b\u4ee3\u7801\u5b9e\u73b0<\/h2>\n\n\n\n<p>1\u3001\u5728BookServiceImpl\u7c7b\u4e2d\uff0c\u5220\u9664\u4e1a\u52a1\u5c42\u4e2d\u4f7f\u7528new\u7684\u65b9\u5f0f\u521b\u5efa\u7684dao\u5bf9\u8c61<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >public class BookServiceImpl implements BookService {\r\n    \/\/\u5220\u9664\u4e1a\u52a1\u5c42\u4e2d\u4f7f\u7528new\u7684\u65b9\u5f0f\u521b\u5efa\u7684dao\u5bf9\u8c61\r\n    private BookDao bookDao;\r\n\r\n    public void save() {\r\n        System.out.println(&quot;book service save ...&quot;);\r\n        bookDao.save();\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<p>2\u3001\u4e3a\u5c5e\u6027\u63d0\u4f9bsetter\u65b9\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >public class BookServiceImpl implements BookService {\r\n    \/\/\u5220\u9664\u4e1a\u52a1\u5c42\u4e2d\u4f7f\u7528new\u7684\u65b9\u5f0f\u521b\u5efa\u7684dao\u5bf9\u8c61\r\n    private BookDao bookDao;\r\n\r\n    public void save() {\r\n        System.out.println(&quot;book service save ...&quot;);\r\n        bookDao.save();\r\n    }\r\n    \/\/\u63d0\u4f9b\u5bf9\u5e94\u7684set\u65b9\u6cd5\r\n    public void setBookDao(BookDao bookDao) {\r\n        this.bookDao = bookDao;\r\n    }\r\n}\r\n<\/code><\/pre>\n\n\n\n<p>3\u3001\u4fee\u6539\u914d\u7f6e\u5b8c\u6210\u6ce8\u5165<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n       xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n       xsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd&quot;&gt;\r\n    &lt;!--bean\u6807\u7b7e\u6807\u793a\u914d\u7f6ebean\r\n    \tid\u5c5e\u6027\u6807\u793a\u7ed9bean\u8d77\u540d\u5b57\r\n    \tclass\u5c5e\u6027\u8868\u793a\u7ed9bean\u5b9a\u4e49\u7c7b\u578b\r\n\t--&gt;\r\n    &lt;bean id=&quot;bookDao&quot; class=&quot;com.itheima.dao.impl.BookDaoImpl&quot;\/&gt;\r\n\r\n    &lt;bean id=&quot;bookService&quot; class=&quot;com.itheima.service.impl.BookServiceImpl&quot;&gt;\r\n        &lt;!--\u914d\u7f6eserver\u4e0edao\u7684\u5173\u7cfb--&gt;\r\n        &lt;!--property\u6807\u7b7e\u8868\u793a\u914d\u7f6e\u5f53\u524dbean\u7684\u5c5e\u6027\r\n        \t\tname\u5c5e\u6027\u8868\u793a\u914d\u7f6e\u54ea\u4e00\u4e2a\u5177\u4f53\u7684\u5c5e\u6027\r\n        \t\tref\u5c5e\u6027\u8868\u793a\u53c2\u7167\u54ea\u4e00\u4e2abean\r\n\t\t--&gt;\r\n        &lt;property name=&quot;bookDao&quot; ref=&quot;bookDao&quot;\/&gt;\r\n    &lt;\/bean&gt;\r\n\r\n&lt;\/beans&gt;<\/code><\/pre>\n\n\n\n<ul>\n<li>name=\"bookDao\"\u4e2d<code class=\"prettyprint\" >bookDao<\/code>\u7684\u4f5c\u7528\u662f\u8ba9Spring\u7684IOC\u5bb9\u5668\u5728\u83b7\u53d6\u5230\u540d\u79f0\u540e\uff0c\u5c06\u9996\u5b57\u6bcd\u5927\u5199\uff0c\u524d\u9762\u52a0set\u627e\u5bf9\u5e94\u7684<code class=\"prettyprint\" >setBookDao()<\/code>\u65b9\u6cd5\u8fdb\u884c\u5bf9\u8c61\u6ce8\u5165<\/li>\n\n\n\n<li>ref=\"bookDao\"\u4e2d<code class=\"prettyprint\" >bookDao<\/code>\u7684\u4f5c\u7528\u662f\u8ba9Spring\u80fd\u5728IOC\u5bb9\u5668\u4e2d\u627e\u5230id\u4e3a<code class=\"prettyprint\" >bookDao<\/code>\u7684Bean\u5bf9\u8c61\u7ed9<code class=\"prettyprint\" >bookService<\/code>\u8fdb\u884c\u6ce8\u5165<\/li>\n<\/ul>\n\n\n\n<p>4\u3001\u8fd0\u884c\u7a0b\u5e8f<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"475\" height=\"92\" src=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/03\/image-31.png\" alt=\"\" class=\"wp-image-1133\" style=\"width:227px;height:auto\" srcset=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/03\/image-31.png 475w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/03\/image-31-300x58.png 300w\" sizes=\"(max-width: 475px) 100vw, 475px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>DI\u5165\u95e8\u6848\u4f8b\u601d\u8def\u5206\u6790 1\u3001\u57fa\u4e8eIoC\u7ba1\u7406bean 2\u3001Service\u4e2d\u4f7f\u7528new\u5f62\u5f0f\u521b\u5efa\u7684Dao\u5bf9\u8c61\u662f\u5426\u4fdd\u5b58  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1134,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,5],"tags":[],"_links":{"self":[{"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/posts\/1132"}],"collection":[{"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1132"}],"version-history":[{"count":1,"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/posts\/1132\/revisions"}],"predecessor-version":[{"id":1135,"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/posts\/1132\/revisions\/1135"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/media\/1134"}],"wp:attachment":[{"href":"http:\/\/ericw.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1132"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}