{"id":16781,"date":"2024-12-17T09:24:14","date_gmt":"2024-12-17T01:24:14","guid":{"rendered":"https:\/\/92it.top\/?p=16781"},"modified":"2024-12-17T09:24:14","modified_gmt":"2024-12-17T01:24:14","slug":"spring-bean%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f%e5%8d%81%e5%88%86%e9%92%9f%e5%b8%a6%e4%bd%a0%e4%ba%86%e8%a7%a3%ef%bc%81","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=16781","title":{"rendered":"Spring Bean\u662f\u4ec0\u4e48\uff1f\u5341\u5206\u949f\u5e26\u4f60\u4e86\u89e3\uff01"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1a<a href=\"https:\/\/mp.weixin.qq.com\/s?__biz=MzIwNDAyOTI2Nw==&amp;mid=2247500982&amp;idx=1&amp;sn=23f8517bc0d95c57e2971f686e016eb5&amp;chksm=96c4e88aa1b3619cbc34060e7c02a1b6ae3e324307e34e51f0ef2b0c02b7b25e13906c69bfbe&amp;cur_album_id=2818595943724974083&amp;scene=190#rd\">Spring Bean\u662f\u4ec0\u4e48\uff1f\u5341\u5206\u949f\u5e26\u4f60\u4e86\u89e3\uff01<\/a><\/p>\n\n\n\n<p>Spring Bean \u662f Spring \u6846\u67b6\u4e2d\u7684\u6838\u5fc3\u6982\u5ff5\u4e4b\u4e00\uff0c\u5b83\u4ee3\u8868\u4e86\u7531 Spring \u5bb9\u5668\u7ba1\u7406\u7684\u5bf9\u8c61\u3002\u5728 Spring \u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u51e0\u4e4e\u6240\u6709\u7684\u5bf9\u8c61\u90fd\u53ef\u4ee5\u88ab\u5b9a\u4e49\u4e3a\u4e00\u4e2a Bean\uff0c\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0cSpring \u5bb9\u5668\u8d1f\u8d23\u5bf9\u8c61\u7684\u521b\u5efa\u3001\u7ba1\u7406\u3001\u88c5\u914d\u4ee5\u53ca\u6574\u4e2a\u751f\u547d\u5468\u671f\u7684\u63a7\u5236\u3002\u8fd9\u7bc7\u6587\u7ae0\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u5206\u6790 Spring Bean\u3002<\/p>\n\n\n\n<p><strong>1. \u4ec0\u4e48\u662f Spring Bean\uff1f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Spring Bean \u662f\u5728 Spring \u5bb9\u5668\u4e2d\u5b9e\u4f8b\u5316\u3001\u7ec4\u88c5\u548c\u7ba1\u7406\u7684\u5bf9\u8c61\u3002\u5b83\u4eec\u901a\u5e38\u662f\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4e1a\u52a1\u903b\u8f91\u3001\u6570\u636e\u8bbf\u95ee\u3001\u670d\u52a1\u7b49\u529f\u80fd\u7684\u5177\u4f53\u5b9e\u73b0\u3002\u901a\u8fc7\u5b9a\u4e49 Bean\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5229\u7528 Spring \u63d0\u4f9b\u7684\u4f9d\u8d56\u6ce8\u5165\uff08Dependency Injection\uff09\u548c\u9762\u5411\u5207\u9762\u7f16\u7a0b\uff08Aspect-Oriented Programming\uff09\u7b49\u7279\u6027\uff0c\u7b80\u5316\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u548c\u7ef4\u62a4\u3002<\/p>\n\n\n\n<p><strong>2. \u5982\u4f55\u5b9a\u4e49 Spring Bean\uff1f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>2.1 \u57fa\u4e8e XML \u7684\u914d\u7f6e<\/strong><\/p>\n\n\n\n<p>\u4f20\u7edf\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u5728 XML \u914d\u7f6e\u6587\u4ef6\u4e2d\u58f0\u660e Bean\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;beans>\n  &lt;bean id=\"myBean\" class=\"com.example.MyClass\">\n      &lt;property name=\"propertyName\" value=\"propertyValue\"\/>\n  &lt;\/bean>\n&lt;\/beans><\/pre>\n\n\n\n<p><strong>2.2 \u57fa\u4e8e\u6ce8\u89e3\u7684\u914d\u7f6e<\/strong><\/p>\n\n\n\n<p>\u4f7f\u7528\u6ce8\u89e3\u6765\u6807\u8bc6 Bean\uff0c\u4f8b\u5982 <code>@Component<\/code>\u3001<code>@Service<\/code>\u3001<code>@Repository<\/code> \u7b49\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Component\npublic class MyBean {\n  \/\/ ...\n}<\/pre>\n\n\n\n<p>\u5e76\u5728\u914d\u7f6e\u7c7b\u4e2d\u542f\u7528\u6ce8\u89e3\u626b\u63cf\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Configuration\n@ComponentScan(basePackages = \"com.example\")\npublic class AppConfig {\n  \/\/ ...\n}<\/pre>\n\n\n\n<p><strong>2.3 \u57fa\u4e8e Java \u914d\u7f6e<\/strong><\/p>\n\n\n\n<p>\u4f7f\u7528 <code>@Bean<\/code> \u6ce8\u89e3\u5728\u914d\u7f6e\u7c7b\u4e2d\u663e\u5f0f\u58f0\u660e Bean\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Configuration\npublic class AppConfig {\n  \n  @Bean\n  public MyBean myBean() {\n      return new MyBean();\n  }\n}\n\n<\/pre>\n\n\n\n<p><strong>3. Spring Bean \u7684\u751f\u547d\u5468\u671f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Spring \u5bb9\u5668\u5bf9 Bean \u7684\u751f\u547d\u5468\u671f\u8fdb\u884c\u7ba1\u7406\uff0c\u5305\u62ec\u521b\u5efa\u3001\u521d\u59cb\u5316\u3001\u9500\u6bc1\u7b49\u9636\u6bb5\u3002\u4ee5\u4e0b\u662f Bean \u751f\u547d\u5468\u671f\u7684\u4e3b\u8981\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<ol>\n<li><strong>\u5b9e\u4f8b\u5316<\/strong>\uff1a\u901a\u8fc7\u53cd\u5c04\u673a\u5236\u521b\u5efa Bean \u7684\u5b9e\u4f8b\u3002<\/li>\n\n\n\n<li><strong>\u4f9d\u8d56\u6ce8\u5165<\/strong>\uff1a\u5c06 Bean \u6240\u9700\u7684\u4f9d\u8d56\uff08\u5176\u4ed6 Bean \u6216\u8d44\u6e90\uff09\u6ce8\u5165\u5230 Bean \u4e2d\u3002<\/li>\n\n\n\n<li><strong>\u521d\u59cb\u5316<\/strong>\uff1a\u5982\u679c Bean \u5b9e\u73b0\u4e86 <code>InitializingBean<\/code> \u63a5\u53e3\u6216\u901a\u8fc7 <code>init-method<\/code> \u6307\u5b9a\u4e86\u521d\u59cb\u5316\u65b9\u6cd5\uff0cSpring \u4f1a\u8c03\u7528\u76f8\u5e94\u7684\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528<\/strong>\uff1aBean \u88ab\u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528\u3002<\/li>\n\n\n\n<li><strong>\u9500\u6bc1<\/strong>\uff1a\u5728\u5bb9\u5668\u5173\u95ed\u65f6\uff0c\u5982\u679c Bean \u5b9e\u73b0\u4e86 <code>DisposableBean<\/code> \u63a5\u53e3\u6216\u901a\u8fc7 <code>destroy-method<\/code> \u6307\u5b9a\u4e86\u9500\u6bc1\u65b9\u6cd5\uff0cSpring \u4f1a\u8c03\u7528\u8fd9\u4e9b\u65b9\u6cd5\u8fdb\u884c\u6e05\u7406\u3002<\/li>\n<\/ol>\n\n\n\n<p><strong>4. \u4f5c\u7528\u57df\uff08Scope\uff09<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Spring Bean \u53ef\u4ee5\u6709\u4e0d\u540c\u7684\u4f5c\u7528\u57df\uff0c\u51b3\u5b9a\u4e86 Bean \u7684\u5b9e\u4f8b\u5316\u65b9\u5f0f\u548c\u751f\u547d\u5468\u671f\u3002\u5e38\u89c1\u7684\u4f5c\u7528\u57df\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul>\n<li><strong>Singleton\uff08\u5355\u4f8b\uff09<\/strong>\uff1a\u9ed8\u8ba4\u4f5c\u7528\u57df\uff0c\u6574\u4e2a Spring \u5bb9\u5668\u4e2d\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u3002<\/li>\n\n\n\n<li><strong>Prototype\uff08\u539f\u578b\uff09<\/strong>\uff1a\u6bcf\u6b21\u8bf7\u6c42\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b9e\u4f8b\u3002<\/li>\n\n\n\n<li><strong>Request<\/strong>\uff1a\u5728 Web \u5e94\u7528\u4e2d\uff0c\u6bcf\u4e2a HTTP \u8bf7\u6c42\u5bf9\u5e94\u4e00\u4e2a Bean \u5b9e\u4f8b\u3002<\/li>\n\n\n\n<li><strong>Session<\/strong>\uff1a\u5728 Web \u5e94\u7528\u4e2d\uff0c\u6bcf\u4e2a HTTP \u4f1a\u8bdd\u5bf9\u5e94\u4e00\u4e2a Bean \u5b9e\u4f8b\u3002<\/li>\n\n\n\n<li><strong>Global Session<\/strong>\uff1a\u5728\u57fa\u4e8e portlet \u7684 Web \u5e94\u7528\u4e2d\uff0c\u6bcf\u4e2a\u5168\u5c40 HTTP \u4f1a\u8bdd\u5bf9\u5e94\u4e00\u4e2a Bean \u5b9e\u4f8b\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>5.\u4f9d\u8d56\u6ce8\u5165\uff08Dependency Injection\uff09<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Spring Bean \u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\u901a\u8fc7\u4f9d\u8d56\u6ce8\u5165\u8fdb\u884c\u7ba1\u7406\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b 3\u79cd\u6ce8\u5165\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<p><strong>5.1 \u6784\u9020\u5668\u6ce8\u5165<\/strong><\/p>\n\n\n\n<p>\u901a\u8fc7\u6784\u9020\u51fd\u6570\u4f20\u9012\u4f9d\u8d56\u3002\u5982\u4e0b\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Component\npublic class ServiceA {\n    private final ServiceB serviceB;\n    \n    @Autowired\n    public ServiceA(ServiceB serviceB) {\n        this.serviceB = serviceB;\n    }\n    \/\/ ...\n}<\/pre>\n\n\n\n<p><strong>5.2 Setter \u65b9\u6cd5\u6ce8\u5165<\/strong><\/p>\n\n\n\n<p>\u901a\u8fc7 Setter \u65b9\u6cd5\u4f20\u9012\u4f9d\u8d56\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Component\npublic class ServiceA {\n    private ServiceB serviceB;\n    \n    @Autowired\n    public void setServiceB(ServiceB serviceB) {\n        this.serviceB = serviceB;\n    }\n    \/\/ ...\n}<\/pre>\n\n\n\n<p><strong>5.3 \u5b57\u6bb5\u6ce8\u5165<\/strong><\/p>\n\n\n\n<p>\u76f4\u63a5\u5728\u5b57\u6bb5\u4e0a\u4f7f\u7528 <code>@Autowired<\/code> \u6ce8\u89e3\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Component\npublic class ServiceA {\n    @Autowired\n    private ServiceB serviceB;\n    \/\/ ...\n}<\/pre>\n\n\n\n<p><strong>6. \u81ea\u52a8\u88c5\u914d<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Spring \u63d0\u4f9b\u4e86\u81ea\u52a8\u88c5\u914d\uff08Autowiring\uff09\u673a\u5236\uff0c\u901a\u8fc7\u89e3\u6790 Bean \u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u81ea\u52a8\u5b8c\u6210\u4f9d\u8d56\u6ce8\u5165\u3002\u5e38\u89c1\u7684\u81ea\u52a8\u88c5\u914d\u6a21\u5f0f\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul>\n<li><strong>byName<\/strong>\uff1a\u6839\u636e Bean \u7684\u540d\u79f0\u8fdb\u884c\u88c5\u914d\u3002<\/li>\n\n\n\n<li><strong>byType<\/strong>\uff1a\u6839\u636e Bean \u7684\u7c7b\u578b\u8fdb\u884c\u88c5\u914d\u3002<\/li>\n\n\n\n<li><strong>constructor<\/strong>\uff1a\u901a\u8fc7\u6784\u9020\u51fd\u6570\u8fdb\u884c\u88c5\u914d\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4f7f\u7528\u6ce8\u89e3\u5982 <code>@Autowired<\/code>\u3001<code>@Qualifier<\/code> \u7b49\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u63a7\u5236\u88c5\u914d\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<p><strong>7. \u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Spring Bean \u662f\u6784\u5efa Spring \u5e94\u7528\u7a0b\u5e8f\u7684\u57fa\u7840\u5355\u4f4d\uff0c\u901a\u8fc7\u5b83\u4eec\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5229\u7528 Spring \u63d0\u4f9b\u7684\u5f3a\u5927\u529f\u80fd\uff0c\u5b9e\u73b0\u677e\u8026\u5408\u3001\u53ef\u7ef4\u62a4\u548c\u53ef\u6d4b\u8bd5\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u7406\u89e3\u548c\u5408\u7406\u4f7f\u7528 Spring Bean \u7684\u5b9a\u4e49\u3001\u914d\u7f6e\u548c\u7ba1\u7406\uff0c\u662f\u9ad8\u6548\u4f7f\u7528 Spring \u6846\u67b6\u7684\u5173\u952e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1aSpring Bean\u662f\u4ec0\u4e48\uff1f\u5341\u5206\u949f\u5e26\u4f60\u4e86\u89e3\uff01 Spring Bean \u662f Spring \u6846\u67b6\u4e2d\u7684\u6838\u5fc3\u6982 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,27],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16781"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=16781"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16781\/revisions"}],"predecessor-version":[{"id":16782,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16781\/revisions\/16782"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}