{"id":12619,"date":"2023-09-06T17:31:23","date_gmt":"2023-09-06T09:31:23","guid":{"rendered":"https:\/\/92it.top\/?p=12619"},"modified":"2023-09-07T08:37:56","modified_gmt":"2023-09-07T00:37:56","slug":"springmvc%e4%b8%ad%e7%9a%84controller%e5%92%8cservice%e9%ab%98%e5%b9%b6%e5%8f%91%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=12619","title":{"rendered":"SpringMVC\u4e2d\u7684@Controller\u548c@Service\u9ad8\u5e76\u53d1\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898"},"content":{"rendered":"\n<p><strong>\u80cc\u666f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Spring Bean\u7684scope\u9ed8\u8ba4\u662fsingleton\uff08\u5355\u4f8b\uff09\u6a21\u5f0f\uff0c\u5bb9\u5668\u672c\u8eab\u5e76\u6ca1\u6709\u63d0\u4f9bBean\u7684\u7ebf\u7a0b\u5b89\u5168\u7b56\u7565\uff0c\u56e0\u6b64Spring\u5bb9\u5668\u4e2d\u7684\u9ed8\u8ba4Bean\u672c\u8eab\u7ebf\u7a0b\u4e0d\u5b89\u5168\u3002<\/p>\n\n\n\n<p>\u4e4b\u524d\u7684\u6587\u7ae0 <a href=\"https:\/\/blog.csdn.net\/weixin_40017062\/article\/details\/125265251\">Spring\u7684\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898<\/a> \u4e5f\u8bf4\u660e\u4e86Bean\u5728\u591a\u7ebf\u7a0b\u5982\u679c\u6709\u5171\u4eab\u53d8\u91cf\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\u793a\u4f8b\u4ee5\u53ca\u89e3\u51b3\u5355\u4f8b\u6a21\u5f0fBean\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\u65b9\u6848\u3002<\/p>\n\n\n\n<ul><li>scope \u7684\u503c\u6539\u4e3a prototype<\/li><li>bean\u6539\u4e3a\u65e0\u72b6\u6001\u7684<\/li><li>\u52a0\u9501<\/li><li>ThreadLocal<\/li><\/ul>\n\n\n\n<p><strong>Spring\u5e76\u53d1\u8bbf\u95ee\u7684\u7ebf\u7a0b\u5b89\u5168\u6027\u95ee\u9898&nbsp;&nbsp;<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u7531\u4e8eSpring MVC\u9ed8\u8ba4\u662f<a href=\"https:\/\/so.csdn.net\/so\/search?q=Singleton&amp;spm=1001.2101.3001.7020\" target=\"_blank\" rel=\"noreferrer noopener\">Singleton<\/a>\u7684\uff0c\u6240\u4ee5\u4f1a\u4ea7\u751f\u4e00\u4e2a\u6f5c\u5728\u7684\u5b89\u5168\u9690\u60a3\u3002\u6839\u672c\u6838\u5fc3\u662finstance\u53d8\u91cf\u4fdd\u6301\u72b6\u6001\u7684\u95ee\u9898\u3002\u8fd9\u610f\u5473\u7740\u6bcf\u4e2arequest\u8fc7\u6765\uff0c\u7cfb\u7edf\u90fd\u4f1a\u7528\u539f\u6709\u7684instance\u53bb\u5904\u7406\uff0c\u8fd9\u6837\u5bfc\u81f4\u4e86\u4e24\u4e2a\u7ed3\u679c\uff1a<\/p>\n\n\n\n<ul><li>\u4e00\u662f\u6211\u4eec\u4e0d\u7528\u6bcf\u6b21\u521b\u5efaController\uff0c<\/li><li>\u4e8c\u662f\u51cf\u5c11\u4e86\u5bf9\u8c61\u521b\u5efa\u548c\u5783\u573e\u6536\u96c6\u7684\u65f6\u95f4\uff1b<\/li><\/ul>\n\n\n\n<p>\u7531\u4e8e\u53ea\u6709\u4e00\u4e2aController\u7684instance\uff0c\u5f53\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8c03\u7528\u5b83\u7684\u65f6\u5019\uff0c\u5b83\u91cc\u9762\u7684instance\u53d8\u91cf\u5c31\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u4e86\uff0c\u4f1a\u53d1\u751f\u7a9c\u6570\u636e\u7684\u95ee\u9898\u3002<br>\u5f53\u7136\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u6839\u672c\u4e0d\u9700\u8981\u8003\u8651\u7ebf\u7a0b\u5b89\u5168\u7684\u95ee\u9898\uff0c\u6bd4\u5982dao,service\u7b49\uff0c\u9664\u975e\u5728bean\u4e2d\u58f0\u660e\u4e86\u5b9e\u4f8b\u53d8\u91cf\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5728\u4f7f\u7528spring mvc \u7684contrller\u65f6\uff0c\u5e94\u907f\u514d\u5728controller\u4e2d\u5b9a\u4e49\u5b9e\u4f8b\u53d8\u91cf\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 Controller extends AbstractCommandController {\n   protectedCompany company;\n   \n   ......\n   \n   protected ModelAndView handle(HttpServletRequest request, \n                                 HttpServletResponse response,\n                                 Object command, BindException errors)throwsException{\n      company =................;\n   }\n}<\/pre>\n\n\n\n<p>\u5728\u8fd9\u91cc\u6709\u58f0\u660e\u4e00\u4e2a\u53d8\u91cfcompany\uff0c\u8fd9\u91cc\u5c31\u5b58\u5728\u5e76\u53d1\u7ebf\u7a0b\u5b89\u5168\u7684\u95ee\u9898\u3002\u5982\u679c\u63a7\u5236\u5668\u662f\u4f7f\u7528\u5355\u4f8b\u5f62\u5f0f\uff0c\u4e14controller\u4e2d\u6709\u4e00\u4e2a\u79c1\u6709\u7684\u53d8\u91cfcompany,\u6240\u6709\u8bf7\u6c42\u5230\u540c\u4e00\u4e2acontroller\u65f6\uff0c\u4f7f\u7528\u7684company\u53d8\u91cf\u662f\u5171\u7528\u7684\uff0c\u5373\u82e5\u662f\u67d0\u4e2a\u8bf7\u6c42\u4e2d\u4fee\u6539\u4e86\u8fd9\u4e2a\u53d8\u91cfcompany\uff0c\u5219\uff0c\u5728\u522b\u7684\u8bf7\u6c42\u4e2d\u80fd\u591f\u8bfb\u5230\u8fd9\u4e2a\u4fee\u6539\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<p><strong>\u6709\u51e0\u79cd\u89e3\u51b3\u65b9\u6cd5<\/strong>\uff1a<\/p>\n\n\n\n<ul><li>\u5728\u63a7\u5236\u5668\u4e2d\u4e0d\u4f7f\u7528\u5b9e\u4f8b\u53d8\u91cf <\/li><\/ul>\n\n\n\n<ul><li>\u5c06\u63a7\u5236\u5668\u7684\u4f5c\u7528\u57df\u4ece\u5355\u4f8b\u6539\u4e3a\u539f\u578b\uff0c\u5373\u5728spring\u914d\u7f6e\u6587\u4ef6Controller\u4e2d\u58f0\u660escope=&#8221;prototype&#8221;\uff0c\u6bcf\u6b21\u90fd\u521b\u5efa\u65b0\u7684controller <\/li><\/ul>\n\n\n\n<ul><li>\u5728Controller\u4e2d\u4f7f\u7528ThreadLocal\u53d8\u91cf<\/li><\/ul>\n\n\n\n<p>\u8fd9\u51e0\u79cd\u505a\u6cd5\u6709\u597d\u6709\u574f\uff1a<\/p>\n\n\n\n<p><strong>\u7b2c\u4e00\u79cd<\/strong>\uff1a\u9700\u8981\u5f00\u53d1\u4eba\u5458\u62e5\u6709\u8f83\u9ad8\u7684\u7f16\u7a0b\u6c34\u5e73\u4e0e\u601d\u60f3\u610f\u8bc6\uff0c\u5728\u7f16\u7801\u8fc7\u7a0b\u4e2d\u529b\u6c42\u907f\u514d\u51fa\u73b0\u8fd9\u79cdBUG\uff0c <\/p>\n\n\n\n<p>\u597d\u5904\u662f\u5b9e\u4f8b\u5bf9\u8c61\u53ea\u6709\u4e00\u4e2a\uff0c\u6240\u6709\u7684\u8bf7\u6c42\u90fd\u8c03\u7528\u8be5\u5b9e\u4f8b\u5bf9\u8c61\uff0c\u901f\u5ea6\u548c\u6027\u80fd\u4e0a\u8981\u4f18\u4e8e\u7b2c\u4e8c\u79cd\uff0c \u4e0d\u597d\u7684\u5730\u65b9\uff0c\u5c31\u662f\u9700\u8981\u7a0b\u5e8f\u5458\u81ea\u5df1\u53bb\u63a7\u5236\u5b9e\u4f8b\u53d8\u91cf\u7684\u72b6\u6001\u4fdd\u6301\u95ee\u9898\u3002<\/p>\n\n\n\n<p><strong>\u7b2c\u4e8c\u79cd<\/strong>\uff1a\u5219\u662f\u5bb9\u5668\u81ea\u52a8\u7684\u5bf9\u6bcf\u4e2a\u8bf7\u6c42\u4ea7\u751f\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u7531JVM\u8fdb\u884c\u5783\u573e\u56de\u6536\uff0c\u56e0\u6b64\u505a\u5230\u4e86\u7ebf\u7a0b\u5b89\u5168\u3002<br>\u7531\u4e8e\u6bcf\u6b21\u8bf7\u6c42\u90fd\u521b\u5efa\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u6240\u4ee5\u4f1a\u6d88\u8017\u8f83\u591a\u7684\u5185\u5b58\u7a7a\u95f4\u3002<br><\/p>\n\n\n\n<p>\u6240\u4ee5\u5728\u4f7f\u7528spring\u5f00\u53d1web \u65f6\u8981\u6ce8\u610f\uff0c\u9ed8\u8ba4Controller\u3001Dao\u3001Service\u90fd\u662f\u5355\u4f8b\u7684<\/p>\n\n\n\n<p><strong>\u90a3\u4eca\u5929\u804a\u4e0b spring\u5355\u4f8b\uff0c\u4e3a\u4ec0\u4e48controller\u3001service\u548cdao\u786e\u80fd\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\uff1f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u9996\u5148\uff0c\u770b\u4e00\u4e0b\u8ba4\u8bc1\u63a7\u5236\u7c7b<strong>PcAuthController \uff1a<\/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=\"\">@RestController\npublic class PcAuthController extends BaseController {\n    private static final Logger LOG = LoggerFactory.getLogger(PcAuthController.class);\n    \n    @Autowired\n    PcCardAuth4Service cardAuth4Service;\n\t\n    \/\/controller\u5c42\u5bf9\u5916\u66b4\u9732\u63a5\u53e3\uff0c\u5c3d\u91cf\u4e0d\u8981\u505a\u4e1a\u52a1\u5904\u7406\uff0c\u901a\u8fc7\u7edf\u4e00\u4e0a\u4e0b\u6587BusinessContext\u627f\u8f7d\u6570\u636e\uff0c\u8c03\u7528service\u6a21\u5757\u5904\u7406\n    @RequestMapping(value = \"\/milkyway-pc\/pcng-auth-cardAuth4\", method = RequestMethod.POST)\n    public PcCardAuth4Resp cardAuth4(@RequestBody PcCardAuth4Req req) {\n    \n        \/\/ \u4e1a\u52a1\u8c03\u7528\u903b\u8f91\u7f16\u6392\n        cardAuth4Service.transaction(bizContext);\n    }\n}\n<\/pre>\n\n\n\n<p>\u63a7\u5236\u7c7bPcAuthController\u6ce8\u5165\u670d\u52a1\u7c7bPcCardAuth4Service\uff0c\u6bcf\u6b21\u63a5\u6536\u5230 \u201c\/milkyway-pc\/pcng-auth-cardAuth4\u201d \u7684\u8bf7\u6c42\uff0c\u5747\u4f1a\u8c03\u7528\u65b9\u6cd5cardAuth4\u3002<\/p>\n\n\n\n<p>\u6bcf\u4e2a\u7ebf\u7a0b\u8c03\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61\u7684\u540c\u4e00\u4e2a\u65b9\u6cd5\uff0c\u90fd\u62e5\u6709\u81ea\u5df1\u7684\u7ebf\u7a0b\u6808\uff0c\u8fd9\u4e2a\u7ebf\u7a0b\u6808\u5305\u542b\u4e86\u8fd9\u4e2a\u7ebf\u7a0b\u8c03\u7528\u7684\u65b9\u6cd5\u5f53\u524d\u6267\u884c\u70b9\u76f8\u5173\u7684\u4fe1\u606f\uff0c\u5373\u4f7f\u4e24\u4e2a\u7ebf\u7a0b\u6267\u884c\u540c\u6837\u7684\u4ee3\u7801\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u521b\u5efa\u7684\u672c\u5730\u53d8\u91cf\u5bf9\u5176\u5b83\u7ebf\u7a0b\u4e0d\u53ef\u89c1\uff0c\u4ec5\u81ea\u5df1\u53ef\u89c1\u3002<\/p>\n\n\n\n<p>\u6bcf\u4e2a\u8bf7\u6c42\u90fd\u662f\u5355\u72ec\u7684\u7ebf\u7a0b\uff0c\u5373\u4f7f\u540c\u65f6\u8bbf\u95ee\u540c\u4e00\u4e2aController\u5bf9\u8c61\uff0c\u56e0\u4e3a\u5e76\u6ca1\u6709\u4fee\u6539Controller\u5bf9\u8c61\uff0c\u76f8\u5f53\u4e8e\u9488\u5bf9Controller\u5bf9\u8c61\u800c\u8a00\uff0c\u53ea\u662f\u8bfb\u64cd\u4f5c\uff0c\u6ca1\u6709\u5199\u64cd\u4f5c\uff0c\u4e0d\u9700\u8981\u505a\u540c\u6b65\u5904\u7406\u3002<\/p>\n\n\n\n<p>\u65b9\u6cd5cardAuth4\u5185\u90e8\u8c03\u7528\u6210\u5458\u53d8\u91cfcardAuth4Service\u7684\u65b9\u6cd5transaction()\uff0c\u591a\u7ebf\u7a0b\u573a\u666f\u4e0b\uff0c\u6210\u5458\u53d8\u91cf\u4fee\u6539\u4f1a\u7ebf\u7a0b\u4e0d\u5b89\u5168\uff0c\u4f46\u53ea\u662f\u8c03\u7528\u6210\u5458\u53d8\u91cfcardAuth4Service\u7684\u65b9\u6cd5transaction()\uff0c\u5e76\u6ca1\u6709\u4fee\u6539PcCardAuth4Service\u5bf9\u8c61\uff0c\u6545\u4e5f\u4e0d\u5b58\u5728\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u8ba4\u8bc1\u670d\u52a1\u7c7b<strong>PcCardAuth4Service \uff1a<\/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\npublic class PcCardAuth4Service extends BaseFeignService {\n\n    private static final Logger LOG = LoggerFactory.getLogger(PcCardAuth4Service.class);\n\n    \/**\n     * socket\u4ea4\u6613\u901a\u8bafIP\n     *\/\n    @Value(\"${service.socketUppJson.ip}\")\n    private String socketIp;\n\n    \/**\n     * socket\u4ea4\u6613\u901a\u8baf\u7aef\u53e3\n     *\/\n    @Value(\"${service.socketUppJson.port}\")\n    private String socketPort;\n\n    @Autowired\n    private PcTranDaoService pcTranDaoService;\n\n\n    @Override\n    public boolean transaction(IBizContext bizContext) {\n        LOG.info(\"PcCardAuth4Service enter !!!\");\n\n        \/\/ TODO somethings\n    }\n}\n<\/pre>\n\n\n\n<p><br><strong>Service<\/strong>\u5c42\u3001<strong>Dao<\/strong>\u5c42\u7528\u9ed8\u8ba4<strong>singleton<\/strong>\u5c31\u884c\uff0c\u867d\u7136Service\u7c7b\u4e5f\u6709dao\u8fd9\u6837\u7684\u5c5e\u6027\uff0c\u4f46dao\u8fd9\u4e9b\u7c7b\u90fd\u662f\u6ca1\u6709\u72b6\u6001\u4fe1\u606f\u7684\uff0c\u4e5f\u5c31\u662f \u76f8\u5f53\u4e8e\u4e0d\u53d8(immutable)\u7c7b\uff0c\u6240\u4ee5\u4e0d\u5f71\u54cd\u3002<\/p>\n\n\n\n<p><strong>SpringMVC\u9ed8\u8ba4\u521b\u5efabean\u662f\u5355\u4f8b\u7684\uff0c\u9ad8\u5e76\u53d1\u60c5\u51b5\u4e0b\uff0c\u5982\u4f55\u4fdd\u8bc1\u6027\u80fd\u7684\uff1f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u548b\u4e00\u60f3\uff0c\u56e0\u4e3aSpringMVC\u9ed8\u8ba4\u521b\u5efabean\u662f\u5355\u4f8b\u7684\uff0c\u9ad8\u5e76\u53d1\u65f6\uff0c\u4f1a\u4e0d\u4f1a\u56e0\u4e3a\u90fd\u8981\u8bbf\u95ee\u540c\u4e00\u4e2abean\u800c\u5f71\u54cd\u6027\u80fd\u5462\u3002<\/p>\n\n\n\n<p>\u5b9e\u8d28\u4e0a\u8fd9\u79cd\u7406\u89e3\u662f\u9519\u8bef\u7684\uff0cJava\u91cc\u6709\u4e2aAPI\u53eb\u505aThreadLocal\uff0cspring\u5355\u4f8b\u6a21\u5f0f\u4e0b\u7528\u5b83\u6765\u5207\u6362\u4e0d\u540c\u7ebf\u7a0b\u4e4b\u95f4\u7684\u53c2\u6570\u3002\u7528ThreadLocal\u662f\u4e3a\u4e86\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\uff0c\u5b9e\u9645\u4e0aThreadLoacal\u7684key\u5c31\u662f\u5f53\u524d\u7ebf\u7a0b\u7684Thread\u5b9e\u4f8b\u3002\u5355\u4f8b\u6a21\u5f0f\u4e0b\uff0cspring\u628a\u6bcf\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u5b58\u5728\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u7684\u53c2\u6570\u503c\u653e\u8fdb\u4e86ThreadLocal\u3002\u8fd9\u6837\u867d\u7136\u662f\u4e00\u4e2a\u5b9e\u4f8b\u5728\u64cd\u4f5c\uff0c\u4f46\u662f\u4e0d\u540c\u7ebf\u7a0b\u4e0b\u7684\u6570\u636e\u4e92\u76f8\u4e4b\u95f4\u90fd\u662f\u9694\u79bb\u7684\uff0c\u56e0\u4e3a\u8fd0\u884c\u65f6\u521b\u5efa\u548c\u9500\u6bc1\u7684bean\u5927\u5927\u51cf\u5c11\u4e86\uff0c\u6240\u4ee5\u5927\u591a\u6570\u573a\u666f\u4e0b\u8fd9\u79cd\u65b9\u5f0f\u5bf9\u5185\u5b58\u8d44\u6e90\u7684\u6d88\u8017\u8f83\u5c11\uff0c\u800c\u4e14\u5e76\u53d1\u8d8a\u9ad8\u4f18\u52bf\u8d8a\u660e\u663e\u3002<\/p>\n\n\n\n<p>\u603b\u7684\u6765\u8bf4\u5c31\u662f\uff0c\u5355\u4f8b\u6a21\u5f0f\u56e0\u4e3a\u5927\u5927\u8282\u7701\u4e86\u5b9e\u4f8b\u7684\u521b\u5efa\u548c\u9500\u6bc1\u7684\u65f6\u95f4\uff0c\u6709\u5229\u4e8e\u63d0\u9ad8\u6027\u80fd\uff0c\u800cThreadLocal\u7528\u6765\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u6027\u3002<\/p>\n\n\n\n<p>\u5355\u4f8b\u6a21\u5f0f\u662fspring\u63a8\u8350\u7684\u914d\u7f6e\uff0c\u5b83\u5728\u9ad8\u5e76\u53d1\u4e0b\u80fd\u6781\u5927\u7684\u8282\u7701\u8d44\u6e90\uff0c\u63d0\u9ad8\u670d\u52a1\u6297\u538b\u80fd\u529b\u3002<\/p>\n\n\n\n<p><strong>@Controller\/@Service\u4f7f\u7528\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6ce8\u5165\u7684Bean\u5bf9\u8c61\uff0cscope\u503c\u662f\u5355\u4f8b-singleton\u7684\uff0c\u7ebf\u7a0b\u4e0d\u5b89\u5168\u3002<br>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c@Controller\/@Service\uff0cscope\u503c\u662f\u5355\u4f8b-singleton\u7684\uff0c\u4e5f\u662f\u7ebf\u7a0b\u4e0d\u5b89\u5168\u3002<br>\u5c3d\u91cf\u4e0d\u8981\u5728@Controller\/@Service\u7b49\u5bb9\u5668\u4e2d\u5b9a\u4e49\u9759\u6001\u53d8\u91cf\uff0c\u4e0d\u8bba\u662f\u5355\u4f8b(singleton)\u8fd8\u662f\u591a\u5b9e\u4f8b(prototype)\u4ed6\u90fd\u662f\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\u3002<br>\u4e00\u5b9a\u8981\u5b9a\u4e49\u53d8\u91cf\u7684\u8bdd\uff0c\u7528ThreadLocal\u6765\u5c01\u88c5\uff0c\u8fd9\u4e2a\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/p>\n\n\n\n<p><strong>\u591a\u7ebf\u7a0b\u64cd\u4f5c\u540c\u4e00\u4e2a\u5bf9\u8c61\u5e76\u4e14\u8c03\u7528\u76f8\u540c\u7684\u65b9\u6cd5\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u4f1a\u963b\u585e\uff1f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<pre class=\"wp-block-preformatted\">\u8fd9\u4e2a\u95ee\u9898\u7684\u7f18\u8d77\u662f spring mvc \u9ed8\u8ba4\u662f\u5355\u4f8b\u7684\uff0c\u6240\u4ee5\u6211\u4ea7\u751f\u4e86\u8fd9\u6837\u4e00\u4e2a\u7591\u95ee\uff1f\u5728\u9ad8\u5e76\u53d1\u4e0b\uff0c\u662f\u5355\u4f8b\u6a21\u5f0f\u6548\u7387\u9ad8\u8fd8\u662f\u591a\u4f8b\u6a21\u5f0f\u6548\u7387\u9ad8\uff1f\u4e4b\u6240\u4ee5\u6709\u8fd9\u4e2a\u7591\u95ee\u662f\u56e0\u4e3a\u6211\u8ba4\u4e3a\u5355\u4f8b\u6a21\u5f0f\u4e0b\uff0c\u5f53\u4e00\u4e2a\u8bf7\u6c42\u5728\u5904\u7406\u4e2d\u7684\u65f6\u5019\uff0c\u63a5\u4e0b\u6765\u7684\u8bf7\u6c42\u4f1a\u5904\u4e8e\u7b49\u5f85\u4e2d\uff08\u5c0f\u767d\uff0c\u5927\u5bb6\u52ff\u7b11\uff09\u3002\u4e4b\u540e\u6709\u5927\u795e\u544a\u8bc9\u6211\u4e0d\u4f1a\u963b\u585e\uff0c\u56e0\u4e3a web \u5bb9\u5668\u662f\u591a\u7ebf\u7a0b\u7684\u3002\u5f53\u65f6\u6211\u89c9\u5f97\u81ea\u5df1\u7406\u89e3\u4e86\u3002\u4f46\u4e4b\u540e\u6211\u5c31\u5482\u6478\u8fd9\u53e5\u8bdd\uff0c\u662f\u56e0\u4e3a\u591a\u7ebf\u7a0b\u6240\u4ee5\u4e0d\u4f1a\u963b\u585e\u3002\u4e3a\u4ec0\u4e48\u591a\u7ebf\u7a0b\u5c31\u4e0d\u4f1a\u963b\u585e\u5462\uff1f\u4e3a\u4ec0\u4e48\u591a\u4e2a\u7ebf\u7a0b\u64cd\u4f5c\u540c\u4e00\u4e2a\u5bf9\u8c61\u7684\u540c\u4e00\u4e2a\u65b9\u6cd5\u5c31\u4e0d\u4f1a\u963b\u585e\u5462\uff1f\u4e3a\u4ec0\u4e48\u591a\u7ebf\u7a0b\u6267\u884c\u540c\u4e00\u4e2a\u5bf9\u8c61\u7684\u540c\u4e00\u4e2a\u65b9\u6cd5\u4e2d\u7684\u76f8\u540c\u4ee3\u7801\u4e0d\u4f1a\u963b\u585e\u5462\uff1f\u6c42\u6559\u5404\u4f4d\u5927\u795e\u3002<\/pre>\n\n\n\n<p><strong>\u56de\u7b54\uff1a<\/strong><\/p>\n\n\n\n<p>\u65b0\u7684\u8bf7\u6c42\u4f1a\u65b0\u5f00\u7ebf\u7a0b\u8fdb\u884c\u76f8\u5e94\u3002<\/p>\n\n\n\n<p>\u4e00\u4e2a\u51fd\u6570\u88ab\u6267\u884c\u65f6\u4f1a\u5206\u914d\u65b0\u7684\u5185\u5b58\u533a\u57df\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u5c31\u5206\u914d\u4e00\u6b21\uff0c\u6240\u4ee5\u4ed6\u4eec\u662f\u4e92\u4e0d\u53ef\u89c1\u7684\u3002<\/p>\n\n\n\n<p>\u800c\u5982\u679c\u6709static\u53d8\u91cf\u6216\u8005\u5355\u4f8b\u53d8\u91cf\uff0c\u8fd9\u79cd\u60c5\u51b5\u591a\u7ebf\u7a0b\u5199\u4f1a\u6709\u51b2\u7a81\u3002<\/p>\n\n\n\n<p>\u6bcf\u4e00\u4e2a\u8fd0\u884c\u5728Java\u865a\u62df\u673a\u91cc\u7684\u7ebf\u7a0b\u90fd\u62e5\u6709\u81ea\u5df1\u7684\u7ebf\u7a0b\u6808\u3002\u8fd9\u4e2a\u7ebf\u7a0b\u6808\u5305\u542b\u4e86\u8fd9\u4e2a\u7ebf\u7a0b\u8c03\u7528\u7684\u65b9\u6cd5\u5f53\u524d\u6267\u884c\u70b9\u76f8\u5173\u7684\u4fe1\u606f\u3002\u4e00\u4e2a\u7ebf\u7a0b\u4ec5\u80fd\u8bbf\u95ee\u81ea\u5df1\u7684\u7ebf\u7a0b\u6808\u3002\u4e00\u4e2a\u7ebf\u7a0b\u521b\u5efa\u7684\u672c\u5730\u53d8\u91cf\u5bf9\u5176\u5b83\u7ebf\u7a0b\u4e0d\u53ef\u89c1\uff0c\u4ec5\u81ea\u5df1\u53ef\u89c1\u3002\u5373\u4f7f\u4e24\u4e2a\u7ebf\u7a0b\u6267\u884c\u540c\u6837\u7684\u4ee3\u7801\uff0c\u8fd9\u4e24\u4e2a\u7ebf\u7a0b\u4efb\u7136\u5728\u5728\u81ea\u5df1\u7684\u7ebf\u7a0b\u6808\u4e2d\u7684\u4ee3\u7801\u6765\u521b\u5efa\u672c\u5730\u53d8\u91cf\u3002\u56e0\u6b64\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u62e5\u6709\u6bcf\u4e2a\u672c\u5730\u53d8\u91cf\u7684\u72ec\u6709\u7248\u672c\u3002<\/p>\n\n\n\n<p>\u51fd\u6570\u5c31\u50cf\u83dc\u8c31\uff0c\u4e0d\u540c\u7ebf\u7a0b\u5c31\u50cf\u4e0d\u540c\u53a8\u5e08\uff0c\u80fd\u4e0d\u80fd\u5e76\u884c\u505a\u83dc\u548c\u83dc\u8c31\u7684\u6027\u8d28\u672c\u8eab\u65e0\u5173\uff0c\u800c\u662f\u6bd4\u5982\u53ea\u6709\u4e00\u4e2a\u6c34\u9f99\u5934\uff0c\u83dc\u8c31\u8981\u6c42\u6bcf\u4e2a\u53a8\u5e08\u53bb\u63a5\u6c34\uff0c\u8fd9\u4e2a\u63a5\u6c34\u624d\u4f1a\u4ea7\u751f\u5e76\u53d1\u51b2\u7a81<\/p>\n\n\n\n<p><strong>ChatGpt\u7684\u56de\u7b54<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-29-1024x897.png\" alt=\"\" class=\"wp-image-12629\" width=\"388\" height=\"339\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-29-1024x897.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-29-300x263.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-29-768x673.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-29-830x727.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-29-230x201.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-29-350x307.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-29-480x420.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-29.png 1338w\" sizes=\"(max-width: 388px) 100vw, 388px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u5c0f\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ul><li>spring\u5355\u4f8b\u6a21\u5f0f\uff0ccontroller\u3001service\u548cdao\u4e00\u822c\u90fd\u662f\u65e0\u72b6\u6001\u7684\uff0c\u5c31\u7b97\u662f\u6709\u72b6\u6001\u7684\uff0c\u53ea\u662f\u8c03\u7528\u91cc\u9762\u7684\u65b9\u6cd5\uff0c\u800c\u4e14\u591a\u7ebf\u7a0b\u8c03\u7528\u4e00\u4e2a\u5b9e\u4f8b\u7684\u65b9\u6cd5\uff0c\u4f1a\u5728\u5185\u5b58\u4e2d\u590d\u5236\u53d8\u91cf\uff0c\u8fd9\u662f\u81ea\u5df1\u7684\u7ebf\u7a0b\u7684\u5de5\u4f5c\u5185\u5b58\uff0c\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/li><li>controller\u3001service\u4e00\u5b9a\u8981\u5b9a\u4e49\u53d8\u91cf\u7684\u8bdd\uff0c\u7528ThreadLocal\u6765\u5c01\u88c5\uff0c\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u3002<\/li><li>\u5355\u4f8b\u6a21\u5f0f\u5927\u5927\u8282\u7701\u4e86\u5b9e\u4f8b\u7684\u521b\u5efa\u548c\u9500\u6bc1\u7684\u65f6\u95f4\uff0c\u6709\u5229\u4e8e\u63d0\u9ad8\u6027\u80fd\uff0c\u800cThreadLocal\u7528\u6765\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u6027\u3002<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f Spring Bean\u7684scope\u9ed8\u8ba4\u662fsingleton\uff08\u5355\u4f8b\uff09\u6a21\u5f0f\uff0c\u5bb9\u5668\u672c\u8eab\u5e76\u6ca1\u6709\u63d0\u4f9bBean\u7684\u7ebf\u7a0b [&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\/12619"}],"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=12619"}],"version-history":[{"count":8,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/12619\/revisions"}],"predecessor-version":[{"id":12630,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/12619\/revisions\/12630"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}