{"id":11956,"date":"2023-11-28T22:38:53","date_gmt":"2023-11-28T14:38:53","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=11956"},"modified":"2023-12-11T14:51:06","modified_gmt":"2023-12-11T06:51:06","slug":"spring-deioc-ru-he-shi-xian","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/spring-deioc-ru-he-shi-xian\/","title":{"rendered":"Spring\u7684IOC\u5982\u4f55\u5b9e\u73b0\uff1f"},"content":{"rendered":"<p>Spring\u7684IOC\uff08\u63a7\u5236\u53cd\u8f6c\uff09\u662f\u901a\u8fc7\u5bb9\u5668\u7ba1\u7406\u548c\u63a7\u5236\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u5bf9\u8c61\u53ca\u5176\u751f\u547d\u5468\u671f\u7684\u4e00\u79cd\u8bbe\u8ba1\u6a21\u5f0f\u3002\u5728Spring\u4e2d\uff0cIOC\u4e3b\u8981\u901a\u8fc7\u4f9d\u8d56\u6ce8\u5165\uff08DI\uff09\u5b9e\u73b0\u3002\u4f9d\u8d56\u6ce8\u5165\u662f\u6307\u5bb9\u5668\u8d1f\u8d23\u521b\u5efa\u5bf9\u8c61\u5e76\u5c06\u5b83\u4eec\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\u6ce8\u5165\u5230\u5bf9\u8c61\u4e2d\uff0c\u800c\u4e0d\u662f\u7531\u5bf9\u8c61\u81ea\u5df1\u521b\u5efa\u6216\u67e5\u627e\u4f9d\u8d56\u3002<\/p>\n<p>\u4ee5\u4e0b\u662fSpring IOC\u7684\u4e3b\u8981\u5b9e\u73b0\u65b9\u5f0f\uff1a<\/p>\n<ol>\n<li>\n<strong>XML\u914d\u7f6e\uff1a<\/strong> \u901a\u8fc7XML\u6587\u4ef6\u6765\u914d\u7f6e\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u5bf9\u8c61\u548c\u5b83\u4eec\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\u3002\u5728XML\u6587\u4ef6\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49Bean\uff08\u5bf9\u8c61\uff09\uff0c\u6307\u5b9a\u5b83\u4eec\u7684\u5c5e\u6027\u548c\u4f9d\u8d56\u5173\u7cfb\u3002Spring\u5bb9\u5668\u8d1f\u8d23\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u5e76\u5b9e\u4f8b\u5316\u3001\u914d\u7f6e\u548c\u7ba1\u7406\u8fd9\u4e9bBean\u3002<\/p>\n<pre><code class=\"language-xml\">&lt;!-- \u793a\u4f8b\uff1aXML\u914d\u7f6e\u4e00\u4e2a\u7b80\u5355\u7684Bean --&gt;\n&lt;bean id=&quot;myBean&quot; class=&quot;com.example.MyBean&quot;&gt;\n    &lt;property name=&quot;property1&quot; value=&quot;someValue&quot; \/&gt;\n    &lt;property name=&quot;property2&quot; ref=&quot;anotherBean&quot; \/&gt;\n&lt;\/bean&gt;\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u6ce8\u89e3\u914d\u7f6e\uff1a<\/strong> \u4f7f\u7528\u6ce8\u89e3\u5728Java\u7c7b\u4e2d\u6807\u8bb0Bean\u548c\u4f9d\u8d56\u5173\u7cfb\u3002\u901a\u8fc7\u5728\u7c7b\u6216\u65b9\u6cd5\u4e0a\u6dfb\u52a0\u6ce8\u89e3\uff0c\u544a\u8bc9Spring\u5bb9\u5668\u5982\u4f55\u521b\u5efa\u548c\u6ce8\u5165\u4f9d\u8d56\u5173\u7cfb\u3002<\/p>\n<pre><code class=\"language-java\">\/\/ \u793a\u4f8b\uff1a\u4f7f\u7528\u6ce8\u89e3\u914d\u7f6e\u4e00\u4e2a\u7b80\u5355\u7684Bean\n@Component\npublic class MyBean {\n    @Value(&quot;someValue&quot;)\n    private String property1;\n\n    @Autowired\n    private AnotherBean property2;\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>Java\u914d\u7f6e\uff1a<\/strong> \u4f7f\u7528\u7eafJava\u7c7b\u6765\u914d\u7f6e\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684Bean\u3002\u901a\u8fc7\u5728\u4e00\u4e2aJava\u914d\u7f6e\u7c7b\u4e2d\u4f7f\u7528<code>@Configuration<\/code>\u6ce8\u89e3\uff0c\u5e76\u5728\u65b9\u6cd5\u4e0a\u4f7f\u7528<code>@Bean<\/code>\u6ce8\u89e3\uff0c\u6765\u5b9a\u4e49Bean\u548c\u5b83\u4eec\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\u3002<\/p>\n<pre><code class=\"language-java\">\/\/ \u793a\u4f8b\uff1a\u4f7f\u7528Java\u914d\u7f6e\u4e00\u4e2a\u7b80\u5355\u7684Bean\n@Configuration\npublic class AppConfig {\n    @Bean\n    public MyBean myBean() {\n        MyBean bean = new MyBean();\n        bean.setProperty1(&quot;someValue&quot;);\n        bean.setProperty2(anotherBean());\n        return bean;\n    }\n\n    @Bean\n    public AnotherBean anotherBean() {\n        return new AnotherBean();\n    }\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u662fSpring IOC\u7684\u4e3b\u8981\u5b9e\u73b0\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u7684\u9700\u6c42\u9009\u62e9\u5176\u4e2d\u4e00\u79cd\u6216\u6df7\u5408\u4f7f\u7528\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u5f0f\uff0cSpring\u5bb9\u5668\u90fd\u4f1a\u8d1f\u8d23\u7ba1\u7406Bean\u7684\u751f\u547d\u5468\u671f\u3001\u4f9d\u8d56\u6ce8\u5165\u4ee5\u53ca\u5176\u4ed6\u65b9\u9762\u7684\u5bf9\u8c61\u63a7\u5236\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring\u7684IOC\uff08\u63a7\u5236\u53cd\u8f6c\uff09\u662f\u901a\u8fc7\u5bb9\u5668\u7ba1\u7406\u548c\u63a7\u5236\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u5bf9\u8c61\u53ca\u5176\u751f\u547d\u5468\u671f\u7684\u4e00\u79cd\u8bbe\u8ba1\u6a21\u5f0f\u3002\u5728Spring [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[],"class_list":["post-11956","post","type-post","status-publish","format-standard","hentry","category-spring"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11956","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=11956"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11956\/revisions"}],"predecessor-version":[{"id":40607,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11956\/revisions\/40607"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=11956"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=11956"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=11956"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}