{"id":11637,"date":"2023-11-28T22:29:03","date_gmt":"2023-11-28T14:29:03","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=11637"},"modified":"2023-12-11T11:49:04","modified_gmt":"2023-12-11T03:49:04","slug":"spring-zhongbeanfactory-le-jie-ma","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/spring-zhongbeanfactory-le-jie-ma\/","title":{"rendered":"Spring\u4e2dBeanFactory\u4e86\u89e3\u5417\uff1f"},"content":{"rendered":"<p>\u662f\u7684\uff0c<code>BeanFactory<\/code>\u662fSpring\u6846\u67b6\u4e2d\u7684\u4e00\u4e2a\u6838\u5fc3\u63a5\u53e3\uff0c\u7528\u4e8e\u7ba1\u7406\u548c\u7ef4\u62a4Bean\u7684\u751f\u547d\u5468\u671f\u4ee5\u53ca\u4f9d\u8d56\u6ce8\u5165\u3002<code>BeanFactory<\/code>\u5b9a\u4e49\u4e86\u4e00\u7ec4\u7528\u4e8e\u83b7\u53d6Bean\u7684\u65b9\u6cd5\uff0c\u540c\u65f6\u63d0\u4f9b\u4e86\u5bf9Bean\u7684\u914d\u7f6e\u3001\u5b9e\u4f8b\u5316\u3001\u4f9d\u8d56\u6ce8\u5165\u7b49\u529f\u80fd\u7684\u652f\u6301\u3002<\/p>\n<p>\u4e3b\u8981\u7279\u70b9\u548c\u7528\u9014\u5305\u62ec\uff1a<\/p>\n<ol>\n<li>\n<strong>Bean\u7684\u7ba1\u7406\uff1a<\/strong><\/p>\n<ul>\n<li><code>BeanFactory<\/code>\u8d1f\u8d23\u7ba1\u7406\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684Bean\u3002\u5b83\u901a\u8fc7\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u6216\u6ce8\u89e3\u6765\u4e86\u89e3\u54ea\u4e9bBean\u9700\u8981\u88ab\u521b\u5efa\uff0c\u4ee5\u53ca\u5b83\u4eec\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<strong>Bean\u7684\u914d\u7f6e\uff1a<\/strong><\/p>\n<ul>\n<li><code>BeanFactory<\/code>\u53ef\u4ee5\u901a\u8fc7XML\u914d\u7f6e\u6587\u4ef6\u3001Java\u914d\u7f6e\u6216<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>\u6ce8\u89e3\u7b49\u65b9\u5f0f\u6765\u914d\u7f6eBean\u7684\u5b9a\u4e49\u3002\u914d\u7f6e\u6587\u4ef6\u4e2d\u5305\u542b\u4e86Bean\u7684\u7c7b\u578b\u3001\u4f5c\u7528\u57df\u3001\u4f9d\u8d56\u5173\u7cfb\u7b49\u4fe1\u606f\u3002Spring\u5bb9\u5668\u6839\u636e\u8fd9\u4e9b\u914d\u7f6e\u4fe1\u606f\u6765\u521b\u5efa\u548c\u7ba1\u7406Bean\u3002<\/p>\n<pre><code class=\"language-xml\">&lt;!-- \u793a\u4f8b\uff1aXML\u914d\u7f6e\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e00\u4e2aBean --&gt;\n&lt;bean id=&quot;myBean&quot; class=&quot;com.example.MyBean&quot; \/&gt;\n<\/code><\/pre>\n<ol start=\"3\">\n<li>\n<strong>Bean\u7684\u5b9e\u4f8b\u5316\uff1a<\/strong><\/p>\n<ul>\n<li><code>BeanFactory<\/code>\u8d1f\u8d23\u5b9e\u4f8b\u5316Bean\uff0c\u5e76\u6839\u636e\u914d\u7f6e\u4fe1\u606f\u6765\u521b\u5efa\u76f8\u5e94\u7684Bean\u5bf9\u8c61\u3002\u5b9e\u4f8b\u5316\u7684\u65b9\u5f0f\u53ef\u4ee5\u662f\u5ef6\u8fdf\u52a0\u8f7d\u3001\u5355\u4f8b\u6a21\u5f0f\u3001\u539f\u578b\u6a21\u5f0f\u7b49\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<strong>\u4f9d\u8d56\u6ce8\u5165\uff1a<\/strong><\/p>\n<ul>\n<li>\u901a\u8fc7<code>BeanFactory<\/code>\uff0cSpring\u5bb9\u5668\u53ef\u4ee5\u5728\u521b\u5efaBean\u7684\u540c\u65f6\uff0c\u5c06\u4f9d\u8d56\u7684\u5176\u4ed6Bean\u6ce8\u5165\u5230\u76ee\u6807Bean\u4e2d\u3002\u8fd9\u79cd\u65b9\u5f0f\u79f0\u4e3a\u4f9d\u8d56\u6ce8\u5165\uff0c\u53ef\u4ee5\u901a\u8fc7\u6784\u9020\u5668\u6ce8\u5165\u3001Setter\u6ce8\u5165\u7b49\u65b9\u5f0f\u5b9e\u73b0\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">public 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>\u4f5c\u7528\u57df\u7ba1\u7406\uff1a<\/strong><\/p>\n<ul>\n<li><code>BeanFactory<\/code>\u652f\u6301\u5b9a\u4e49Bean\u7684\u4f5c\u7528\u57df\uff0c\u5982\u5355\u4f8b\uff08Singleton\uff09\u3001\u539f\u578b\uff08Prototype\uff09\u3001\u4f1a\u8bdd\uff08Session\uff09\u3001\u8bf7\u6c42\uff08Request\uff09\u7b49\u3002\u4e0d\u540c\u7684\u4f5c\u7528\u57df\u51b3\u5b9a\u4e86Bean\u7684\u751f\u547d\u5468\u671f\u548c\u5b9e\u4f8b\u5316\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-xml\">&lt;!-- \u793a\u4f8b\uff1aXML\u914d\u7f6e\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u4f5c\u7528\u57df\u4e3a\u5355\u4f8b\u7684Bean --&gt;\n&lt;bean id=&quot;singletonBean&quot; class=&quot;com.example.SingletonBean&quot; scope=&quot;singleton&quot; \/&gt;\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u5bb9\u5668\u7684\u61d2\u52a0\u8f7d\uff1a<\/strong><\/p>\n<ul>\n<li><code>BeanFactory<\/code>\u652f\u6301\u61d2\u52a0\u8f7d\uff0c\u5373\u5728\u9700\u8981\u65f6\u624d\u771f\u6b63\u5b9e\u4f8b\u5316Bean\u3002\u8fd9\u6709\u52a9\u4e8e\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u542f\u52a8\u6027\u80fd\uff0c\u53ea\u6709\u5728\u9700\u8981\u65f6\u624d\u521b\u5efa\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-xml\">&lt;!-- \u793a\u4f8b\uff1aXML\u914d\u7f6e\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u61d2\u52a0\u8f7d\u7684Bean --&gt;\n&lt;bean id=&quot;lazyBean&quot; class=&quot;com.example.LazyBean&quot; lazy-init=&quot;true&quot; \/&gt;\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u603b\u4f53\u800c\u8a00\uff0c<code>BeanFactory<\/code>\u662fSpring\u6846\u67b6\u4e2d\u7528\u4e8e\u7ba1\u7406Bean\u7684\u6838\u5fc3\u63a5\u53e3\u4e4b\u4e00\uff0c\u5b83\u63d0\u4f9b\u4e86\u914d\u7f6e\u3001\u5b9e\u4f8b\u5316\u3001\u4f9d\u8d56\u6ce8\u5165\u7b49\u4e00\u7cfb\u5217\u529f\u80fd\uff0c\u662fSpring\u5bb9\u5668\u7684\u57fa\u7840\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e38\u7528\u7684\u5b9e\u73b0\u7c7b\u662f<code>org.springframework.beans.factory.xml.XmlBeanFactory<\/code>\uff0c\u4e0d\u8fc7\u5728\u65b0\u7248\u672c\u7684Spring\u4e2d\uff0c\u63a8\u8350\u4f7f\u7528<code>ApplicationContext<\/code>\u63a5\u53e3\u7684\u5b9e\u73b0\u7c7b\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\u548c\u66f4\u597d\u7684\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u662f\u7684\uff0cBeanFactory\u662fSpring\u6846\u67b6\u4e2d\u7684\u4e00\u4e2a\u6838\u5fc3\u63a5\u53e3\uff0c\u7528\u4e8e\u7ba1\u7406\u548c\u7ef4\u62a4Bean\u7684\u751f\u547d\u5468\u671f\u4ee5\u53ca\u4f9d\u8d56\u6ce8\u5165\u3002 [&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-11637","post","type-post","status-publish","format-standard","hentry","category-spring"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11637","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=11637"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11637\/revisions"}],"predecessor-version":[{"id":17099,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11637\/revisions\/17099"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=11637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=11637"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=11637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}