{"id":8606,"date":"2022-11-18T20:24:06","date_gmt":"2022-11-18T12:24:06","guid":{"rendered":"http:\/\/123.57.164.21\/?p=8606"},"modified":"2022-11-18T20:24:06","modified_gmt":"2022-11-18T12:24:06","slug":"springboot-controller%e7%ba%bf%e7%a8%8b%e5%ae%89%e5%85%a8%e5%90%97%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=8606","title":{"rendered":"Springboot controller\u7ebf\u7a0b\u5b89\u5168\u5417\uff1f"},"content":{"rendered":"\n<p><strong>\u4e00\u3001\u9a8c\u8bc1controller\u7ebf\u7a0b\u5b89\u5168<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>controller\u9ed8\u8ba4\u662f\u5355\u4f8b\u7684\uff0c\u4e0d\u8981\u4f7f\u7528\u975e\u9759\u6001\u7684\u6210\u5458\u53d8\u91cf\uff0c\u5426\u5219\u4f1a\u53d1\u751f\u6570\u636e\u903b\u8f91\u6df7\u4e71\u3002\u6b63\u56e0\u4e3a\u5355\u4f8b\u6240\u4ee5\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u4e0b\u9762\u6765\u7b80\u5355\u7684\u9a8c\u8bc1\u4e0b\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=\"\">@Controller\n\npublic class ScopeTestController {\n    private int num = 0;\n\n    @RequestMapping(\"\/testScope\")\n    public void testScope() {\n        System.out.println(++num);\n    }\n\n    @RequestMapping(\"\/testScope2\")\n    public void testScope2() {\n        System.out.println(++num);\n    }\n\n}\n<\/pre>\n\n\n\n<p>\u6211\u4eec\u9996\u5148\u8bbf\u95ee <a rel=\"noreferrer noopener\" href=\"https:\/\/links.jianshu.com\/go?to=http%3A%2F%2Flocalhost%3A8080%2FtestScope\" target=\"_blank\">http:\/\/localhost:8080\/testScope<\/a>\uff0c\u5f97\u5230\u7684\u7b54\u6848\u662f<code>1<\/code><\/p>\n\n\n\n<p>\u7136\u540e\u6211\u4eec\u518d\u8bbf\u95ee <a rel=\"noreferrer noopener\" href=\"https:\/\/links.jianshu.com\/go?to=http%3A%2F%2Flocalhost%3A8080%2FtestScope2\" target=\"_blank\">http:\/\/localhost:8080\/testScope2<\/a>\uff0c\u5f97\u5230\u7684\u7b54\u6848\u662f<code>2<\/code>\u3002<\/p>\n\n\n\n<p>\u5f97\u5230\u7684\u4e0d\u540c\u7684\u503c\uff0c\u5355\u4f8b\u4f1a\u5bfc\u81f4\u5c5e\u6027\u91cd\u590d\u4f7f\u7528\uff0c\u8fd9\u662f\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\u3002<\/p>\n\n\n\n<p><strong>\u4e8c\u3001controller\u591a\u4f8b<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u518d\u6765\u7ed9controller\u589e\u52a0\u4f5c\u7528\u591a\u4f8b\u6ce8\u89e3<code>@Scope(\"prototype\")<\/code><\/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=\"\">@Controller\n@Scope(\"prototype\")\npublic class ScopeTestController {\n\n    private int num = 0\uff1b\n\n    @RequestMapping(\"\/testScope\")\n    public void testScope() {\n        System.out.println(++num);\n    }\n\n    @RequestMapping(\"\/testScope2\")\n    public void testScope2() {\n        System.out.println(++num);\n    }\n\n}\n<\/pre>\n\n\n\n<p>\u6211\u4eec\u4f9d\u65e7\u9996\u5148\u8bbf\u95ee <a rel=\"noreferrer noopener\" href=\"https:\/\/links.jianshu.com\/go?to=http%3A%2F%2Flocalhost%3A8080%2FtestScope\" target=\"_blank\">http:\/\/localhost:8080\/testScope<\/a>\uff0c\u5f97\u5230\u7684\u7b54\u6848\u662f<code>1<\/code><\/p>\n\n\n\n<p>\u7136\u540e\u6211\u4eec\u518d\u8bbf\u95ee <a rel=\"noreferrer noopener\" href=\"https:\/\/links.jianshu.com\/go?to=http%3A%2F%2Flocalhost%3A8080%2FtestScope2\" target=\"_blank\">http:\/\/localhost:8080\/testScope2<\/a>\uff0c\u5f97\u5230\u7684\u7b54\u6848\u8fd8\u662f<code>1<\/code><\/p>\n\n\n\n<p><strong>\u4e09\u3001\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ul><li>\u4e0d\u8981\u5728controller\u4e2d\u5b9a\u4e49\u6210\u5458\u53d8\u91cf\u3002<\/li><li>\u4e07\u4e00\u5fc5\u987b\u8981\u5b9a\u4e49\u4e00\u4e2a\u975e\u9759\u6001\u6210\u5458\u53d8\u91cf\u65f6\u5019\uff0c\u5219\u901a\u8fc7\u6ce8\u89e3@Scope(\u201cprototype\u201d)\uff0c\u5c06\u5176\u8bbe\u7f6e\u4e3a\u591a\u4f8b\u6a21\u5f0f\u3002<\/li><li>\u5728controller\u4e2d\u4f7f\u7528ThreadLocal\u53d8\u91cf\u3002<\/li><\/ul>\n\n\n\n<p><strong>\u56db\u3001\u4f5c\u7528\u57df\u8865\u5145\u8bf4\u660e<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>spring bean\u4f5c\u7528\u57df\u6709\u4ee5\u4e0b5\u4e2a\uff1a<\/p>\n\n\n\n<ul><li>singleton:\u5355\u4f8b\u6a21\u5f0f\uff0c\u5f53spring\u521b\u5efaapplicationContext\u5bb9\u5668\u7684\u65f6\u5019\uff0cspring\u4f1a\u6b32\u521d\u59cb\u5316\u6240\u6709\u7684\u8be5\u4f5c\u7528\u57df\u5b9e\u4f8b\uff0c\u52a0\u4e0alazy-init\u5c31\u53ef\u4ee5\u907f\u514d\u9884\u5904\u7406\uff1b<\/li><li>prototype\uff1a\u539f\u578b\u6a21\u5f0f\uff0c\u6bcf\u6b21\u901a\u8fc7getBean\u83b7\u53d6\u8be5bean\u5c31\u4f1a\u65b0\u4ea7\u751f\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u521b\u5efa\u540espring\u5c06\u4e0d\u518d\u5bf9\u5176\u7ba1\u7406\uff1b<\/li><\/ul>\n\n\n\n<p><code>------\uff08\u4e0b\u9762\u662f\u5728web\u9879\u76ee\u4e0b\u624d\u7528\u5230\u7684\uff09------<\/code><\/p>\n\n\n\n<ul><li>request\uff1a\u641eweb\u7684\u5927\u5bb6\u90fd\u5e94\u8be5\u660e\u767drequest\u7684\u57df\u4e86\u5427\uff0c\u5c31\u662f\u6bcf\u6b21\u8bf7\u6c42\u90fd\u65b0\u4ea7\u751f\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u548cprototype\u4e0d\u540c\u5c31\u662f\u521b\u5efa\u540e\uff0c\u63a5\u4e0b\u6765\u7684\u7ba1\u7406\uff0cspring\u4f9d\u7136\u5728\u76d1\u542c\uff1b<\/li><li>session:\u6bcf\u6b21\u4f1a\u8bdd\uff0c\u540c\u4e0a\uff1b<\/li><li>global session:\u5168\u5c40\u7684web\u57df\uff0c\u7c7b\u4f3c\u4e8eservlet\u4e2d\u7684application\u3002<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u9a8c\u8bc1controller\u7ebf\u7a0b\u5b89\u5168 controller\u9ed8\u8ba4\u662f\u5355\u4f8b\u7684\uff0c\u4e0d\u8981\u4f7f\u7528\u975e\u9759\u6001\u7684\u6210\u5458\u53d8\u91cf\uff0c\u5426\u5219\u4f1a\u53d1\u751f\u6570 [&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\/8606"}],"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=8606"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/8606\/revisions"}],"predecessor-version":[{"id":8607,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/8606\/revisions\/8607"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8606"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8606"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}