{"id":530,"date":"2021-09-22T15:26:31","date_gmt":"2021-09-22T07:26:31","guid":{"rendered":"https:\/\/explause.com\/?p=530"},"modified":"2021-09-22T15:26:31","modified_gmt":"2021-09-22T07:26:31","slug":"bean%e4%bd%9c%e7%94%a8%e5%9f%9f","status":"publish","type":"post","link":"https:\/\/devdoge.org\/?p=530","title":{"rendered":"Bean\u4f5c\u7528\u57df"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Spring\u63d0\u4f9b6\u79cd\u4f5c\u7528\u57df\uff0c\u5176\u4e2d4\u79cd\u53ea\u80fd\u7528\u4e8eWEB\u5e94\u7528\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table aligncenter is-style-stripes\"><table><tbody><tr><td>singleton<\/td><td>\u9ed8\u8ba4\u7684\u4f5c\u7528\u57df\uff0c\u6bcf\u4e2a\u5bb9\u5668\u5185\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff1b<\/td><\/tr><tr><td>prototype<\/td><td>\u8bf7\u6c42\u4e00\u6b21\u5c31\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b9e\u4f8b\uff1b<\/td><\/tr><tr><td>request<\/td><td>bean\u751f\u547d\u5468\u671f\u7ed1\u5b9a\u5355\u4e2ahttp\u8bf7\u6c42\uff0c\u6bcf\u4e2ahttp\u8bf7\u6c42\u90fd\u6709\u81ea\u5df1\u7684\u5b9e\u4f8b\u3002<br>\u4ec5\u5728\u7528\u4e8eweb\u7684\u5bb9\u5668\u5185\u6709\u6548\uff1b<\/td><\/tr><tr><td>session<\/td><td>bean\u751f\u547d\u5468\u671f\u7ed1\u5b9a\u5355\u4e2ahttp\u4f1a\u8bdd\u3002\u4ec5\u5728\u7528\u4e8eweb\u7684\u5bb9\u5668\u5185\u6709\u6548\uff1b<\/td><\/tr><tr><td>application<\/td><td>bean\u751f\u547d\u5468\u671f\u7ed1\u5b9a\u5355\u4e2aServletContext\u3002\u4ec5\u5728\u7528\u4e8eweb\u7684\u5bb9\u5668\u5185\u6709\u6548\uff1b<\/td><\/tr><tr><td>websocket<\/td><td>bean\u751f\u547d\u5468\u671f\u7ed1\u5b9a\u5355\u4e2aWebSocket\u3002\u4ec5\u5728\u7528\u4e8eweb\u7684\u5bb9\u5668\u5185\u6709\u6548\uff1b<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6ce8\u610f\uff1a\u5728prototype\u4f5c\u7528\u57df\u4e2d\uff0c\u867d\u7136\u751f\u547d\u5468\u671f\u65b9\u6cd5\u4f1a\u88ab\u8c03\u7528\uff0c\u4f46\u662f\u9500\u6bc1\u7684\u65b9\u6cd5\u4e0d\u4f1a\u88ab\u8c03\u7528\uff0c\u7528\u6237\u5fc5\u987b\u624b\u52a8\u91ca\u653e\u5e76\u6e05\u7406\u8fd9\u79cdbean\u6253\u5f00\u7684\u8d44\u6e90\u3002\u53ef\u4ee5\u8003\u8651\u5b9a\u5236\u4e00\u4e2a&nbsp;bean post-processor\u3002\u67d0\u4e9b\u65f6\u5019prototype\u4f5c\u7528\u57df\u4ec5\u4ec5\u662f\u66ff\u4ee3\u4e86new\u64cd\u4f5c\u7b26\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>request<\/code>,\u00a0<code>session<\/code>,\u00a0<code>application<\/code>, \u548c<code>websocket<\/code>\u4f5c\u7528\u57df\u90fd\u548cwebMVC\u76f8\u5173\u3002<\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>Singleton\u4f5c\u7528\u57df<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bb9\u5668\u5185\u53ea\u6709\u4e00\u4e2abean\u5b9e\u4f8b\uff0c\u8bf7\u6c42\u8fd9\u4e2abean\u90fd\u8fd4\u56de\u540c\u4e00\u4e2a\u5b9e\u4f8b\u3002\u8fd9\u79cd<meta charset=\"utf-8\"><strong>Singleton<\/strong>\u7684bean\u88ab\u5b58\u50a8\u4e8ecache\u4e2d\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"398\" src=\"https:\/\/explause.com\/wp-content\/uploads\/2021\/09\/singleton.png\" alt=\"\" class=\"wp-image-534\" srcset=\"https:\/\/devdoge.org\/wp-content\/uploads\/2021\/09\/singleton.png 800w, https:\/\/devdoge.org\/wp-content\/uploads\/2021\/09\/singleton-300x149.png 300w, https:\/\/devdoge.org\/wp-content\/uploads\/2021\/09\/singleton-768x382.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><figcaption>Singleton\u793a\u610f\u56fe<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Spring\u7684<meta charset=\"utf-8\"><strong>Singleton<\/strong>\u6700\u597d\u79f0\u505a\u4e00\u4e2a\u5bb9\u5668\u4e00\u4e2abean\uff0c\u4e0d\u540c\u5bb9\u5668\u4e4b\u95f4\u4e92\u4e0d\u5f71\u54cd\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e5f\u662fSpring\u7684\u9ed8\u8ba4\u4f5c\u7528\u57df\uff0c\u5728xml\u6587\u4ef6\u4e2d\u5b9a\u4e49\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;bean id=\"accountService\" class=\"com.something.DefaultAccountService\"\/>\n\n&lt;!-- \u4e0a\u4e0b\u7b49\u4ef7 -->\n&lt;bean id=\"accountService\" class=\"com.something.DefaultAccountService\" scope=\"singleton\"\/><\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>Prototype\u4f5c\u7528\u57df<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6bcf\u6b21\u8bf7\u6c42\u8fd9\u4e2abean\u90fd\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b9e\u4f8b\u3002<br>\u4e00\u4e2a\u5e38\u7528\u7684\u89c4\u5219\u662f\uff1a\u5c06<strong>prototype<\/strong>\u4f5c\u7528\u57df\u7528\u4e8e\u6709\u72b6\u6001\u7684bean\uff0c\u800c<strong>singleton<\/strong>\u7528\u4e8e\u65e0\u72b6\u6001\u7684bean\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"397\" src=\"https:\/\/explause.com\/wp-content\/uploads\/2021\/09\/prototype.png\" alt=\"\" class=\"wp-image-537\" srcset=\"https:\/\/devdoge.org\/wp-content\/uploads\/2021\/09\/prototype.png 800w, https:\/\/devdoge.org\/wp-content\/uploads\/2021\/09\/prototype-300x149.png 300w, https:\/\/devdoge.org\/wp-content\/uploads\/2021\/09\/prototype-768x381.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><figcaption>Prototype\u793a\u610f\u56fe<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4e86\u548c\u522b\u7684\u4f5c\u7528\u57df\u505a\u51fa\u533a\u5206\uff0cSpring\u5e76\u4e0d\u5b8c\u5168\u63a5\u7ba1prototype bean\u7684\u751f\u547d\u5468\u671f\uff0c<br>\u5bb9\u5668\u53ea\u7ba1\u5b9e\u4f8b\u5316\u3001\u8bbe\u7f6e\u6216\u8005\u88c5\u914d\uff0c\u4ea4\u7531\u7528\u6237\u4e4b\u540e\u5c31\u4e0d\u518d\u7ba1\u3002\u6240\u4ee5\u9500\u6bc1\u751f\u547d\u5468\u671f\u8c03\u7528\u4e0d\u4f1a\u751f\u6548\u3002<br>\u7528\u6237\u5fc5\u987b\u624b\u52a8\u91ca\u653e\u5df2\u7ecf\u6253\u5f00\u7684\u8d44\u6e90\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u8fd8\u662f\u60f3\u8ba9Spring\u5bb9\u5668\u91ca\u653e\u8d44\u6e90\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u4e00\u4e2a\u5b9a\u5236\u7684bean post-processor\u3002<br><meta charset=\"utf-8\">\u67d0\u4e9b\u65f6\u5019prototype\u4f5c\u7528\u57df\u4ec5\u4ec5\u662f\u66ff\u4ee3\u4e86Java\u7684new\u64cd\u4f5c\u7b26\u3002<\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>Singleton\u7684bean\u6709Prototype\u7684\u4f9d\u8d56<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c06<meta charset=\"utf-8\"><strong>Prototype<\/strong>\u7684bean\u6ce8\u5165<meta charset=\"utf-8\"><strong>Singleton<\/strong>\u7684bean\uff0c\u53ea\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b9e\u4f8b\u5c31\u597d\uff0c\u4f46\u662f\u53cd\u8fc7\u6765\u4f1a\u6bd4\u8f83\u9ebb\u70e6\u3002<br>\u8fd9\u65f6\u5019\u9700\u8981\u4f7f\u7528\u65b9\u6cd5\u6ce8\u5165\u3002<\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>\u5b9a\u5236\u4e00\u4e2a\u4f5c\u7528\u57df<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">bean\u4f5c\u7528\u57df\u673a\u5236\u662f\u53ef\u62d3\u5c55\u7684\uff0c\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684\u4f5c\u7528\u57df\uff0c\u4f46\u662f\u4e0d\u53ef\u4ee5\u8986\u76d6\u5185\u7f6e\u7684\u4f5c\u7528\u57df\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u521b\u5efa\u4e00\u4e2a\u4f5c\u7528\u57df\uff1a\u5b9e\u73b0<strong>org.springframework.beans.factory.config.Scope<\/strong>\u63a5\u53e3\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Scope\u63a5\u53e3\u6709\u56db\u4e2a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u83b7\u5f97\u3001\u79fb\u9664\u548c\u9500\u6bc1bean\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Object get(String name, ObjectFactory&lt;?> objectFactory);\nObject remove(String name);\nvoid registerDestructionCallback(String name, Runnable destructionCallback);\nString getConversationId();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f7f\u7528<\/strong><meta charset=\"utf-8\"><strong>\u5b9a\u5236<\/strong><meta charset=\"utf-8\"><strong>\u4f5c\u7528\u57df<\/strong>\uff1a\u901a\u8fc7ApplicationContext\u7684registerScope\u65b9\u6cd5\u6ce8\u518c\u81ea\u5df1\u7684\u4f5c\u7528\u57df\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void registerScope(String scopeName, Scope scope);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u5728xml\u914d\u7f6e\u91cc\u4f7f\u7528<meta charset=\"utf-8\">\u5b9a\u5236<meta charset=\"utf-8\">\u4f5c\u7528\u57df\u7684\u8bdd\u9700\u8981\u901a\u8fc7CustomScopeConfigurer\u7c7b\u6ce8\u518c\u4f5c\u7528\u57df\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?>\n&lt;beans xmlns=\"http:\/\/www.springframework.org\/schema\/beans\"\n    xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n    xmlns:aop=\"http:\/\/www.springframework.org\/schema\/aop\"\n    xsi:schemaLocation=\"http:\/\/www.springframework.org\/schema\/beans\n        https:&#47;&#47;www.springframework.org\/schema\/beans\/spring-beans.xsd\n        http:\/\/www.springframework.org\/schema\/aop\n        https:\/\/www.springframework.org\/schema\/aop\/spring-aop.xsd\">\n\n    &lt;bean class=\"org.springframework.beans.factory.config.CustomScopeConfigurer\">\n        &lt;property name=\"scopes\">\n            &lt;map>\n                &lt;entry key=\"thread\">\n                    &lt;bean class=\"org.springframework.context.support.SimpleThreadScope\"\/>\n                &lt;\/entry>\n            &lt;\/map>\n        &lt;\/property>\n    &lt;\/bean>\n\n    &lt;bean id=\"thing2\" class=\"x.y.Thing2\" scope=\"thread\">\n        &lt;property name=\"name\" value=\"Rick\"\/>\n        &lt;aop:scoped-proxy\/>\n    &lt;\/bean>\n\n    &lt;bean id=\"thing1\" class=\"x.y.Thing1\">\n        &lt;property name=\"thing2\" ref=\"thing2\"\/>\n    &lt;\/bean>\n\n&lt;\/beans><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6ce8\u610f\uff1a\u628a&lt;aop:scoped-proxy\/>\u653e\u5728\u5b9e\u73b0<strong>FactoryBean<\/strong>\u63a5\u53e3\u7684&lt;bean>\u58f0\u660e\u5185\uff0c<br>\u610f\u601d\u662f\u8fd9\u4e2abean\u81ea\u5df1\u7684\u4f5c\u7528\u57df\uff0c\u800c\u4e0d\u662f\u5b83\u8fd4\u56de\u7684bean\u7684\u4f5c\u7528\u57df\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a7\u5236bean\u5728\u5bb9\u5668\u5185\u7684\u5b58\u5728<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[28],"class_list":["post-530","post","type-post","status-publish","format-standard","hentry","category-ioc","tag-ioc"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":6}},"_links":{"self":[{"href":"https:\/\/devdoge.org\/index.php?rest_route=\/wp\/v2\/posts\/530","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devdoge.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devdoge.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devdoge.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/devdoge.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=530"}],"version-history":[{"count":8,"href":"https:\/\/devdoge.org\/index.php?rest_route=\/wp\/v2\/posts\/530\/revisions"}],"predecessor-version":[{"id":541,"href":"https:\/\/devdoge.org\/index.php?rest_route=\/wp\/v2\/posts\/530\/revisions\/541"}],"wp:attachment":[{"href":"https:\/\/devdoge.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devdoge.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=530"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devdoge.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}