{"id":202856,"date":"2025-05-22T08:53:01","date_gmt":"2025-05-22T00:53:01","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202856\/"},"modified":"2025-05-22T08:53:01","modified_gmt":"2025-05-22T00:53:01","slug":"spring-boot-%e6%b3%a8%e8%a7%a3%e8%a7%a3%e9%87%8a%ef%bc%9a%e6%82%a8%e5%ba%94%e8%af%a5%e4%ba%86%e8%a7%a3%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%b3%a8%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202856\/","title":{"rendered":"Spring Boot \u6ce8\u89e3\u89e3\u91ca\uff1a\u60a8\u5e94\u8be5\u4e86\u89e3\u7684\u57fa\u672c\u6ce8\u89e3"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Spring Boot \u6ce8\u89e3\u89e3\u91ca\uff1a\u60a8\u5e94\u8be5\u4e86\u89e3\u7684\u57fa\u672c\u6ce8\u89e3<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u6587\u7ae0\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300aSpring Boot \u6ce8\u89e3\u89e3\u91ca\uff1a\u60a8\u5e94\u8be5\u4e86\u89e3\u7684\u57fa\u672c\u6ce8\u89e3\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729951964671cf8dcd8725.jpg\" class=\"aligncenter\"><\/p>\n<p>spring boot \u901a\u8fc7\u62bd\u8c61\u5927\u90e8\u5206\u6837\u677f\u914d\u7f6e\uff0c\u4f7f\u5f00\u53d1\u57fa\u4e8e java \u7684\u5e94\u7528\u7a0b\u5e8f\u53d8\u5f97\u5f02\u5e38\u5bb9\u6613\u3002 spring boot \u5982\u6b64\u5f3a\u5927\u4e14\u7528\u6237\u53cb\u597d\u7684\u5173\u952e\u529f\u80fd\u4e4b\u4e00\u662f\u5b83\u5e7f\u6cdb\u4f7f\u7528<strong>\u6ce8\u91ca<\/strong>\u3002\u8fd9\u4e9b\u6ce8\u91ca\u53ef\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u914d\u7f6e\u548c\u7ba1\u7406\u4ed6\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u800c\u65e0\u9700\u7f16\u5199\u5927\u578b xml \u914d\u7f6e\u6587\u4ef6\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u6bcf\u4e2a\u5f00\u53d1\u4eba\u5458\u90fd\u5e94\u8be5\u4e86\u89e3\u7684 15 \u4e2a\u57fa\u672c spring boot \u6ce8\u91ca\u3002<\/p>\n<hr>\n<h3> 1. <strong>@springbootapplication<\/strong> <\/h3>\n<p>\u8fd9\u662fspring boot\u4e2d\u6700\u91cd\u8981\u7684\u6ce8\u89e3\u3002\u5b83\u7ed3\u5408\u4e86\u4e09\u4e2a\u5173\u952e\u6ce8\u91ca\uff1a<\/p>\n<ul>\n<li> <strong>@configuration<\/strong>\uff1a\u8868\u793a\u8be5\u7c7b\u5305\u542bspring\u914d\u7f6e\u3002<\/li>\n<li> <strong>@enableautoconfiguration<\/strong>\uff1a\u542f\u7528 spring boot \u7684\u81ea\u52a8\u914d\u7f6e\u673a\u5236\u3002<\/li>\n<li> <strong>@componentscan<\/strong>\uff1a\u544a\u8bc9 spring \u626b\u63cf\u5305\u4e2d\u7684\u7ec4\u4ef6\u3001\u914d\u7f6e\u548c\u670d\u52a1\u3002<\/li>\n<\/ul>\n<p><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<pre>@springbootapplication\npublic class myapplication {\n    public static void main(string[] args) {\n        springapplication.run(myapplication.class, args);\n    }\n}\n<\/pre>\n<h3> 2. <strong>@restcontroller<\/strong> <\/h3>\n<p>\u6b64\u6ce8\u89e3\u662f\u7ec4\u5408 <strong>@controller<\/strong> \u548c <strong>@responsebody<\/strong> \u7684\u5feb\u6377\u65b9\u5f0f\u3002\u5b83\u7528\u4e8e\u521b\u5efa restful web \u670d\u52a1\u3002<\/p>\n<p><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<pre>@restcontroller\npublic class mycontroller {\n\n    @getmapping(\"\/hello\")\n    public string sayhello() {\n        return \"hello, world!\";\n    }\n}\n<\/pre>\n<h3> 3. <strong>@requestmapping<\/strong> <\/h3>\n<p>\u6b64\u6ce8\u91ca\u7528\u4e8e\u5c06 web \u8bf7\u6c42\u6620\u5c04\u5230\u7279\u5b9a\u7684\u5904\u7406\u7a0b\u5e8f\u65b9\u6cd5\u6216\u7c7b\u3002\u5b83\u53ef\u4ee5\u5904\u7406\u5404\u79cd http \u65b9\u6cd5\uff0c\u5982 get\u3001post\u3001put \u548c delete\u3002<\/p>\n<p><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<pre>@restcontroller\n@requestmapping(\"\/api\")\npublic class apicontroller {\n\n    @requestmapping(value = \"\/data\", method = requestmethod.get)\n    public string getdata() {\n        return \"here is the data!\";\n    }\n}\n<\/pre>\n<h3> 4. <strong>@getmapping\u3001@postmapping\u3001@putmapping\u3001@deletemapping<\/strong> <\/h3>\n<p>\u8fd9\u4e9b\u6ce8\u91ca\u662f\u7279\u5b9a http \u65b9\u6cd5\u7684 <strong>@requestmapping<\/strong> \u7684\u5feb\u6377\u65b9\u5f0f\u3002\u5b83\u4eec\u4f7f\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u9488\u5bf9\u6027\u3002<\/p>\n<p><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<pre>@getmapping(\"\/items\")\npublic list&lt;item&gt; getitems() {\n    return itemservice.getallitems();\n}\n\n@postmapping(\"\/items\")\npublic item additem(@requestbody item item) {\n    return itemservice.additem(item);\n}\n<\/pre>\n<h3> 5.<strong>@autowired<\/strong> <\/h3>\n<p>\u8be5\u6ce8\u89e3\u7528\u4e8e\u6309\u7c7b\u578b\u81ea\u52a8\u6ce8\u5165bean\u3002\u5b83\u53ef\u4ee5\u7528\u5728\u6784\u9020\u51fd\u6570\u3001setter \u6216\u5b57\u6bb5\u4e0a\u3002<\/p>\n<p><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<pre>@service\npublic class userservice {\n\n    @autowired\n    private userrepository userrepository;\n\n    public user getuserbyid(long id) {\n        return userrepository.findbyid(id).orelse(null);\n    }\n}\n<\/pre>\n<h3> 6. <strong>@\u670d\u52a1<\/strong> <\/h3>\n<p>\u8be5\u6ce8\u89e3\u7528\u4e8e\u5c06\u7c7b\u6807\u8bb0\u4e3a\u670d\u52a1\uff0c\u901a\u5e38\u5305\u542b\u4e1a\u52a1\u903b\u8f91\u3002 spring\u81ea\u52a8\u68c0\u6d4b\u5230\u5b83\u5e76\u5c06\u5176\u6ce8\u518c\u4e3abean\u3002<\/p>\n<p><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<pre>@service\npublic class emailservice {\n    public void sendemail(string recipient, string message) {\n        \/\/ logic for sending email\n    }\n}\n<\/pre>\n<h3> 7. <strong>@repository<\/strong> <\/h3>\n<p>\u8fd9\u4e2a\u6ce8\u89e3\u8868\u660e\u7c7b\u662f\u4e00\u4e2a\u5b58\u50a8\u5e93\uff0c\u5b83\u4e0e\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\u3002\u5b83\u662f <strong>@component<\/strong> \u7684\u4e13\u95e8\u7248\u672c\uff0c\u5e76\u4e3a\u6570\u636e\u5e93\u9519\u8bef\u63d0\u4f9b\u81ea\u52a8\u5f02\u5e38\u7ffb\u8bd1\u3002<\/p>\n<p><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<pre>@repository\npublic interface userrepository extends jparepository&lt;user, long&gt; {\n}\n<\/pre>\n<h3> 8. <strong>@component<\/strong> <\/h3>\n<p>\u8fd9\u4e2a\u901a\u7528\u6ce8\u91ca\u5c06\u7c7b\u6807\u8bb0\u4e3a spring \u7ba1\u7406\u7684\u7ec4\u4ef6\u3002\u5b83\u662f\u4e00\u4e2a\u6838\u5fc3\u6784\u9020\u578b\u6ce8\u91ca\uff0c\u6709\u52a9\u4e8e\u81ea\u52a8\u53d1\u73b0 bean\u3002<\/p>\n<p><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<pre>@component\npublic class utilityservice {\n    public string generaterandomstring() {\n        return uuid.randomuuid().tostring();\n    }\n}\n<\/pre>\n<h3> 9. <strong>@\u914d\u7f6e<\/strong> <\/h3>\n<p>\u8be5\u6ce8\u89e3\u8868\u660e\u8be5\u7c7b\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2a spring bean \u5b9a\u4e49\u3002\u5b83\u7528\u4e8e\u914d\u7f6e\u76ee\u7684\u3002<\/p>\n<p><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<pre>@configuration\npublic class appconfig {\n\n    @bean\n    public modelmapper modelmapper() {\n        return new modelmapper();\n    }\n}\n<\/pre>\n<h3> 10.<strong>@bean<\/strong> <\/h3>\n<p>\u8be5\u6ce8\u89e3\u7528\u4e8e\u663e\u5f0f\u58f0\u660e spring bean\u3002\u5b83\u901a\u5e38\u5728 <strong>@configuration<\/strong> \u7c7b\u4e2d\u4f7f\u7528\uff0c\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u5b9a\u4e49 beans\u3002<\/p>\n<p><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<pre>@configuration\npublic class appconfig {\n\n    @bean\n    public passwordencoder passwordencoder() {\n        return new bcryptpasswordencoder();\n    }\n}\n<\/pre>\n<h3> 11. <strong>@enableautoconfiguration<\/strong> <\/h3>\n<p>\u8be5\u6ce8\u89e3\u7528\u4e8e\u6839\u636e\u7c7b\u8def\u5f84\u8bbe\u7f6e\u3001\u5176\u4ed6 bean \u548c\u5404\u79cd\u5c5e\u6027\u8bbe\u7f6e\u81ea\u52a8\u914d\u7f6e spring \u5e94\u7528\u7a0b\u5e8f\u3002\u901a\u5e38\uff0c\u8be5\u6ce8\u89e3\u4e0e <strong>@springbootapplication<\/strong>.<\/p>\n<p> \u7ec4\u5408\u4f7f\u7528 <\/p>\n<p><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<pre>@enableautoconfiguration\npublic class myapplication {\n    \/\/ custom configuration code\n}\n<\/pre>\n<h3> 12. <strong>@qualifier<\/strong> <\/h3>\n<p>\u5f53\u5b58\u5728\u591a\u4e2a\u76f8\u540c\u7c7b\u578b\u7684bean\u65f6\uff0c<strong>@qualifier<\/strong>\u7528\u4e8e\u901a\u8fc7\u6307\u5b9a\u5e94\u8be5\u6ce8\u5165\u54ea\u4e2abean\u6765\u89e3\u51b3\u6b67\u4e49\u3002<\/p>\n<p><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<pre>@service\npublic class notificationservice {\n\n    @autowired\n    @qualifier(\"emailservice\")\n    private messageservice messageservice;\n}\n<\/pre>\n<h3> 13. <strong>@value<\/strong> <\/h3>\n<p>\u6b64\u6ce8\u91ca\u7528\u4e8e\u5c06\u5c5e\u6027\u6587\u4ef6\u4e2d\u7684\u503c\u6ce8\u5165 spring beans\u3002<\/p>\n<p><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<pre>@component\npublic class appproperties {\n\n    @value(\"${app.name}\")\n    private string appname;\n\n    public string getappname() {\n        return appname;\n    }\n}\n<\/pre>\n<h3> 14.<strong>@\u4ea4\u6613<\/strong> <\/h3>\n<p>\u6b64\u6ce8\u91ca\u7528\u4e8e\u6307\u793a\u65b9\u6cd5\u6216\u7c7b\u5e94\u5305\u88c5\u5728\u6570\u636e\u5e93\u4e8b\u52a1\u4e2d\u3002\u5b83\u786e\u4fdd\u4e8b\u52a1\u8303\u56f4\u5185\u7684\u64cd\u4f5c\u662f\u539f\u5b50\u7684\u3002<\/p>\n<p><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<pre>@service\npublic class paymentservice {\n\n    @transactional\n    public void processpayment(order order) {\n        \/\/ payment processing logic\n    }\n}\n<\/pre>\n<h3> 15. <strong>@exceptionhandler<\/strong> <\/h3>\n<p>\u8be5\u6ce8\u89e3\u7528\u4e8e\u5b9a\u4e49\u5904\u7406\u63a7\u5236\u5668\u629b\u51fa\u7684\u5f02\u5e38\u7684\u65b9\u6cd5\u3002\u5b83\u5141\u8bb8\u96c6\u4e2d\u9519\u8bef\u5904\u7406\u3002<\/p>\n<p><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<pre>@RestController\npublic class GlobalExceptionHandler {\n\n    @ExceptionHandler(Exception.class)\n    public ResponseEntity&lt;String&gt; handleException(Exception ex) {\n        return new ResponseEntity&lt;&gt;(\"Error: \" + ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);\n    }\n}\n<\/pre>\n<hr>\n<h3> <strong>\u7ed3\u8bba<\/strong> <\/h3>\n<p>\u65e0\u8bba\u60a8\u662f\u6784\u5efa rest api\u3001web \u5e94\u7528\u7a0b\u5e8f\u8fd8\u662f\u5fae\u670d\u52a1\uff0cspring boot \u6ce8\u91ca\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\u548c\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u5065\u58ee\u3001\u53ef\u6269\u5c55\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aSpring Boot \u6ce8\u89e3\u89e3\u91ca\uff1a\u60a8\u5e94\u8be5\u4e86\u89e3\u7684\u57fa\u672c\u6ce8\u89e3\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring Boot \u6ce8\u89e3\u89e3\u91ca&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202856","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202856","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202856"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202856\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}