{"id":12614,"date":"2023-09-06T16:23:18","date_gmt":"2023-09-06T08:23:18","guid":{"rendered":"https:\/\/92it.top\/?p=12614"},"modified":"2023-09-06T16:23:18","modified_gmt":"2023-09-06T08:23:18","slug":"spring%e7%9a%84%e7%ba%bf%e7%a8%8b%e5%ae%89%e5%85%a8%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=12614","title":{"rendered":"Spring\u7684\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898"},"content":{"rendered":"\n<p><strong>\u6982\u8ff0<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u6211\u4eec\u77e5\u9053Spring IoC \u5bb9\u5668\u4e2d\u53ea\u4f1a\u5b58\u5728\u4e00\u4e2a bean \u7684\u5b9e\u4f8b\uff0c\u65e0\u8bba\u4e00\u6b21\u8c03\u7528\u8fd8\u662f\u591a\u6b21\u8c03\u7528\uff0c\u59cb\u7ec8\u6307\u5411\u7684\u90fd\u662f\u540c\u4e00\u4e2a bean \u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u5355\u5b9e\u4f8b\u6765\u8bf4\uff0c\u6240\u6709\u7ebf\u7a0b\u90fd\u5171\u4eab\u540c\u4e00\u4e2a bean \u5b9e\u4f8b\uff0c\u81ea\u7136\u5c31\u4f1a\u53d1\u751f\u8d44\u6e90\u7684\u4e89\u62a2\uff0c\u4ece\u800c\u5bfc\u81f4\u7ebf\u7a0b\u4e0d\u5b89\u5168\u3002<\/p>\n\n\n\n<p>\u4e3e\u4f8b\uff1a<br>\u65b0\u589e\u670d\u52a1\u7c7bThreadUnSafeService<\/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 ThreadUnSafeService {\n    public int i;\n\n    public void add() {\n        i++;\n    }\n\n    public void sub() {\n        i--;\n    }\n\n    public int getValue() {\n        return i;\n    }\n}\n<\/pre>\n\n\n\n<p>\u542f\u52a8\u5341\u4e2a\u7ebf\u7a0b\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u5404\u505a\u4e00\u5343\u6b21\u52a0\u51cf\uff1a<\/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 TestSpring {\n    public static void main(String[] args) {\n        ApplicationContext context = new ClassPathXmlApplicationContext(\n                new String[] { \"applicationContext.xml\" });\n\n        for (int j = 0; j &lt; 10; j++) {\n            new Thread(new Runnable() {\n                @Override\n                public void run() {\n                    ThreadUnSafeService service = (ThreadUnSafeService) context.getBean(\"threadUnSafeService\");\n                    for (int i = 0; i &lt; 1000; i++) {\n                        service.add();\n                    }\n                    for (int i = 0; i &lt; 1000; i++) {\n                        service.sub();\n                    }\n                    System.out.println(Thread.currentThread().getName() + \"-\" + service.getValue());\n                }\n            }).start();\n        }\n    }\n}\n<\/pre>\n\n\n\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/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=\"\">Thread-2-0\nThread-3-0\nThread-8-1201\nThread-10-663\nThread-1-1443\nThread-9-1380\nThread-7-1846\nThread-6-488\nThread-4-2507\nThread-5-2856\n<\/pre>\n\n\n\n<p>\u4ece\u7ed3\u679c\u53ef\u4ee5\u770b\u51fa\uff0c\u8fd0\u884c\u7ed3\u679c\u90fd\u4e0d\u662f 0\uff0c\u8fd9\u660e\u663e\u7684\u662f\u7ebf\u7a0b\u4e0d\u5b89\u5168\uff01\uff01<\/p>\n\n\n\n<p>\u56e0\u4e3a 10 \u4e2a\u7ebf\u7a0b\u83b7\u53d6\u7684 ThreadUnSafe \u5b9e\u4f8b\u90fd\u662f\u540c\u4e00\u4e2a\uff0c\u5e76\u4e14 10 \u4e2a\u7ebf\u7a0b\u90fd\u5bf9\u540c\u4e00\u4e2a\u8d44\u6e90 i \u53d1\u751f\u4e86\u4e89\u62a2\uff0c\u6240\u4ee5\u624d\u4f1a\u5bfc\u81f4\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u7684\u53d1\u751f\u3002<\/p>\n\n\n\n<p><strong>\u89e3\u51b3\u65b9\u6848\u4e00\uff1ascope \u7684\u503c\u6539\u4e3a prototype<\/strong><\/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\n@Scope(\"prototype\")\npublic class ThreadSafeService {\n    public int i;\n\n    public void add() {\n        i++;\n    }\n\n    public void sub() {\n        i--;\n    }\n\n    public int getValue() {\n        return i;\n    }\n}\n<\/pre>\n\n\n\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/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=\"\">Thread-3-0\nThread-1-0\nThread-7-0\nThread-2-0\nThread-4-0\nThread-6-0\nThread-5-0\nThread-8-0\nThread-9-0\nThread-10-0\n<\/pre>\n\n\n\n<p>prototype \u4f5c\u7528\u57df\u4e0b\uff0c\u6bcf\u6b21\u83b7\u53d6\u7684 ThreadUnSafe \u5b9e\u4f8b\u90fd\u4e0d\u540c\uff0c\u6240\u4ee5\u81ea\u7136\u4e0d\u4f1a\u6709\u7ebf\u7a0b\u5b89\u5168\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<p><strong>\u89e3\u51b3\u65b9\u6848\u4e8c\uff1a\u5c06bean\u6539\u4e3a\u65e0\u72b6\u6001\u7684<\/strong><\/p>\n\n\n\n<p><mark>\u65e0\u72b6\u6001bean\u5c31\u662f\u6ca1\u6709\u5171\u4eab\u53d8\u91cf<\/mark>\uff0c\u6240\u4ee5\u5c31\u4e0d\u4f1a\u4ea7\u751f\u7ebf\u7a0b\u5b89\u5168\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=\"\">@Service\npublic class ThreadSafeService {\n    public void getValue() {\n        int val = 0;\n        for (int i = 0; i &lt; 1000; i++) {\n            val++;\n        }\n        for (int i = 0; i &lt; 1000; i++) {\n            val--;\n        }\n        System.out.println(val);\n    }\n}\n<\/pre>\n\n\n\n<p>\u8c03\u7528\uff1a<\/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 TestSpring {\n    public static void main(String[] args) {\n        ApplicationContext context = new ClassPathXmlApplicationContext(\n                new String[] { \"applicationContext.xml\" });\n\n        for (int i = 0; i &lt; 10; i++) {\n            new Thread(new Runnable() {\n                @Override\n                public void run() {\n                    ThreadSafeService service = (ThreadSafeService) context.getBean(\"threadSafeService\");\n                    service.getValue();\n                }\n            }).start();\n        }\n    }\n}\n<\/pre>\n\n\n\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/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=\"\">0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n<\/pre>\n\n\n\n<p><strong>\u89e3\u51b3\u65b9\u6848\u4e09\uff1a\u52a0\u9501<\/strong><\/p>\n\n\n\n<p>\u65e2\u7136\u662f\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\uff0c\u90a3\u5c31\u52a0\u9501\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 TestSpring {\n    public static void main(String[] args) {\n        ApplicationContext context = new ClassPathXmlApplicationContext(\n                new String[]{\"applicationContext.xml\"});\n\n        for (int j = 0; j &lt; 10; j++) {\n            new Thread(new Runnable() {\n                @Override\n                public void run() {\n                    ThreadUnSafeService service = (ThreadUnSafeService) context.getBean(\"threadUnSafeService\");\n\n                    synchronized (service) {\n                        for (int i = 0; i &lt; 1000; i++) {\n                            service.add();\n                        }\n                        for (int i = 0; i &lt; 1000; i++) {\n                            service.sub();\n                        }\n                        System.out.println(Thread.currentThread().getName() + \"-\" + service.getValue());\n                    }\n                }\n            }).start();\n        }\n    }\n}\n<\/pre>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\u4e3a 0\u3002<\/p>\n\n\n\n<p>\u6beb\u65e0\u7591\u95ee\u52a0\u9501\u786e\u5b9e\u53ef\u4ee5\uff0c\u4f46\u662f\u52a0\u9501\u4f1a<mark>\u589e\u52a0\u6027\u80fd\u4e0a\u7684\u5f00\u9500<\/mark>\u3002<\/p>\n\n\n\n<p><strong>\u89e3\u51b3\u65b9\u6848\u56db\uff1aThreadLocal<\/strong><\/p>\n\n\n\n<p>ThreadLocal \u5728\u81ea\u5df1\u7ebf\u7a0b\u5185\u521b\u5efa\u4e00\u4e2a\u53d8\u91cf\u7684\u526f\u672c\uff0c\u89c4\u907f\u4e86\u7ebf\u7a0b\u5b89\u5168\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=\"\">@Service\npublic class ThreadUnSafeService {\n\n    public static ThreadLocal&lt;Integer> i = new ThreadLocal&lt;>();\n\n    public void add() {\n        Integer integer = i.get();\n        if (null == integer){\n            integer = 0;\n        }\n        integer++;\n        i.set(integer);\n    }\n\n    public void sub() {\n        Integer integer = i.get();\n        if (null == integer){\n            integer = 0;\n        }\n        integer--;\n        i.set(integer);\n    }\n\n    public int getValue() {\n        return i.get();\n    }\n}\n<\/pre>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\u4e3a 0\u3002<\/p>\n\n\n\n<p>\u4f46\u662fThreadLocal\u4e3a\u6bcf\u4e2a\u7ebf\u7a0b\u521b\u5efa\u53d8\u91cf\u7684\u526f\u672c\uff0c<mark>\u5e26\u6765\u4e86\u7a7a\u95f4\u4e0a\u7684\u5f00\u9500<\/mark>\u3002 <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u603b\u4e4b\uff0c\u89e3\u51b3\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u601d\u8def\u5206\u4e3a\u4e24\u79cd\u3002\n\u7b2c\u4e00\u79cd\uff0c\u6d88\u9664\u5171\u4eab\u53d8\u91cf\uff0c\u662f\u7ebf\u7a0b\u4e0d\u5b89\u5168\u95ee\u9898\u4e0d\u5b58\u5728\uff0c\u65b9\u6cd5\u6709\u5c06bean\u6539\u4e3a\u65e0\u72b6\u6001\u7684\u3001scope \u7684\u503c\u6539\u4e3a prototype\u3001ThreadLocal\u3002\n\u7b2c\u4e8c\u79cd\uff0c\u5c31\u662f\u52a0\u9501\u3002<\/pre>\n\n\n\n<p><strong>Spring\u81ea\u8eab\u65b9\u6848<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>\u65e0\u72b6\u6001\u7684bean<\/strong><\/p>\n\n\n\n<p>\u5728Spring\u4e2d\uff0c\u7edd\u5927\u90e8\u5206bean\u90fd\u662f\u65e0\u72b6\u6001\u7684\uff0c\u56e0\u6b64\u5373\u4f7f\u8fd9\u4e9bbean\u9ed8\u8ba4\u662f\u5355\u4f8b\u7684\uff0c\u4e5f\u4e0d\u4f1a\u51fa\u73b0\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u7684\u3002\u6bd4\u5982controller\u3001service\u3001dao\u8fd9\u4e9b\u7c7b\uff0c\u8fd9\u4e9b\u7c7b\u91cc\u9762\u901a\u5e38\u4e0d\u4f1a\u542b\u6709\u6210\u5458\u53d8\u91cf\uff0c\u56e0\u6b64\u5b83\u4eec\u88ab\u8bbe\u8ba1\u6210\u5355\u4f8b\u7684\u3002\u5982\u679c\u8fd9\u4e9b\u7c7b\u4e2d\u5b9a\u4e49\u4e86\u5b9e\u4f8b\u53d8\u91cf\uff0c\u5c31\u7ebf\u7a0b\u4e0d\u5b89\u5168\u4e86\uff0c\u6240\u4ee5\u5c3d\u91cf\u907f\u514d\u5b9a\u4e49\u5b9e\u4f8b\u53d8\u91cf\u3002<\/p>\n\n\n\n<p><br><strong>\u6709\u72b6\u6001\u7684bean<\/strong><\/p>\n\n\n\n<p>Spring\u4e2d\uff0c\u7edd\u5927\u90e8\u5206Bean\u90fd\u53ef\u4ee5\u58f0\u660e\u4e3asingleton\u4f5c\u7528\u57df\u3002\u5c31\u662f\u56e0\u4e3aSpring\u5bf9\u4e00\u4e9bBean\uff08\u5982RequestContextHolder\u3001TransactionSynchronizationManager\u3001LocaleContextHolder\u7b49\uff09\u4e2d\u975e\u7ebf\u7a0b\u5b89\u5168\u7684\u201c\u72b6\u6001\u6027\u5bf9\u8c61\u201d \u91c7\u7528ThreadLocal\u8fdb\u884c\u5c01\u88c5\uff0c\u8ba9\u5b83\u4eec\u4e5f\u6210\u4e3a\u7ebf\u7a0b\u5b89\u5168\u7684\u201c\u72b6\u6001\u6027\u5bf9\u8c61\u201d\uff0c\u56e0\u6b64\u6709\u72b6\u6001\u7684Bean\u5c31\u80fd\u591f\u4ee5singleton\u7684\u65b9\u5f0f\u5728\u591a\u7ebf\u7a0b\u4e2d\u6b63\u5e38\u5de5\u4f5c\u4e86\u3002<\/p>\n\n\n\n<p>\u4e00\u822c\u7684Web\u5e94\u7528\u5212\u5206\u4e3a\u5c55\u73b0\u5c42\u3001\u670d\u52a1\u5c42\u548c\u6301\u4e45\u5c42\u4e09\u4e2a\u5c42\u6b21\uff0c\u5728\u4e0d\u540c\u7684\u5c42\u4e2d\u7f16\u5199\u5bf9\u5e94\u7684\u903b\u8f91\uff0c\u4e0b\u5c42\u901a\u8fc7\u63a5\u53e3\u5411\u4e0a\u5c42\u5f00\u653e\u529f\u80fd\u8c03\u7528\u3002\u5728\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u4ece\u63a5\u6536\u8bf7\u6c42\u5230\u8fd4\u56de\u54cd\u5e94\u6240\u7ecf\u8fc7\u7684\u6240\u6709\u7a0b\u5e8f\u8c03\u7528\u90fd\u540c\u5c5e\u4e8e\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u5982\u56fe9-2\u6240\u793a\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-27.png\" alt=\"\" class=\"wp-image-12616\" width=\"371\" height=\"225\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-27.png 668w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-27-300x182.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-27-230x140.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-27-350x213.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-27-480x292.png 480w\" sizes=\"(max-width: 371px) 100vw, 371px\" \/><\/figure><\/div>\n\n\n\n<p>\u8fd9\u6837\u7528\u6237\u5c31\u53ef\u4ee5\u6839\u636e\u9700\u8981\uff0c\u5c06\u4e00\u4e9b\u975e\u7ebf\u7a0b\u5b89\u5168\u7684\u53d8\u91cf\u4ee5ThreadLocal\u5b58\u653e\uff0c\u5728\u540c\u4e00\u6b21\u8bf7\u6c42\u54cd\u5e94\u7684\u8c03\u7528\u7ebf\u7a0b\u4e2d\uff0c\u6240\u6709\u5bf9\u8c61\u6240\u8bbf\u95ee\u7684\u540c\u4e00ThreadLocal\u53d8\u91cf\u90fd\u662f\u5f53\u524d\u7ebf\u7a0b\u6240\u7ed1\u5b9a\u7684\u3002<br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8ff0 \u6211\u4eec\u77e5\u9053Spring IoC \u5bb9\u5668\u4e2d\u53ea\u4f1a\u5b58\u5728\u4e00\u4e2a bean \u7684\u5b9e\u4f8b\uff0c\u65e0\u8bba\u4e00\u6b21\u8c03\u7528\u8fd8\u662f\u591a\u6b21\u8c03\u7528\uff0c\u59cb\u7ec8\u6307\u5411\u7684 [&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\/12614"}],"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=12614"}],"version-history":[{"count":2,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/12614\/revisions"}],"predecessor-version":[{"id":12617,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/12614\/revisions\/12617"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}