{"id":10006356,"date":"2022-08-31T11:10:39","date_gmt":"2022-08-31T03:10:39","guid":{"rendered":"https:\/\/ityet.com\/?p=10006356"},"modified":"2022-08-31T11:10:39","modified_gmt":"2022-08-31T03:10:39","slug":"%e9%9d%a2%e8%af%95%e7%aa%81%e5%87%bb79%ef%bc%9abean-%e4%bd%9c%e7%94%a8%e5%9f%9f%e6%98%af%e5%95%a5%ef%bc%9f%e5%ae%83%e6%9c%89%e5%87%a0%e7%a7%8d%e7%b1%bb%e5%9e%8b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006356","title":{"rendered":"\u9762\u8bd5\u7a81\u51fb79\uff1aBean \u4f5c\u7528\u57df\u662f\u5565\uff1f\u5b83\u6709\u51e0\u79cd\u7c7b\u578b\uff1f"},"content":{"rendered":"<p>Spring \u6846\u67b6\u4f5c\u4e3a\u4e00\u4e2a\u7ba1\u7406 Bean \u7684 IoC \u5bb9\u5668\uff0c\u90a3\u4e48 Bean \u81ea\u7136\u662f Spring \u4e2d\u7684\u91cd\u8981\u8d44\u6e90\u4e86\uff0c\u90a3 Bean \u7684\u4f5c\u7528\u57df\u662f\u4ec0\u4e48\u610f\u601d\uff1f\u53c8\u6709\u51e0\u79cd\u7c7b\u578b\u5462\uff1f\u63a5\u4e0b\u6765\u6211\u4eec\u4e00\u8d77\u6765\u770b\u3002<\/p>\n<blockquote><p>PS\uff1aJava \u4e2d\u7684\u516c\u5171\u7c7b\u53ef\u79f0\u4e4b\u4e3a Bean \u6216 Java Bean\u3002<\/p><\/blockquote>\n<h2>1.\u4f5c\u7528\u57df<\/h2>\n<p><strong>Bean \u7684\u4f5c\u7528\u57df\u662f\u6307 Bean \u5728 Spring \u6574\u4e2a\u6846\u67b6\u4e2d\u7684\u67d0\u79cd\u884c\u4e3a\u6a21\u5f0f\u3002\u6bd4\u5982 singleton \u5355\u4f8b\u4f5c\u7528\u57df\uff0c\u5c31\u8868\u793a Bean \u5728\u6574\u4e2a Spring \u4e2d\u53ea\u6709\u4e00\u4efd\uff0c\u5b83\u662f\u5168\u5c40\u5171\u4eab\u7684\uff0c\u5f53\u6709\u4eba\u4fee\u6539\u4e86\u8fd9\u4e2a\u503c\u4e4b\u540e\uff0c\u90a3\u4e48\u53e6\u4e00\u4e2a\u4eba\u8bfb\u53d6\u5230\u7684\u5c31\u662f\u88ab\u4fee\u6539\u540e\u7684\u503c<\/strong>\u3002<br \/>\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u6bd4\u5982\u6211\u4eec\u5728 Spring \u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5355\u4f8b\u7684 Bean \u5bf9\u8c61 user\uff08\u9ed8\u8ba4\u4f5c\u7528\u57df\u4e3a\u5355\u4f8b\uff09\uff0c\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">@Component\npublic class UserBean {\n    @Bean\n    public User user() {\n        User user = new User();\n        user.setId(1);\n        user.setName(&quot;Java&quot;); \/\/ \u6b64\u884c\u4e3a\u91cd\u70b9\uff1a\u7528\u6237\u540d\u79f0\u4e3a Java\n        return user;\n    }\n}<\/code><\/pre>\n<p>\u7136\u540e\uff0c\u5728 A \u7c7b\u4e2d\u4f7f\u7528\u5e76\u4fee\u6539\u4e86 user \u5bf9\u8c61\uff0c\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">@Controller\npublic class AController {\n    @Autowired\n    private User user;\n    public User getUser() {\n        User user = user;\n        user.setName(&quot;MySQL&quot;); \/\/ \u6b64\u884c\u4e3a\u91cd\u70b9\uff1a\u5c06 user \u540d\u79f0\u4fee\u6539\u4e86\n        return user;\n    }\n}<\/code><\/pre>\n<p>\u6700\u540e\uff0c\u5728 B \u7c7b\u4e2d\u4e5f\u4f7f\u7528\u4e86 user \u5bf9\u8c61\uff0c\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">@Controller\npublic class BController {\n    @Autowired\n    private User user;\n    public User getUser() {\n        User user = user;\n        return user;\n    }\n}<\/code><\/pre>\n<p>\u6b64\u65f6\u6211\u4eec\u8bbf\u95ee B \u5bf9\u8c61\u4e2d\u7684 getUser \u65b9\u6cd5\uff0c\u5c31\u4f1a\u53d1\u73b0\u6b64\u65f6\u7684\u7528\u6237\u540d\u4e3a A \u7c7b\u4e2d\u4fee\u6539\u7684\u201cMySQL\u201d\uff0c\u800c\u975e\u539f\u6765\u7684\u201cJava\u201d\uff0c\u8fd9\u5c31\u8bf4\u660e Bean \u5bf9\u8c61 user \u9ed8\u8ba4\u5c31\u662f\u5355\u4f8b\u7684\u4f5c\u7528\u57df\u3002\u5982\u679c\u6709\u4efb\u4f55\u5730\u65b9\u4fee\u6539\u4e86\u8fd9\u4e2a\u5355\u4f8b\u5bf9\u8c61\uff0c\u90a3\u4e48\u5176\u4ed6\u7c7b\u518d\u8c03\u7528\u5c31\u4f1a\u5f97\u5230\u4e00\u4e2a\u4fee\u6539\u540e\u7684\u503c\u3002<\/p>\n<h2>2.\u4f5c\u7528\u57df\u5206\u7c7b<\/h2>\n<p>\u5728 Spring \u4e2d\uff0cBean \u7684\u5e38\u89c1\u4f5c\u7528\u57df\u6709\u4ee5\u4e0b 5 \u79cd\uff1a<\/p>\n<ol>\n<li>singleton\uff1a\u5355\u4f8b\u4f5c\u7528\u57df\uff1b<\/li>\n<li>prototype\uff1a\u539f\u578b\u4f5c\u7528\u57df\uff08\u591a\u4f8b\u4f5c\u7528\u57df\uff09\uff1b<\/li>\n<li>request\uff1a\u8bf7\u6c42\u4f5c\u7528\u57df\uff1b<\/li>\n<li>session\uff1a\u4f1a\u8bdd\u4f5c\u7528\u57df\uff1b<\/li>\n<li>application\uff1a\u5168\u5c40\u4f5c\u7528\u57df\u3002<\/li>\n<\/ol>\n<p><strong>\u6ce8\u610f\uff1a\u540e 3 \u79cd\u4f5c\u7528\u57df\uff0c\u53ea\u9002\u7528\u4e8e Spring MVC \u6846\u67b6\u3002<\/strong><\/p>\n<h3>2.1 singleton<\/h3>\n<ul>\n<li>\u5b98\u65b9\u8bf4\u660e\uff1a(Default) Scopes a single bean definition to a single object instance for each Spring IoC container.<\/li>\n<li>\u63cf\u8ff0\uff1a\u8be5\u4f5c\u7528\u57df\u4e0b\u7684 Bean \u5728 IoC \u5bb9\u5668\u4e2d\u53ea\u5b58\u5728\u4e00\u4e2a\u5b9e\u4f8b\uff1a\u83b7\u53d6 Bean\uff08\u5373\u901a\u8fc7 applicationContext.getBean\u7b49\u65b9\u6cd5\u83b7\u53d6\uff09\u53ca\u88c5\u914d Bean\uff08\u5373\u901a\u8fc7 @Autowired \u6ce8\u5165\uff09\u90fd\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002<\/li>\n<li>\u573a\u666f\uff1a\u901a\u5e38<strong>\u65e0\u72b6\u6001<\/strong>\u7684 Bean \u4f7f\u7528\u8be5\u4f5c\u7528\u57df\u3002\u65e0\u72b6\u6001\u8868\u793a Bean \u5bf9\u8c61\u7684\u5c5e\u6027\u72b6\u6001\u4e0d\u9700\u8981\u66f4\u65b0\u3002<\/li>\n<li>\u5907\u6ce8\uff1a<strong>Spring \u9ed8\u8ba4\u9009\u62e9\u8be5\u4f5c\u7528\u57df\u3002<\/strong><\/li>\n<\/ul>\n<h3>2.2 prototype<\/h3>\n<ul>\n<li>\u5b98\u65b9\u8bf4\u660e\uff1aScopes a single bean definition to any number of object instances.<\/li>\n<li>\u63cf\u8ff0\uff1a\u6bcf\u6b21\u5bf9\u8be5\u4f5c\u7528\u57df\u4e0b\u7684 Bean \u7684\u8bf7\u6c42\u90fd\u4f1a\u521b\u5efa\u65b0\u7684\u5b9e\u4f8b\uff1a\u83b7\u53d6 Bean\uff08\u5373\u901a\u8fc7 applicationContext.getBean \u7b49\u65b9\u6cd5\u83b7\u53d6\uff09\u53ca\u88c5\u914d Bean\uff08\u5373\u901a\u8fc7 @Autowired \u6ce8\u5165\uff09\u90fd\u662f\u65b0\u7684\u5bf9\u8c61\u5b9e\u4f8b\u3002<\/li>\n<li>\u573a\u666f\uff1a\u901a\u5e38<strong>\u6709\u72b6\u6001<\/strong>\u7684 Bean \u4f7f\u7528\u8be5\u4f5c\u7528\u57df\u3002<\/li>\n<\/ul>\n<h3>2.3 request<\/h3>\n<ul>\n<li>\u5b98\u65b9\u8bf4\u660e\uff1aScopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.<\/li>\n<li>\u63cf\u8ff0\uff1a\u6bcf\u6b21 Http \u8bf7\u6c42\u4f1a\u521b\u5efa\u65b0\u7684 Bean \u5b9e\u4f8b\uff0c\u7c7b\u4f3c\u4e8e prototype\u3002<\/li>\n<li>\u573a\u666f\uff1a\u4e00\u6b21 Http \u7684\u8bf7\u6c42\u548c\u54cd\u5e94\u7684\u5171\u4eab Bean\u3002<\/li>\n<li>\u5907\u6ce8\uff1a\u9650\u5b9a Spring MVC \u6846\u67b6\u4e2d\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<h3>2.4 session<\/h3>\n<ul>\n<li>\u5b98\u65b9\u8bf4\u660e\uff1aScopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.<\/li>\n<li>\u63cf\u8ff0\uff1a\u5728\u4e00\u4e2a Http Session \u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a Bean \u5b9e\u4f8b\u3002<\/li>\n<li>\u573a\u666f\uff1a\u7528\u6237\u4f1a\u8bdd\u7684\u5171\u4eab Bean, \u6bd4\u5982\uff1a\u8bb0\u5f55\u4e00\u4e2a\u7528\u6237\u7684\u767b\u9646\u4fe1\u606f\u3002<\/li>\n<li>\u5907\u6ce8\uff1a\u9650\u5b9a Spring MVC \u6846\u67b6\u4e2d\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<h3>2.5 application<\/h3>\n<ul>\n<li>\u5b98\u65b9\u8bf4\u660e\uff1aScopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.<\/li>\n<li>\u63cf\u8ff0\uff1a\u5728\u4e00\u4e2a Http Servlet Context \u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a Bean \u5b9e\u4f8b\u3002<\/li>\n<li>\u573a\u666f\uff1aWeb \u5e94\u7528\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\uff0c\u6bd4\u5982\uff1a\u8bb0\u5f55\u4e00\u4e2a\u5e94\u7528\u7684\u5171\u4eab\u4fe1\u606f\u3002<\/li>\n<li>\u5907\u6ce8\uff1a\u9650\u5b9a Spring MVC \u6846\u67b6\u4e2d\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<h2>3.\u4f5c\u7528\u57df\u8bbe\u7f6e<\/h2>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 @Scope  \u6ce8\u89e3\u6765\u8bbe\u7f6e Bean \u7684\u4f5c\u7528\u57df\uff0c\u5b83\u7684\u8bbe\u7f6e\u65b9\u5f0f\u6709\u4ee5\u4e0b\u4e24\u79cd\uff1a<\/p>\n<ol>\n<li>\u76f4\u63a5\u8bbe\u7f6e\u4f5c\u7528\u57df\u7684\u5177\u4f53\u503c\uff0c\u5982\uff1a@Scope(&#8220;prototype&#8221;)\uff1b<\/li>\n<li>\u8bbe\u7f6e ConfigurableBeanFactory \u548c WebApplicationContext \u63d0\u4f9b\u7684 SCOPE_XXX \u53d8\u91cf\uff0c\u5982 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)\u3002<\/li>\n<\/ol>\n<p>\u5177\u4f53\u8bbe\u7f6e\u4ee3\u7801\u5982\u4e0b\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042406746\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042406747\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042406748\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>Bean \u7684\u4f5c\u7528\u57df\u662f\u6307 Bean \u5728 Spring \u6574\u4e2a\u6846\u67b6\u4e2d\u7684\u67d0\u79cd\u884c\u4e3a\u6a21\u5f0f\u3002Bean \u7684\u5e38\u89c1\u4f5c\u7528\u57df\u6709 5 \u79cd\uff1asingleton\uff08\u5355\u4f8b\u4f5c\u7528\u57df\uff09\u3001prototype\uff08\u539f\u578b\u4f5c\u7528\u57df\uff09\u3001request\uff08\u8bf7\u6c42\u4f5c\u7528\u57df\uff09\u3001session\uff08\u8bf7\u6c42\u4f5c\u7528\u57df\uff09\u3001application\uff08\u5168\u5c40\u4f5c\u7528\u57df\uff09\uff0c\u6ce8\u610f\u540e 3 \u79cd\u4f5c\u7528\u57df\u53ea\u9002\u7528\u4e8e Spring MVC \u6846\u67b6\u3002<\/p>\n<blockquote>\n<p>\u662f\u975e\u5ba1\u4e4b\u4e8e\u5df1\uff0c\u6bc1\u8a89\u542c\u4e4b\u4e8e\u4eba\uff0c\u5f97\u5931\u5b89\u4e4b\u4e8e\u6570\u3002<\/p>\n<p>\u516c\u4f17\u53f7\uff1aJava\u9762\u8bd5\u771f\u9898\u89e3\u6790<\/p>\n<p>\u9762\u8bd5\u5408\u96c6\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=ZwwLbSTiG0xYqR6iqfP0gQ%3D%3D.QALKxoBAWISxsViGmzWc6fcnMZCiLmrimqU%2BgPsEhAVc16FM8Mde41OHP68ZYw9M\" rel=\"nofollow\">https:\/\/gitee.com\/mydb\/interview<\/a><\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042406744\">https:\/\/segmentfault.com\/a\/1190000042406744<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring \u6846\u67b6\u4f5c\u4e3a\u4e00\u4e2a\u7ba1\u7406 Bean \u7684 IoC \u5bb9\u5668\uff0c\u90a3\u4e48 Bean \u81ea\u7136\u662f Spring \u4e2d\u7684\u91cd\u8981\u8d44\u6e90 [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006356","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006356","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006356"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006356\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}