{"id":1120,"date":"2024-03-07T17:40:29","date_gmt":"2024-03-07T09:40:29","guid":{"rendered":"http:\/\/ericw.top\/?p=1120"},"modified":"2024-03-07T18:03:19","modified_gmt":"2024-03-07T10:03:19","slug":"ioc-%e5%85%a5%e9%97%a8%e6%a1%88%e4%be%8b","status":"publish","type":"post","link":"http:\/\/ericw.top\/?p=1120","title":{"rendered":"IoC \u5165\u95e8\u6848\u4f8b"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"914\" height=\"543\" src=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/03\/image-26.png\" alt=\"\" class=\"wp-image-1121\" style=\"width:310px;height:auto\" srcset=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/03\/image-26.png 914w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/03\/image-26-300x178.png 300w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/03\/image-26-768x456.png 768w\" sizes=\"(max-width: 914px) 100vw, 914px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc-0\"><strong>IoC\u5165\u95e8\u6848\u4f8b\u601d\u8def\u5206\u6790<\/strong><\/h2>\n\n\n\n<p>1\u3001\u7ba1\u7406\u4ec0\u4e48\uff1f\uff08Service\u4e0eDao\uff09<\/p>\n\n\n\n<p>2\u3001\u5982\u4f55\u5c06\u88ab\u7ba1\u7406\u7684\u5bf9\u8c61\u544a\u77e5IoC\u5bb9\u5668\uff1f\uff08\u914d\u7f6e\uff09<\/p>\n\n\n\n<p>3\u3001\u88ab\u6302\u91cc\u7684\u5bf9\u8c61\u4ea4\u7ed9IoC\u5bb9\u5668\uff0c\u5982\u4f55\u83b7\u53d6\u5230IoC\u5bb9\u5668\uff08\u63a5\u53e3\uff09<\/p>\n\n\n\n<p>4\u3001IoC\u5bb9\u5668\u5f97\u5230\u540e\uff0c\u5982\u4f55\u4ece\u5bb9\u5668\u4e2d\u83b7\u53d6bean\uff1f\uff08\u63a5\u53e3\u65b9\u6cd5\uff09<\/p>\n\n\n\n<p>5\u3001\u4f7f\u7528Spring\u5bfc\u5165\u54ea\u4e9b\u5750\u6807\uff1f\uff08pom.xml\uff09<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"toc-1\"><br>\u6b65\u9aa41:\u521b\u5efaMaven\u9879\u76ee<\/h5>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"954\" height=\"388\" src=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/03\/image-27.png\" alt=\"\" class=\"wp-image-1122\" style=\"width:438px;height:auto\" srcset=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/03\/image-27.png 954w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/03\/image-27-300x122.png 300w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/03\/image-27-768x312.png 768w\" sizes=\"(max-width: 954px) 100vw, 954px\" \/><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"toc-2\">\u6b65\u9aa42:\u6dfb\u52a0Spring\u7684\u4f9d\u8d56jar\u5305 \u5230pom.xml (bean\u5b9a\u4e49\u65f6\u7684id\u5c5e\u6027\u5728\u540c\u4e00\u4e2a\u4e0a\u4e0b\u6587\u4e2d\u4e0d\u80fd\u91cd\u590d)<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >        &lt;dependency&gt;\n            &lt;groupId&gt;org.springframework&lt;\/groupId&gt;\n            &lt;artifactId&gt;spring-context&lt;\/artifactId&gt;\n            &lt;version&gt;5.2.10.RELEASE&lt;\/version&gt;\n        &lt;\/dependency&gt;\n\n        &lt;dependency&gt;\n            &lt;groupId&gt;junit&lt;\/groupId&gt;\n            &lt;artifactId&gt;junit&lt;\/artifactId&gt;\n            &lt;version&gt;4.12&lt;\/version&gt;\n            &lt;scope&gt;test&lt;\/scope&gt;\n        &lt;\/dependency&gt;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"toc-3\">\u6b65\u9aa43:\u6dfb\u52a0\u6848\u4f8b\u4e2d\u9700\u8981\u7684\u7c7b<\/h5>\n\n\n\n<p><strong>\u521b\u5efaBookService,BookServiceImpl\uff0cBookDao\u548cBookDaoImpl\u56db\u4e2a\u7c7b<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >public interface BookDao {\n    public void save();\n}\n\npublic class BookDaoImpl implements BookDao {\n    public void save() {\n        System.out.println(&quot;book dao save ...&quot;);\n    }\n}\n\npublic interface BookService {\n    public void save();\n}\n\npublic class BookServiceImpl implements BookService {\n    private BookDao bookDao = new BookDaoImpl();\n    public void save() {\n        System.out.println(&quot;book service save ...&quot;);\n        bookDao.save();\n    }\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"toc-4\">\u6b65\u9aa44:\u6dfb\u52a0spring\u914d\u7f6e\u6587\u4ef6<\/h5>\n\n\n\n<p>resources\u4e0b\u6dfb\u52a0spring\u914d\u7f6e\u6587\u4ef6applicationContext.xml\uff0c\u5e76\u5b8c\u6210bean\u7684\u914d\u7f6e<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" width=\"1024\" height=\"595\" src=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/03\/image-28-1024x595.png\" alt=\"\" class=\"wp-image-1123\" style=\"width:714px;height:auto\" srcset=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/03\/image-28-1024x595.png 1024w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/03\/image-28-300x174.png 300w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/03\/image-28-768x446.png 768w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/03\/image-28.png 1514w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"toc-5\">\u6b65\u9aa45:\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u5b8c\u6210bean\u7684\u914d\u7f6e<\/h5>\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;\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\n       xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\n       xsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd&quot;&gt;\n\n    &lt;!--bean\u6807\u7b7e\u6807\u793a\u914d\u7f6ebean\n    \tid\u5c5e\u6027\u6807\u793a\u7ed9bean\u8d77\u540d\u5b57\n    \tclass\u5c5e\u6027\u8868\u793a\u7ed9bean\u5b9a\u4e49\u7c7b\u578b\n\t--&gt;\n    &lt;bean id=&quot;bookDao&quot; class=&quot;com.ericw.dao.impl.BookDaoImpl&quot;\/&gt;\n    &lt;bean id=&quot;bookService&quot; class=&quot;com.ericw.service.impl.BookServiceImpl&quot;\/&gt;\n\n&lt;\/beans&gt;<\/code><\/pre>\n\n\n\n<p><strong>==\u6ce8\u610f\u4e8b\u9879\uff1abean\u5b9a\u4e49\u65f6id\u5c5e\u6027\u5728\u540c\u4e00\u4e2a\u4e0a\u4e0b\u6587\u4e2d(\u914d\u7f6e\u6587\u4ef6)\u4e0d\u80fd\u91cd\u590d==<\/strong><\/p>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"toc-6\">\u6b65\u9aa46:\u83b7\u53d6IOC\u5bb9\u5668<\/h5>\n\n\n\n<p>\u4f7f\u7528Spring\u63d0\u4f9b\u7684\u63a5\u53e3\u5b8c\u6210IOC\u5bb9\u5668\u7684\u521b\u5efa\uff0c\u521b\u5efaApp\u7c7b\uff0c\u7f16\u5199main\u65b9\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >public class App {\n    public static void main(String&#091;] args) {\n        \/\/\u83b7\u53d6IOC\u5bb9\u5668\n        ApplicationContext ctx = new ClassPathXmlApplicationContext(&quot;applicationContext.xml&quot;);\n    }\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"toc-7\">\u6b65\u9aa47:\u4ece\u5bb9\u5668\u4e2d\u83b7\u53d6\u5bf9\u8c61\u8fdb\u884c\u65b9\u6cd5\u8c03\u7528<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >public class App2 {\n    public static void main(String&#091;] args) {\n\/\/        \u83b7\u53d6IoC\u5bb9\u5668\n        ApplicationContext ctx = new ClassPathXmlApplicationContext(&quot;applicationContext.xml&quot;);\n\/\/        \u83b7\u53d6Bean\n        BookService bookService = (BookService) ctx.getBean(&quot;bookService&quot;);\n\/\/        \u8fd0\u884c\u8f93\u51fabookservice\n        bookService.save();\n    }\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"toc-8\">\u6b65\u9aa48:\u8fd0\u884c\u7a0b\u5e8f<\/h5>\n\n\n\n<p>\u6d4b\u8bd5\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"662\" height=\"86\" src=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/03\/image-29.png\" alt=\"\" class=\"wp-image-1124\" style=\"width:377px;height:auto\" srcset=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/03\/image-29.png 662w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/03\/image-29-300x39.png 300w\" sizes=\"(max-width: 662px) 100vw, 662px\" \/><\/figure>\n\n\n\n<p>Spring\u7684IOC\u5165\u95e8\u6848\u4f8b\u5df2\u7ecf\u5b8c\u6210\uff0c\u4f46\u662f\u5728<code class=\"prettyprint\" >BookServiceImpl<\/code>\u7684\u7c7b\u4e2d\u4f9d\u7136\u5b58\u5728<code class=\"prettyprint\" >BookDaoImpl<\/code>\u5bf9\u8c61\u7684new\u64cd\u4f5c\uff0c\u5b83\u4eec\u4e4b\u95f4\u7684\u8026\u5408\u5ea6\u8fd8\u662f\u6bd4\u8f83\u9ad8\uff0c\u8fd9\u5757\u8be5\u5982\u4f55\u89e3\u51b3\uff0c\u5c31\u9700\u8981\u7528\u5230\u4e0b\u9762\u7684<code class=\"prettyprint\" >DI:\u4f9d\u8d56\u6ce8\u5165<\/code>\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>IoC\u5165\u95e8\u6848\u4f8b\u601d\u8def\u5206\u6790 1\u3001\u7ba1\u7406\u4ec0\u4e48\uff1f\uff08Service\u4e0eDao\uff09 2\u3001\u5982\u4f55\u5c06\u88ab\u7ba1\u7406\u7684\u5bf9\u8c61\u544a\u77e5IoC\u5bb9\u5668\uff1f\uff08\u914d\u7f6e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1121,"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\/1120"}],"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=1120"}],"version-history":[{"count":3,"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/posts\/1120\/revisions"}],"predecessor-version":[{"id":1128,"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/posts\/1120\/revisions\/1128"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/media\/1121"}],"wp:attachment":[{"href":"http:\/\/ericw.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1120"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}