{"id":11936,"date":"2023-11-28T22:38:23","date_gmt":"2023-11-28T14:38:23","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=11936"},"modified":"2023-12-11T14:42:43","modified_gmt":"2023-12-11T06:42:43","slug":"spring-zhongbeandefinition-shi-shen-me","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/spring-zhongbeandefinition-shi-shen-me\/","title":{"rendered":"Spring\u4e2dBeanDefinition\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<p>\u5728 Spring \u4e2d\uff0c<code>BeanDefinition<\/code> \u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u7528\u4e8e\u63cf\u8ff0\u5728 Spring \u5bb9\u5668\u4e2d\u6ce8\u518c\u7684 Bean \u7684\u914d\u7f6e\u5143\u6570\u636e\u3002<code>BeanDefinition<\/code> \u5305\u542b\u4e86\u5173\u4e8e Bean \u7684\u5404\u79cd\u914d\u7f6e\u4fe1\u606f\uff0c\u4f8b\u5982\u7c7b\u540d\u3001\u4f5c\u7528\u57df\u3001\u6784\u9020\u51fd\u6570\u53c2\u6570\u3001\u5c5e\u6027\u503c\u3001\u521d\u59cb\u5316\u65b9\u6cd5\u3001\u9500\u6bc1\u65b9\u6cd5\u7b49\u3002<\/p>\n<p>\u4e3b\u8981\u7684\u5b9e\u73b0\u7c7b\u662f <code>GenericBeanDefinition<\/code>\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65b9\u6cd5\u7528\u4e8e\u8bbe\u7f6e Bean \u7684\u5404\u79cd\u5c5e\u6027\u3002<code>BeanDefinition<\/code> \u7684\u8bbe\u8ba1\u4f7f\u5f97 Spring \u53ef\u4ee5\u6839\u636e\u914d\u7f6e\u4fe1\u606f\u6765\u5b9e\u4f8b\u5316\u548c\u7ba1\u7406 Bean\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b <code>BeanDefinition<\/code> \u7684\u4e3b\u8981\u5c5e\u6027\u548c\u65b9\u6cd5\uff1a<\/p>\n<ol>\n<li>\n<strong>\u7c7b\u540d\uff08Class Name\uff09\uff1a<\/strong> \u63cf\u8ff0 Bean \u7684\u7c7b\u578b\uff0c\u53ef\u4ee5\u662f\u5177\u4f53\u7684\u7c7b\u6216\u62bd\u8c61\u7684\u63a5\u53e3\u3002<\/p>\n<pre><code class=\"language-java\">beanDefinition.setBeanClassName(&quot;com.example.MyBean&quot;);\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u4f5c\u7528\u57df\uff08Scope\uff09\uff1a<\/strong> \u63cf\u8ff0 Bean \u7684\u4f5c\u7528\u57df\uff0c\u5305\u62ec\u5355\u4f8b\uff08singleton\uff09\u3001\u539f\u578b\uff08prototype\uff09\u7b49\u3002<\/p>\n<pre><code class=\"language-java\">beanDefinition.setScope(BeanDefinition.SCOPE_SINGLETON);\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u6784\u9020\u51fd\u6570\u53c2\u6570\uff08Constructor Arguments\uff09\uff1a<\/strong> \u63cf\u8ff0 Bean \u6784\u9020\u51fd\u6570\u7684\u53c2\u6570\u3002<\/p>\n<pre><code class=\"language-java\">ConstructorArgumentValues constructorArgs = new ConstructorArgumentValues();\nconstructorArgs.addGenericArgumentValue(&quot;arg1&quot;);\nbeanDefinition.setConstructorArgumentValues(constructorArgs);\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u5c5e\u6027\u503c\uff08Property Values\uff09\uff1a<\/strong> \u63cf\u8ff0 Bean \u7684\u5c5e\u6027\u503c\u3002<\/p>\n<pre><code class=\"language-java\">MutablePropertyValues propertyValues = new MutablePropertyValues();\npropertyValues.add(&quot;propertyName&quot;, &quot;propertyValue&quot;);\nbeanDefinition.setPropertyValues(propertyValues);\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u521d\u59cb\u5316\u65b9\u6cd5\uff08Init Method\uff09\u548c\u9500\u6bc1\u65b9\u6cd5\uff08Destroy Method\uff09\uff1a<\/strong> \u63cf\u8ff0 Bean \u7684\u521d\u59cb\u5316\u548c\u9500\u6bc1\u65b9\u6cd5\u3002<\/p>\n<pre><code class=\"language-java\">beanDefinition.setInitMethodName(&quot;initMethod&quot;);\nbeanDefinition.setDestroyMethodName(&quot;destroyMethod&quot;);\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u61d2\u52a0\u8f7d\uff08Lazy Initialization\uff09\uff1a<\/strong> \u63cf\u8ff0\u662f\u5426\u5728\u9700\u8981\u65f6\u624d\u8fdb\u884c Bean \u7684\u5b9e\u4f8b\u5316\u3002<\/p>\n<pre><code class=\"language-java\">beanDefinition.setLazyInit(true);\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p><code>BeanDefinition<\/code> \u7684\u4e3b\u8981\u4f5c\u7528\u662f\u5728\u5bb9\u5668\u542f\u52a8\u65f6\u8bfb\u53d6\u548c\u89e3\u6790\u914d\u7f6e\u4fe1\u606f\uff0c\u7136\u540e\u6839\u636e\u8fd9\u4e9b\u4fe1\u606f\u6765\u5b9e\u4f8b\u5316\u548c\u7ba1\u7406 Bean\u3002Spring \u5bb9\u5668\u4f1a\u6839\u636e\u914d\u7f6e\u6587\u4ef6\u6216\u6ce8\u89e3\u4e2d\u7684 <code>BeanDefinition<\/code> \u4fe1\u606f\u6765\u521b\u5efa\u76f8\u5e94\u7684 Bean \u5b9e\u4f8b\uff0c\u5e76\u6839\u636e\u4f5c\u7528\u57df\u548c\u751f\u547d\u5468\u671f\u6765\u7ba1\u7406\u8fd9\u4e9b\u5b9e\u4f8b\u3002\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u5f97\u5f00\u53d1\u8005\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u6216\u6ce8\u89e3\u7684\u65b9\u5f0f\u7075\u6d3b\u5730\u5b9a\u4e49 Bean \u7684\u884c\u4e3a\u548c\u5c5e\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Spring \u4e2d\uff0cBeanDefinition \u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u7528\u4e8e\u63cf\u8ff0\u5728 Spring \u5bb9\u5668\u4e2d\u6ce8\u518c\u7684 Bea [&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-11936","post","type-post","status-publish","format-standard","hentry","category-spring"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11936","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=11936"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11936\/revisions"}],"predecessor-version":[{"id":37859,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11936\/revisions\/37859"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=11936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=11936"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=11936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}