{"id":11562,"date":"2023-11-28T22:24:52","date_gmt":"2023-11-28T14:24:52","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=11562"},"modified":"2023-12-11T11:19:07","modified_gmt":"2023-12-11T03:19:07","slug":"spring-zhi-chi-de-ji-zhongbean-de-zuo-yong-yu","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/spring-zhi-chi-de-ji-zhongbean-de-zuo-yong-yu\/","title":{"rendered":"Spring\u652f\u6301\u7684\u51e0\u79cdbean\u7684\u4f5c\u7528\u57df\uff1f"},"content":{"rendered":"<p>\u5728Spring\u6846\u67b6\u4e2d\uff0c\u652f\u6301\u7684\u51e0\u79cd\u5e38\u89c1\u7684Bean\u4f5c\u7528\u57df\u6709\u4ee5\u4e0b\u4e94\u79cd\uff1a<\/p>\n<ol>\n<li>\n<strong>Singleton\uff08\u5355\u4f8b\uff09\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728Spring\u4e2d\u662f\u9ed8\u8ba4\u7684\u4f5c\u7528\u57df\uff0c\u8868\u793a\u5728\u6574\u4e2a\u5e94\u7528\u7a0b\u5e8f\u4e2d\u53ea\u6709\u4e00\u4e2aBean\u5b9e\u4f8b\u3002\u8fd9\u4e2a\u5b9e\u4f8b\u5728\u5bb9\u5668\u542f\u52a8\u65f6\u521b\u5efa\uff0c\u4e4b\u540e\u4e00\u76f4\u5b58\u5728\u4e8e\u6574\u4e2a\u5e94\u7528\u7a0b\u5e8f\u7684\u751f\u547d\u5468\u671f\u4e2d\u3002\u6240\u6709\u5bf9\u8be5Bean\u7684\u8bf7\u6c42\u90fd\u8fd4\u56de\u540c\u4e00\u4e2a\u5b9e\u4f8b\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@Component\npublic class MySingletonBean {\n    \/\/ ...\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>Prototype\uff08\u539f\u578b\uff09\uff1a<\/strong><\/p>\n<ul>\n<li>\u6bcf\u6b21\u8bf7\u6c42\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Bean\u5b9e\u4f8b\u3002\u6bcf\u6b21\u8c03\u7528<code>getBean()<\/code>\u65b9\u6cd5\u6216\u6ce8\u5165\u4f9d\u8d56\u65f6\uff0c\u90fd\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684Bean\u5b9e\u4f8b\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@Component\n@Scope(&quot;prototype&quot;)\npublic class MyPrototypeBean {\n    \/\/ ...\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>Request\uff08\u8bf7\u6c42\uff09\uff1a<\/strong><\/p>\n<ul>\n<li>\u4ec5\u5728Web\u5e94\u7528\u7a0b\u5e8f\u4e2d\u6709\u6548\uff0c\u8868\u793a\u6bcf\u4e2aHTTP\u8bf7\u6c42\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Bean\u5b9e\u4f8b\u3002\u8be5Bean\u5b9e\u4f8b\u4ec5\u5728\u5f53\u524dHTTP\u8bf7\u6c42\u8303\u56f4\u5185\u6709\u6548\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@Component\n@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)\npublic class MyRequestScopedBean {\n    \/\/ ...\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>Session\uff08\u4f1a\u8bdd\uff09\uff1a<\/strong><\/p>\n<ul>\n<li>\u4ec5\u5728Web\u5e94\u7528\u7a0b\u5e8f\u4e2d\u6709\u6548\uff0c\u8868\u793a\u6bcf\u4e2aHTTP\u4f1a\u8bdd\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Bean\u5b9e\u4f8b\u3002\u8be5Bean\u5b9e\u4f8b\u5728\u6574\u4e2aHTTP\u4f1a\u8bdd\u671f\u95f4\u90fd\u6709\u6548\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@Component\n@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)\npublic class MySessionScopedBean {\n    \/\/ ...\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>Application\uff08\u5e94\u7528\u7a0b\u5e8f\uff09\uff1a<\/strong><\/p>\n<ul>\n<li>\u4ec5\u5728Web\u5e94\u7528\u7a0b\u5e8f\u4e2d\u6709\u6548\uff0c\u8868\u793a\u5728\u6574\u4e2aWeb\u5e94\u7528\u7a0b\u5e8f\u4e2d\u53ea\u5b58\u5728\u4e00\u4e2aBean\u5b9e\u4f8b\u3002\u8be5Bean\u5b9e\u4f8b\u5728\u6574\u4e2aWeb\u5e94\u7528\u7a0b\u5e8f\u7684\u751f\u547d\u5468\u671f\u5185\u90fd\u6709\u6548\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@Component\n@Scope(value = WebApplicationContext.SCOPE_APPLICATION, proxyMode = ScopedProxyMode.TARGET_CLASS)\npublic class MyApplicationScopedBean {\n    \/\/ ...\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u8fd9\u4e9b\u4f5c\u7528\u57df\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\uff0c\u7528\u4e8e\u63a7\u5236Bean\u5b9e\u4f8b\u7684\u751f\u547d\u5468\u671f\u548c\u53ef\u89c1\u8303\u56f4\u3002\u5728\u975eWeb\u73af\u5883\u4e2d\uff0c\u901a\u5e38\u4f7f\u7528Singleton\u548cPrototype\u4f5c\u7528\u57df\u3002\u800c\u5728Web\u73af\u5883\u4e2d\uff0c\u9664\u4e86Singleton\u548cPrototype\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528Request\u3001Session\u548cApplication\u4f5c\u7528\u57df\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Spring\u6846\u67b6\u4e2d\uff0c\u652f\u6301\u7684\u51e0\u79cd\u5e38\u89c1\u7684Bean\u4f5c\u7528\u57df\u6709\u4ee5\u4e0b\u4e94\u79cd\uff1a Singleton\uff08\u5355\u4f8b\uff09\uff1a \u5728Spring [&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-11562","post","type-post","status-publish","format-standard","hentry","category-spring"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11562","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=11562"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11562\/revisions"}],"predecessor-version":[{"id":17076,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11562\/revisions\/17076"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=11562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=11562"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=11562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}