{"id":16468,"date":"2024-10-25T12:54:12","date_gmt":"2024-10-25T04:54:12","guid":{"rendered":"https:\/\/92it.top\/?p=16468"},"modified":"2024-10-25T12:54:12","modified_gmt":"2024-10-25T04:54:12","slug":"spring%e5%88%9b%e5%bb%ba%e7%9a%84%e5%8d%95%e4%be%8b%e5%af%b9%e8%b1%a1%ef%bc%8c%e5%ad%98%e5%9c%a8%e7%ba%bf%e7%a8%8b%e5%ae%89%e5%85%a8%e9%97%ae%e9%a2%98%e5%90%97%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=16468","title":{"rendered":"Spring\u521b\u5efa\u7684\u5355\u4f8b\u5bf9\u8c61\uff0c\u5b58\u5728\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u5417\uff1f"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1a<a href=\"https:\/\/segmentfault.com\/a\/1190000044546069\"><a href=\"https:\/\/segmentfault.com\/a\/1190000044546069\">Spring\u521b\u5efa\u7684\u5355\u4f8b\u5bf9\u8c61\uff0c\u5b58\u5728\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u5417\uff1f<\/a><\/a><\/p>\n\n\n\n<p>\u8fd9\u4e2a\u95ee\u9898\u6d89\u53ca\u5230Spring\u6846\u67b6\u4e2d\u7684Bean\u7684\u4f5c\u7528\u57df\u3001\u5355\u4f8b\u6a21\u5f0f\u7684\u7ebf\u7a0b\u5b89\u5168\u6027\u4ee5\u53ca\u5982\u4f55\u5224\u65ad\u548c\u5904\u7406\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002\u8ba9\u6211\u4eec\u4e00\u6b65\u6b65\u6df1\u5165\u63a2\u8ba8\u8fd9\u4e9b\u6982\u5ff5\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"319\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-10-1024x319.png\" alt=\"\" class=\"wp-image-16469\" style=\"width:408px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-10-1024x319.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-10-300x93.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-10-768x239.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-10-830x259.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-10-230x72.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-10-350x109.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-10-480x150.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-10.png 1162w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p><strong>Spring Bean\u7684\u4f5c\u7528\u57df<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Spring\u63d0\u4f9b\u4e86\u51e0\u79cd\u4e0d\u540c\u7684Bean\u4f5c\u7528\u57df\uff0c\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul>\n<li><strong>1\u3001<\/strong>&nbsp;<strong>Singleton\uff08\u5355\u4f8b\uff09\uff1a<\/strong>&nbsp;\u9ed8\u8ba4\u4f5c\u7528\u57df\uff0c\u4fdd\u8bc1\u6bcf\u4e2aSpring\u5bb9\u5668\u4e2d\u53ea\u6709\u4e00\u4e2aBean\u5b9e\u4f8b\u3002<\/li>\n\n\n\n<li><strong>2\u3001<\/strong>&nbsp;<strong>Prototype\uff08\u539f\u578b\uff09\uff1a<\/strong>&nbsp;\u6bcf\u6b21\u8bf7\u6c42\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Bean\u5b9e\u4f8b\u3002<\/li>\n\n\n\n<li><strong>3\u3001<\/strong>&nbsp;<strong>Request\uff1a<\/strong>&nbsp;\u6bcf\u4e2aHTTP\u8bf7\u6c42\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Bean\uff0c\u4ec5\u5728web\u5e94\u7528\u4e2d\u6709\u6548\u3002<\/li>\n\n\n\n<li><strong>4\u3001<\/strong>&nbsp;<strong>Session\uff1a<\/strong>&nbsp;\u6bcf\u4e2aHTTP Session\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Bean\uff0c\u4ec5\u5728web\u5e94\u7528\u4e2d\u6709\u6548\u3002<\/li>\n\n\n\n<li><strong>5\u3001<\/strong>&nbsp;<strong>GlobalSession\uff1a<\/strong>&nbsp;\u5168\u5c40Session\u4f5c\u7528\u57df\uff0c\u4ec5\u5728Portlet\u73af\u5883\u4e2d\u6709\u6548\u3002<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"506\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-12-1024x506.png\" alt=\"\" class=\"wp-image-16471\" style=\"width:504px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-12-1024x506.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-12-300x148.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-12-768x380.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-12-830x410.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-12-230x114.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-12-350x173.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-12-480x237.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-12.png 1444w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p><strong>\u5355\u4f8bBean\u7684\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728Spring\u4e2d\uff0c\u9ed8\u8ba4\u7684Bean\u4f5c\u7528\u57df\u662f\u5355\u4f8b\uff08Singleton\uff09\u3002\u8fd9\u610f\u5473\u7740Spring\u5bb9\u5668\u53ea\u4e3a\u6bcf\u4e2a\u5b9a\u4e49\u7684Bean\u521b\u5efa\u4e00\u4e2a\u5b9e\u4f8b\u3002\u8fd9\u4e2a\u5355\u4f8b\u5b9e\u4f8b\u5728\u591a\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u5171\u4eab\uff0c\u56e0\u6b64\u7ebf\u7a0b\u5b89\u5168\u6027\u6210\u4e3a\u4e00\u4e2a\u5173\u6ce8\u70b9\u3002<\/p>\n\n\n\n<p><strong>\u521b\u5efa\u5355\u4f8b\u662f\u5426\u7ebf\u7a0b\u5b89\u5168<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Spring\u5bb9\u5668\u5728\u521b\u5efa\u5355\u4f8bBean\u65f6\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u5bb9\u5668\u786e\u4fdd\u5728\u6574\u4e2a\u8fc7\u7a0b\u4e2d\uff0cBean\u7684\u521d\u59cb\u5316\u53ea\u4f1a\u53d1\u751f\u4e00\u6b21\uff0c\u5373\u4f7f\u5728\u9ad8\u5e76\u53d1\u7684\u73af\u5883\u4e0b\u4e5f\u662f\u5982\u6b64\u3002<\/p>\n\n\n\n<p><strong>\u4f7f\u7528\u5355\u4f8b\u662f\u5426\u7ebf\u7a0b\u5b89\u5168<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5355\u4f8bBean\u7684\u7ebf\u7a0b\u5b89\u5168\u6027\u53d6\u51b3\u4e8eBean\u672c\u8eab\u7684\u5b9e\u73b0\u3002Spring\u4e0d\u4f1a\u5bf9\u5355\u4f8bBean\u7684\u72b6\u6001\u8fdb\u884c\u7ebf\u7a0b\u5b89\u5168\u5904\u7406\u3002\u5982\u679cBean\u6709\u5171\u4eab\u6570\u636e\u6216\u72b6\u6001\uff0c\u90a3\u4e48\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u4f7f\u7528\u65f6\u5c31\u9700\u8981\u5c0f\u5fc3\u3002<\/p>\n\n\n\n<p><strong>\u5224\u65ad\u548c\u5904\u7406\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>1\u3001<\/strong>\u00a0<strong>\u65e0\u72b6\u6001Bean\uff1a<\/strong>\u00a0\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u8ba9Bean\u4fdd\u6301\u65e0\u72b6\u6001\u3002\u8fd9\u610f\u5473\u7740Bean\u4e0d\u4fdd\u7559\u4efb\u4f55\u6570\u636e\uff08\u72b6\u6001\uff09\uff0c\u53ef\u4ee5\u88ab\u591a\u4e2a\u7ebf\u7a0b\u5b89\u5168\u5730\u5171\u4eab\u3002<\/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=\"\">@Service\npublic class StatelessService {\n    public void performAction(String input) {\n        \/\/ \u903b\u8f91\u5904\u7406\uff0c\u4e0d\u4fdd\u5b58\u4efb\u4f55\u72b6\u6001\n.....\n\n    }\n}<\/pre>\n\n\n\n<p><strong>2\u3001<\/strong>\u00a0<strong>\u7ebf\u7a0b\u5c40\u90e8\u53d8\u91cf\uff1a<\/strong>\u00a0\u5982\u679c\u5fc5\u987b\u4fdd\u7559\u72b6\u6001\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ThreadLocal<\/code>\u53d8\u91cf\u786e\u4fdd\u6bcf\u4e2a\u7ebf\u7a0b\u6709\u81ea\u5df1\u7684\u72b6\u6001\u526f\u672c\u3002<\/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=\"\">@Service\npublic class StatefulService {\n    private static final ThreadLocal&lt;MyState> stateHolder = ThreadLocal.withInitial(MyState::new);\n\n    public void performAction() {\n        MyState state = stateHolder.get();\n        \/\/ \u4f7f\u7528state\u8fdb\u884c\u64cd\u4f5c\n    }\n}<\/pre>\n\n\n\n<p><strong>3\u3001<\/strong>\u00a0<strong>\u540c\u6b65\u8bbf\u95ee\u63a7\u5236\uff1a<\/strong>\u00a0\u53e6\u4e00\u4e2a\u9009\u62e9\u662f\u4f7f\u7528\u540c\u6b65\u65b9\u6cd5\u6216\u540c\u6b65\u5757\u6765\u63a7\u5236\u5bf9\u72b6\u6001\u7684\u8bbf\u95ee\u3002<\/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=\"\">@Service\npublic class SynchronizedService {\n    private MyState state;\n\n    public synchronized void performAction() {\n        \/\/ \u540c\u6b65\u8bbf\u95ee\u6216\u4fee\u6539state\n    }\n}<\/pre>\n\n\n\n<p><strong>4\u3001\u7ebf\u7a0b\u72ec\u7acb\u6027<\/strong><\/p>\n\n\n\n<p>\u5c40\u90e8\u53d8\u91cf\u662f\u5728\u65b9\u6cd5\u5185\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u5b83\u4eec\u7684\u751f\u547d\u5468\u671f\u548c\u4f5c\u7528\u57df\u4ec5\u9650\u4e8e\u8be5\u65b9\u6cd5\u3002\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u8c03\u7528\u67d0\u4e2a\u65b9\u6cd5\u65f6\uff0c\u8be5\u65b9\u6cd5\u7684\u5c40\u90e8\u53d8\u91cf\u4f1a\u88ab\u5206\u914d\u5728\u6808\u5185\u5b58\u4e2d\uff0c\u5c5e\u4e8e\u8be5\u7ebf\u7a0b\u7684\u6808\u5e27\u3002\u56e0\u6b64\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u5728\u8c03\u7528\u540c\u4e00\u4e2a\u65b9\u6cd5\u65f6\u4f1a\u6709\u81ea\u5df1\u72ec\u7acb\u7684\u5c40\u90e8\u53d8\u91cf\u526f\u672c\uff0c\u4e0d\u4f1a\u5171\u4eab\u3002<\/p>\n\n\n\n<p>\u7531\u4e8e\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u81ea\u5df1\u7684\u6808\u548c\u5c40\u90e8\u53d8\u91cf\uff0c\u5c40\u90e8\u53d8\u91cf\u4e4b\u95f4\u4e0d\u4f1a\u51fa\u73b0\u7ade\u4e89\u6761\u4ef6\uff08race condition\uff09\u3002\u5373\u4f7f\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8c03\u7528\u540c\u4e00\u4e2a\u65b9\u6cd5\uff0c\u5b83\u4eec\u64cd\u4f5c\u7684\u662f\u5404\u81ea\u72ec\u7acb\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u56e0\u6b64\u4e0d\u4f1a\u53d1\u751f\u6570\u636e\u7ade\u4e89\u6216\u4e0d\u4e00\u81f4\u7684\u95ee\u9898\u3002<\/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=\"\">public class Singleton {\n    private static Singleton instance;\n\n    private Singleton() {}\n\n    public static synchronized Singleton getInstance() {\n        if (instance == null) {\n            instance = new Singleton();\n        }\n        return instance;\n    }\n\n    public void someMethod() {\n        int localVariable = 0; \/\/ \u8fd9\u4e2a\u53d8\u91cf\u662f\u5c40\u90e8\u53d8\u91cf\n        localVariable++;\n        System.out.println(localVariable);\n    }\n}\n<\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>someMethod<\/code> \u65b9\u6cd5\u4e2d\u7684 <code>localVariable<\/code> \u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\u3002\u5373\u4f7f\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8c03\u7528 <code>someMethod<\/code>\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u4f1a\u6709\u81ea\u5df1\u72ec\u7acb\u7684 <code>localVariable<\/code> \u526f\u672c\u3002\u56e0\u6b64\uff0c\u5b83\u4eec\u4e0d\u4f1a\u76f8\u4e92\u5f71\u54cd\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\u8ba1\u6570\u5668\u670d\u52a1<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u901a\u8fc7\u4e00\u4e2a\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u6765\u63a2\u8ba8Spring\u4e2d\u5355\u4f8bBean\u7684\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u8ba1\u6570\u5668\u670d\u52a1\uff0c\u8be5\u670d\u52a1\u5c06\u5728\u591a\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u5171\u4eab\uff0c\u5e76\u6307\u51fa\u5176\u4e2d\u53ef\u80fd\u51fa\u73b0\u7684\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u8ba1\u6570\u5668\u670d\u52a1\uff0c\u5b83\u7b80\u5355\u5730\u7edf\u8ba1\u4e86\u67d0\u4e2a\u64cd\u4f5c\u88ab\u8c03\u7528\u7684\u6b21\u6570\u3002<\/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=\"\">import org.springframework.stereotype.Service;\n\n@Service\npublic class CounterService {\n    private int count = 0;\n\n    public void increment() {\n        count++;\n    }\n\n    public int getCount() {\n        return count;\n    }\n}<\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<strong>CounterService<\/strong>\u662f\u4e00\u4e2aSpring\u7ba1\u7406\u7684\u5355\u4f8bBean\u3002\u5b83\u6709\u4e00\u4e2a<strong>count<\/strong>\u53d8\u91cf\u6765\u8ddf\u8e2a\u64cd\u4f5c\u88ab\u8c03\u7528\u7684\u6b21\u6570\u3002<strong>increment<\/strong>\u65b9\u6cd5\u7528\u4e8e\u589e\u52a0\u8ba1\u6570\u5668\uff0c<strong>getCount<\/strong>\u65b9\u6cd5\u7528\u4e8e\u83b7\u53d6\u5f53\u524d\u8ba1\u6570\u5668\u7684\u503c\u3002<\/p>\n\n\n\n<p><strong>\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898<\/strong><\/p>\n\n\n\n<p>\u8be5\u670d\u52a1\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u662f\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\u3002\u95ee\u9898\u51fa\u5728<strong>increment<\/strong>\u65b9\u6cd5\u4e0a\uff0c\u5f53\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u65f6\uff0c<strong>count<\/strong>\u53d8\u91cf\u7684\u589e\u52a0\u64cd\u4f5c\u53ef\u80fd\u4f1a\u4e92\u76f8\u5e72\u6270\uff0c\u5bfc\u81f4\u8ba1\u6570\u5668\u7684\u503c\u4e0d\u6b63\u786e\u3002<\/p>\n\n\n\n<p><strong>\u4e3a\u4ec0\u4e48\u4e0d\u5b89\u5168<\/strong><\/p>\n\n\n\n<p>\u5728Java\u4e2d\uff0c\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u4fee\u6539\u540c\u4e00\u4e2a\u53d8\u91cf\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002\u8fd9\u662f\u56e0\u4e3a<strong>count++<\/strong> \u64cd\u4f5c\u5e76\u4e0d\u662f\u539f\u5b50\u7684\u3002\u5b83\u5b9e\u9645\u4e0a\u5305\u542b\u4e86\u4e09\u4e2a\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<ol>\n<li>\u8bfb\u53d6<strong>count<\/strong>\u7684\u5f53\u524d\u503c\u3002<\/li>\n\n\n\n<li>\u589e\u52a0\u8fd9\u4e2a\u503c\u3002<\/li>\n\n\n\n<li>\u5c06\u65b0\u503c\u5199\u56de<strong>count<\/strong>\u53d8\u91cf\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u5982\u679c\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u6267\u884c\u8fd9\u4e2a\u64cd\u4f5c\uff0c\u5b83\u4eec\u53ef\u80fd\u8bfb\u53d6\u5230\u76f8\u540c\u7684<strong>count<\/strong>\u503c\uff0c\u7136\u540e\u5404\u81ea\u589e\u52a01\uff0c\u5e76\u5199\u56de\u3002\u8fd9\u5c06\u5bfc\u81f4<strong>count<\/strong>\u53ea\u589e\u52a0\u4e861\uff0c\u800c\u4e0d\u662f2\u3002<\/p>\n\n\n\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n\n\n\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<strong>synchronized<\/strong>\u5173\u952e\u5b57\u6765\u540c\u6b65\u5bf9<strong>count<\/strong>\u53d8\u91cf\u7684\u8bbf\u95ee\u3002<\/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=\"\">@Service\npublic class ThreadSafeCounterService {\n    private int count = 0;\n\n    public synchronized void increment() {\n        count++;\n    }\n\n    public int getCount() {\n        return count;\n    }\n}<\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4fee\u6b63\u7248\u672c\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5c06<strong>increment<\/strong>\u65b9\u6cd5\u58f0\u660e\u4e3a<strong>synchronized<\/strong>\u6765\u786e\u4fdd\u5728\u4efb\u4f55\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u80fd\u6267\u884c\u8fd9\u4e2a\u65b9\u6cd5\u3002\u8fd9\u786e\u4fdd\u4e86\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u4fee\u6539<strong>count<\/strong>\u53d8\u91cf\u65f6\uff0c\u4e0d\u4f1a\u6709\u5176\u4ed6\u7ebf\u7a0b\u540c\u65f6\u4fee\u6539\u5b83\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u793a\u4f8b\u5c55\u793a\u4e86\u5728Spring\u5355\u4f8bBean\u4e2d\u5982\u4f55\u56e0\u4e3a\u5171\u4eab\u72b6\u6001\u800c\u4ea7\u751f\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\uff0c\u4ee5\u53ca\u5982\u4f55\u901a\u8fc7\u540c\u6b65\u65b9\u6cd5\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u5728\u8bbe\u8ba1Spring\u5e94\u7528\u65f6\uff0c\u8003\u8651\u5e76\u89e3\u51b3\u8fd9\u7c7b\u95ee\u9898\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n\n\n\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Spring\u4e2d\u7684\u5355\u4f8bBean\u5728\u521b\u5efa\u65f6\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u4f46\u4f7f\u7528\u65f6\u7684\u7ebf\u7a0b\u5b89\u5168\u6027\u5b8c\u5168\u53d6\u51b3\u4e8eBean\u7684\u8bbe\u8ba1\u548c\u5b9e\u73b0\u3002\u4e3a\u4e86\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\uff0c\u53ef\u4ee5\u9009\u62e9\u65e0\u72b6\u6001\u7684\u8bbe\u8ba1\uff0c\u6216\u8005\u901a\u8fc7\u540c\u6b65\u673a\u5236\u3001\u7ebf\u7a0b\u5c40\u90e8\u53d8\u91cf\u7b49\u65b9\u5f0f\u6765\u5904\u7406\u72b6\u6001\u4fe1\u606f\u3002\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u6982\u5ff5\u662f\u786e\u4fddSpring\u5e94\u7528\u7ebf\u7a0b\u5b89\u5168\u7684\u5173\u952e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1aSpring\u521b\u5efa\u7684\u5355\u4f8b\u5bf9\u8c61\uff0c\u5b58\u5728\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u5417\uff1f \u8fd9\u4e2a\u95ee\u9898\u6d89\u53ca\u5230Spring\u6846\u67b6\u4e2d\u7684Bean\u7684\u4f5c\u7528\u57df\u3001\u5355 [&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\/16468"}],"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=16468"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16468\/revisions"}],"predecessor-version":[{"id":16472,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16468\/revisions\/16472"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}