{"id":11578,"date":"2023-11-28T22:25:55","date_gmt":"2023-11-28T14:25:55","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=11578"},"modified":"2023-12-11T11:23:52","modified_gmt":"2023-12-11T03:23:52","slug":"spring-zhong-ni-zen-yang-ding-yi-lei-de-zuo-yong-y","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/spring-zhong-ni-zen-yang-ding-yi-lei-de-zuo-yong-y\/","title":{"rendered":"Spring\u4e2d\u4f60\u600e\u6837\u5b9a\u4e49\u7c7b\u7684\u4f5c\u7528\u57df\uff1f"},"content":{"rendered":"<p>\u5728Spring\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>@Scope<\/code>\u6ce8\u89e3\u6216\u5728XML\u914d\u7f6e\u6587\u4ef6\u4e2d\u4f7f\u7528<code>&lt;bean&gt;<\/code>\u5143\u7d20\u7684<code>scope<\/code>\u5c5e\u6027\u6765\u5b9a\u4e49\u7c7b\u7684\u4f5c\u7528\u57df\u3002Spring\u652f\u6301\u591a\u79cd\u4f5c\u7528\u57df\uff0c\u5305\u62ecSingleton\uff08\u9ed8\u8ba4\uff09\u3001Prototype\u3001Request\u3001Session\u3001Global Session\u3001Application\u7b49\u3002<\/p>\n<h3><a id=\"%E4%BD%BF%E7%94%A8scope%E6%B3%A8%E8%A7%A3%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f7f\u7528<code>@Scope<\/code>\u6ce8\u89e3\uff1a<\/h3>\n<ol>\n<li>\n<strong>Singleton\uff08\u9ed8\u8ba4\uff09\uff1a<\/strong><\/p>\n<ul>\n<li>\u5355\u4f8b\u4f5c\u7528\u57df\u8868\u793a\u5728\u6574\u4e2a\u5e94\u7528\u7a0b\u5e8f\u4e2d\u53ea\u6709\u4e00\u4e2aBean\u5b9e\u4f8b\u3002\u8fd9\u662f\u9ed8\u8ba4\u7684\u4f5c\u7528\u57df\uff0c\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u4f5c\u7528\u57df\uff0cSpring\u4f1a\u5c06Bean\u914d\u7f6e\u4e3a\u5355\u4f8b\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">import org.springframework.context.annotation.Scope;\nimport org.springframework.stereotype.Component;\n\n@Component\n@Scope(&quot;singleton&quot;)\npublic class MySingletonBean {\n    \/\/ ...\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>Prototype\uff1a<\/strong><\/p>\n<ul>\n<li>\u539f\u578b\u4f5c\u7528\u57df\u8868\u793a\u6bcf\u6b21\u8bf7\u6c42\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Bean\u5b9e\u4f8b\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">import org.springframework.context.annotation.Scope;\nimport org.springframework.stereotype.Component;\n\n@Component\n@Scope(&quot;prototype&quot;)\npublic class MyPrototypeBean {\n    \/\/ ...\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3><a id=\"%E4%BD%BF%E7%94%A8xml%E9%85%8D%E7%BD%AE%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f7f\u7528XML\u914d\u7f6e\uff1a<\/h3>\n<ol>\n<li>\n<strong>Singleton\uff08\u9ed8\u8ba4\uff09\uff1a<\/strong><\/p>\n<pre><code class=\"language-xml\">&lt;bean id=&quot;mySingletonBean&quot; class=&quot;com.example.MySingletonBean&quot; \/&gt;\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>Prototype\uff1a<\/strong><\/p>\n<pre><code class=\"language-xml\">&lt;bean id=&quot;myPrototypeBean&quot; class=&quot;com.example.MyPrototypeBean&quot; scope=&quot;prototype&quot; \/&gt;\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n<ul>\n<li>\u5bf9\u4e8eWeb\u5e94\u7528\u7a0b\u5e8f\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528Request\u3001Session\u3001Global Session\u3001Application\u7b49\u4f5c\u7528\u57df\uff0c\u5177\u4f53\u4f7f\u7528\u65b9\u5f0f\u7c7b\u4f3c\u3002<\/li>\n<li>\u5728\u4f7f\u7528<code>@Scope<\/code>\u6ce8\u89e3\u65f6\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>proxyMode<\/code>\u5c5e\u6027\u6307\u5b9a\u4ee3\u7406\u6a21\u5f0f\uff0c\u4f8b\u5982<code>@Scope(value = &quot;prototype&quot;, proxyMode = ScopedProxyMode.TARGET_CLASS)<\/code>\u3002<\/li>\n<\/ul>\n<p>\u9009\u62e9\u4f5c\u7528\u57df\u65f6\uff0c\u8981\u8003\u8651Bean\u5b9e\u4f8b\u7684\u751f\u547d\u5468\u671f\u548c\u662f\u5426\u9700\u8981\u5171\u4eab\u72b6\u6001\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528Singleton\u4f5c\u7528\u57df\u662f\u6700\u5e38\u89c1\u7684\uff0c\u4f46\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\uff0c\u4f7f\u7528Prototype\u4f5c\u7528\u57df\u80fd\u591f\u66f4\u597d\u5730\u6ee1\u8db3\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Spring\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528@Scope\u6ce8\u89e3\u6216\u5728XML\u914d\u7f6e\u6587\u4ef6\u4e2d\u4f7f\u7528&lt;bean&gt;\u5143\u7d20\u7684scope\u5c5e [&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-11578","post","type-post","status-publish","format-standard","hentry","category-spring"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11578","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=11578"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11578\/revisions"}],"predecessor-version":[{"id":17083,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11578\/revisions\/17083"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=11578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=11578"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=11578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}