{"id":12332,"date":"2023-11-28T22:50:37","date_gmt":"2023-11-28T14:50:37","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=12332"},"modified":"2023-12-11T16:21:24","modified_gmt":"2023-12-11T08:21:24","slug":"spring-dei0c-de-shi-xian-ji-zhi","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/spring-dei0c-de-shi-xian-ji-zhi\/","title":{"rendered":"Spring\u7684I0C\u7684\u5b9e\u73b0\u673a\u5236\uff1f"},"content":{"rendered":"<p>Spring\u7684IoC\uff08Inversion of Control\uff0c\u63a7\u5236\u53cd\u8f6c\uff09\u662f\u901a\u8fc7\u5bb9\u5668\u6765\u5b9e\u73b0\u7684\u3002IoC\u7684\u6838\u5fc3\u601d\u60f3\u662f\u5c06\u5bf9\u8c61\u7684\u521b\u5efa\u548c\u4f9d\u8d56\u5173\u7cfb\u7684\u7ba1\u7406\u4ea4\u7531\u5bb9\u5668\u6765\u5b8c\u6210\uff0c\u800c\u4e0d\u662f\u5728\u5e94\u7528\u7a0b\u5e8f\u4ee3\u7801\u4e2d\u76f4\u63a5\u521b\u5efa\u548c\u7ba1\u7406\u5bf9\u8c61\u3002Spring\u7684IoC\u5bb9\u5668\u8d1f\u8d23\u5b9e\u4f8b\u5316\u3001\u7ec4\u88c5\u3001\u914d\u7f6e\u548c\u7ba1\u7406\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u5bf9\u8c61\u3002<\/p>\n<p>\u4ee5\u4e0b\u662fSpring IoC\u7684\u4e3b\u8981\u5b9e\u73b0\u673a\u5236\uff1a<\/p>\n<ol>\n<li>\n<strong>Bean\u5b9a\u4e49\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728Spring IoC\u4e2d\uff0c\u6bcf\u4e2a\u88ab\u5bb9\u5668\u7ba1\u7406\u7684\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a\u5bf9\u5e94\u7684Bean\u5b9a\u4e49\u3002Bean\u5b9a\u4e49\u5305\u542b\u4e86\u5bf9\u8c61\u7684\u5404\u79cd\u5143\u6570\u636e\uff0c\u5982\u7c7b\u540d\u3001\u4f5c\u7528\u57df\u3001\u4f9d\u8d56\u5173\u7cfb\u3001\u521d\u59cb\u5316\u65b9\u6cd5\u3001\u9500\u6bc1\u65b9\u6cd5\u7b49\u3002Bean\u5b9a\u4e49\u53ef\u4ee5\u901a\u8fc7XML\u914d\u7f6e\u3001Java\u914d\u7f6e\u7c7b\u6216\u6ce8\u89e3\u6765\u5b9a\u4e49\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-xml\">&lt;!-- \u793a\u4f8b\uff1a\u901a\u8fc7XML\u914d\u7f6e\u5b9a\u4e49Bean --&gt;\n&lt;bean id=&quot;myBean&quot; class=&quot;com.example.MyBean&quot; scope=&quot;singleton&quot;&gt;\n    &lt;!-- Bean \u7684\u5c5e\u6027\u914d\u7f6e --&gt;\n&lt;\/bean&gt;\n<\/code><\/pre>\n<pre><code class=\"language-java\">\/\/ \u793a\u4f8b\uff1a\u901a\u8fc7Java\u914d\u7f6e\u7c7b\u5b9a\u4e49Bean\n@Configuration\npublic class AppConfig {\n    @Bean\n    public MyBean myBean() {\n        return new MyBean();\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>Bean\u7684\u5b9e\u4f8b\u5316\uff1a<\/strong><\/p>\n<ul>\n<li>IoC\u5bb9\u5668\u8d1f\u8d23\u6839\u636eBean\u5b9a\u4e49\u521b\u5efaBean\u7684\u5b9e\u4f8b\u3002\u5b9e\u4f8b\u5316\u53ef\u4ee5\u901a\u8fc7\u6784\u9020\u51fd\u6570\u3001\u5de5\u5382\u65b9\u6cd5\u3001\u9759\u6001\u5de5\u5382\u65b9\u6cd5\u7b49\u65b9\u5f0f\u8fdb\u884c\u3002Spring\u652f\u6301\u591a\u79cd\u5b9e\u4f8b\u5316\u7b56\u7565\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<strong>\u4f9d\u8d56\u6ce8\u5165\uff1a<\/strong><\/p>\n<ul>\n<li>IoC\u5bb9\u5668\u901a\u8fc7\u4f9d\u8d56\u6ce8\u5165\u5c06\u4e00\u4e2aBean\u6240\u4f9d\u8d56\u7684\u5176\u4ed6Bean\u6ce8\u5165\u5230\u5b83\u5185\u90e8\u3002\u4f9d\u8d56\u6ce8\u5165\u53ef\u4ee5\u901a\u8fc7\u6784\u9020\u51fd\u6570\u6ce8\u5165\u3001Setter\u65b9\u6cd5\u6ce8\u5165\u3001\u5b57\u6bb5\u6ce8\u5165\u7b49\u65b9\u5f0f\u5b9e\u73b0\u3002Spring\u652f\u6301\u901a\u8fc7XML\u914d\u7f6e\u3001Java\u914d\u7f6e\u3001\u6ce8\u89e3\u7b49\u65b9\u5f0f\u58f0\u660e\u4f9d\u8d56\u5173\u7cfb\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">\/\/ \u793a\u4f8b\uff1a\u901a\u8fc7\u6784\u9020\u51fd\u6570\u6ce8\u5165\u4f9d\u8d56\npublic class MyBean {\n    private AnotherBean anotherBean;\n\n    public MyBean(AnotherBean anotherBean) {\n        this.anotherBean = anotherBean;\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>Bean\u7684\u751f\u547d\u5468\u671f\u7ba1\u7406\uff1a<\/strong><\/p>\n<ul>\n<li>IoC\u5bb9\u5668\u8d1f\u8d23\u7ba1\u7406Bean\u7684\u751f\u547d\u5468\u671f\uff0c\u5305\u62ec\u521d\u59cb\u5316\u548c\u9500\u6bc1\u3002\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0<code>InitializingBean<\/code>\u548c<code>DisposableBean<\/code>\u63a5\u53e3\u3001\u4f7f\u7528<code>@PostConstruct<\/code>\u548c<code>@PreDestroy<\/code>\u6ce8\u89e3\uff0c\u6216\u914d\u7f6e\u521d\u59cb\u5316\u548c\u9500\u6bc1\u65b9\u6cd5\u6765\u5b9a\u4e49Bean\u7684\u751f\u547d\u5468\u671f\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">\/\/ \u793a\u4f8b\uff1a\u901a\u8fc7@PostConstruct\u548c@PreDestroy\u6ce8\u89e3\u5b9a\u4e49\u521d\u59cb\u5316\u548c\u9500\u6bc1\u65b9\u6cd5\npublic class MyBean {\n    @PostConstruct\n    public void init() {\n        \/\/ \u521d\u59cb\u5316\u903b\u8f91\n    }\n\n    @PreDestroy\n    public void destroy() {\n        \/\/ \u9500\u6bc1\u903b\u8f91\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>Bean\u7684\u4f5c\u7528\u57df\u7ba1\u7406\uff1a<\/strong><\/p>\n<ul>\n<li>IoC\u5bb9\u5668\u652f\u6301\u4e0d\u540c\u7684Bean\u4f5c\u7528\u57df\uff0c\u5305\u62ecsingleton\uff08\u5355\u4f8b\uff09\u3001prototype\uff08\u539f\u578b\uff09\u3001request\u3001session\u7b49\u3002\u4f5c\u7528\u57df\u5b9a\u4e49\u4e86Bean\u5b9e\u4f8b\u7684\u751f\u547d\u5468\u671f\u8303\u56f4\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">\/\/ \u793a\u4f8b\uff1a\u901a\u8fc7@Scope\u6ce8\u89e3\u5b9a\u4e49Bean\u7684\u4f5c\u7528\u57df\u4e3aprototype\n@Component\n@Scope(&quot;prototype&quot;)\npublic class MyPrototypeBean {\n    \/\/ ...\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>IoC\u5bb9\u5668\u7684\u5b9e\u73b0\uff1a<\/strong><\/p>\n<ul>\n<li>Spring\u7684IoC\u5bb9\u5668\u6709\u591a\u79cd\u5b9e\u73b0\uff0c\u5176\u4e2d\u6700\u5e38\u89c1\u7684\u662f<code>ApplicationContext<\/code>\u63a5\u53e3\u7684\u5b9e\u73b0\u7c7b\uff0c\u5982<code>ClassPathXmlApplicationContext<\/code>\u3001<code>AnnotationConfigApplicationContext<\/code>\u7b49\u3002\u8fd9\u4e9b\u5bb9\u5668\u8d1f\u8d23\u52a0\u8f7dBean\u5b9a\u4e49\u3001\u7ba1\u7406Bean\u7684\u751f\u547d\u5468\u671f\u3001\u5904\u7406\u4f9d\u8d56\u6ce8\u5165\u7b49\u5de5\u4f5c\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">\/\/ \u793a\u4f8b\uff1a\u901a\u8fc7ClassPathXmlApplicationContext\u52a0\u8f7dXML\u914d\u7f6e\u6587\u4ef6\nApplicationContext context = new ClassPathXmlApplicationContext(&quot;applicationContext.xml&quot;);\nMyBean myBean = context.getBean(MyBean.class);\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u673a\u5236\uff0cSpring IoC\u5b9e\u73b0\u4e86\u5bf9\u5bf9\u8c61\u7684\u521b\u5efa\u3001\u88c5\u914d\u3001\u914d\u7f6e\u548c\u7ba1\u7406\uff0c\u4f7f\u5f97\u5e94\u7528\u7a0b\u5e8f\u66f4\u52a0\u7075\u6d3b\u3001\u53ef\u7ef4\u62a4\uff0c\u5e76\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u6d4b\u8bd5\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring\u7684IoC\uff08Inversion of Control\uff0c\u63a7\u5236\u53cd\u8f6c\uff09\u662f\u901a\u8fc7\u5bb9\u5668\u6765\u5b9e\u73b0\u7684\u3002IoC\u7684\u6838\u5fc3\u601d\u60f3 [&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-12332","post","type-post","status-publish","format-standard","hentry","category-spring"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12332","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=12332"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12332\/revisions"}],"predecessor-version":[{"id":47888,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12332\/revisions\/47888"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=12332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=12332"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=12332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}