{"id":581,"date":"2023-03-23T21:28:47","date_gmt":"2023-03-23T13:28:47","guid":{"rendered":""},"modified":"2023-03-23T21:28:47","modified_gmt":"2023-03-23T13:28:47","slug":"SpringBoot \u6ce8\u89e3","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/581.html","title":{"rendered":"SpringBoot \u6ce8\u89e3"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

SpringBoot \u6ce8\u89e3<\/h1>\n<\/p><\/div>\n
\n Spring Boot\u6ce8\u89e3\u662f\u4e00\u79cd\u5143\u6570\u636e\u5f62\u5f0f\uff0c\u53ef\u63d0\u4f9b\u6709\u5173\u7a0b\u5e8f\u7684\u6570\u636e\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6ce8\u89e3\u7528\u4e8e\u63d0\u4f9b\u6709\u5173\u7a0b\u5e8f\u7684
\n \u8865\u5145<\/strong>\u4fe1\u606f\u3002\u5b83\u4e0d\u662f\u6211\u4eec\u5f00\u53d1\u7684\u5e94\u7528\u7a0b\u5e8f\u7684\u4e00\u90e8\u5206\u3002\u5b83\u5bf9\u5176\u6ce8\u89e3\u7684\u4ee3\u7801\u7684\u64cd\u4f5c\u6ca1\u6709\u76f4\u63a5\u5f71\u54cd\u3002\u4e0d\u4f1a\u66f4\u6539\u5df2\u7f16\u8bd1\u7a0b\u5e8f\u7684\u64cd\u4f5c\u3002\n <\/div>\n
\n \u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u4e00\u4e9b\u91cd\u8981\u7684
\n Spring Boot\u6ce8\u89e3<\/strong>\uff0c\u6211\u4eec\u5c06\u5728\u672c\u6559\u7a0b\u7684\u540e\u9762\u90e8\u5206\u4f7f\u7528\u3002\n <\/div>\n

Core Spring\u6846\u67b6\u6ce8\u89e3<\/h2>\n
\n @Required: <\/strong>\u5b83\u9002\u7528\u4e8e
\n bean <\/strong>\u8bbe\u7f6e\u65b9\u6cd5\u3002\u5b83\u6307\u793a\u5fc5\u987b\u5728\u914d\u7f6e\u65f6\u4f7f\u7528\u5fc5\u9700\u7684\u5c5e\u6027\u586b\u5145\u5e26\u6ce8\u89e3\u7684Bean\uff0c\u5426\u5219\u5b83\u5c06\u5f15\u53d1\u5f02\u5e38
\n BeanInitilizationException <\/strong>\u3002\n <\/div>\n
\n \u793a\u4f8b<\/strong>\n <\/div>\n
\n
\r\npublic <\/span>class <\/span>Machine \r\n{\r\nprivate <\/span>Integer cost;\r\n@Required\r\npublic void <\/span>setCost(Integer cost) \r\n{\r\nthis<\/span>.cost = cost;\r\n}\r\npublic <\/span>Integer getCost() \r\n{\r\nreturn <\/span>cost;\r\n}   \r\n}\r\n<\/pre>\n

<\/code>\n <\/div>\n

\n @Autowired: <\/strong>: Spring\u901a\u8fc7\u63d0\u4f9b@Autowired\u6ce8\u89e3\u6765\u63d0\u4f9b\u57fa\u4e8e\u6ce8\u89e3\u7684\u81ea\u52a8\u88c5\u914d\u3002\u5b83\u7528\u4e8e\u81ea\u52a8\u8fde\u63a5setter\u65b9\u6cd5\uff0c\u5b9e\u4f8b\u53d8\u91cf\u548c\u6784\u9020\u51fd\u6570\u4e0a\u7684spring bean\u3002\u5f53\u6211\u4eec\u4f7f\u7528@Autowired\u6279\u6ce8\u65f6\uff0cspring\u5bb9\u5668\u901a\u8fc7\u5339\u914d\u6570\u636e\u7c7b\u578b\u81ea\u52a8\u8fde\u63a5bean\u3002\n <\/div>\n
\n \u793a\u4f8b<\/strong>\n <\/div>\n
\n
\r\n@Component\r\npublic <\/span>class <\/span>Customer\r\n{\r\nprivate <\/span>Person person;\r\n@Autowired\r\npublic <\/span>Customer(Person person) \r\n{ \r\nthis<\/span>.person=person;\r\n}\r\n}\r\n<\/pre>\n

<\/code>\n <\/div>\n

\n @Configuration: <\/strong>: \u5b83\u662f\u4e00\u4e2a\u7c7b\u7ea7\u522b\u7684\u6ce8\u89e3\u3002\u5e26\u6709@Configuration\u6ce8\u89e3\u7684\u7c7b\u7531Spring Containers\u7528\u4f5cbean\u5b9a\u4e49\u7684\u6e90\u3002\n <\/div>\n
\n \u793a\u4f8b<\/strong>\n <\/div>\n
\n
\r\n@Configuration\r\npublic <\/span>class <\/span>Vehicle\r\n{\r\n@BeanVehicle engine()\r\n{\r\nreturn new <\/span>Vehicle();\r\n}\r\n}\r\n<\/pre>\n

<\/code>\n <\/div>\n

\n @ComponentScan: <\/strong>: \u5f53\u6211\u4eec\u8981\u626b\u63cf\u8f6f\u4ef6\u5305\u4e2d\u7684bean\u65f6\u4f7f\u7528\u3002\u5b83\u4e0e\u6ce8\u89e3@Configuration\u4e00\u8d77\u4f7f\u7528\u3002\u6211\u4eec\u8fd8\u53ef\u4ee5\u6307\u5b9a\u7528\u4e8e\u626b\u63cfSpring\u7ec4\u4ef6\u7684\u57fa\u672c\u8f6f\u4ef6\u5305\u3002\n <\/div>\n
\n \u793a\u4f8b<\/strong>\n <\/div>\n
\n
\r\n@ComponentScan(basePackages = \"com.lidihuo\"<\/span>)\r\n@Configuration\r\npublic <\/span>class <\/span>ScanComponent\r\n{\r\n\/\/ ...\r\n}\r\n<\/pre>\n

<\/code>\n <\/div>\n

\n @Bean: <\/strong>\u662f\u65b9\u6cd5\u7ea7\u7684\u6ce8\u89e3\u3002\u5b83\u662fXML
\n
\n \u6807\u8bb0\u7684\u66ff\u4ee3\u65b9\u6cd5\u3002\u5b83\u544a\u8bc9\u4ea7\u751f\u7531Spring Container\u7ba1\u7406\u7684bean\u7684\u65b9\u6cd5\u3002
\n <\/bean>\n <\/div>\n
\n \u793a\u4f8b<\/strong>\n <\/div>\n
\n
\r\n@Bean\r\npublic <\/span>BeanExample beanExample() \r\n{\r\nreturn new <\/span>BeanExample ();\r\n}\r\n<\/pre>\n

<\/code>\n <\/div>\n

Spring Framework\u6784\u9020\u578b\u6ce8\u89e3<\/h2>\n
\n @Component: <\/strong>\u3002\u5b83\u662f\u4e00\u4e2a\u7c7b\u7ea7\u522b\u7684\u6ce8\u89e3\u3002\u5b83\u7528\u4e8e\u5c06Java\u7c7b\u6807\u8bb0\u4e3aBean\u3002\u5728\u7c7b\u8def\u5f84\u4e2d\u627e\u5230\u4e86\u4e00\u4e2a\u7528
\n @Component <\/strong>\u6ce8\u89e3\u7684Java\u7c7b\u3002 Spring\u6846\u67b6\u5c06\u5176\u62fe\u53d6\u5e76\u5728\u5e94\u7528\u7a0b\u5e8f\u4e0a\u4e0b\u6587\u4e2d\u5c06\u5176\u914d\u7f6e\u4e3a
\n Spring Bean <\/strong>\u3002\n <\/div>\n
\n \u793a\u4f8b<\/strong>\n <\/div>\n
\n
\r\n@Component\r\npublic <\/span>class <\/span>Student\r\n{\r\n.......\r\n}\r\n<\/pre>\n

<\/code>\n <\/div>\n

\n @Controller: <\/strong> @Controller\u662f\u7c7b\u7ea7\u522b\u7684\u6ce8\u89e3\u3002\u5b83\u662f
\n @Component <\/strong>\u7684\u4e13\u4e1a\u5316\u3002\u5b83\u5c06\u4e00\u4e2a\u7c7b\u6807\u8bb0\u4e3aWeb\u8bf7\u6c42\u5904\u7406\u7a0b\u5e8f\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u670d\u52a1\u7f51\u9875\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u8be5\u5b57\u7b26\u4e32\u6307\u793a\u8981\u91cd\u5b9a\u5411\u7684\u8def\u7531\u3002\u5b83\u901a\u5e38\u4e0e
\n @RequestMapping <\/strong>\u6ce8\u89e3\u4e00\u8d77\u4f7f\u7528\u3002\n <\/div>\n
\n \u793a\u4f8b<\/strong>\n <\/div>\n
\n
\r\n@Controller\r\n@RequestMapping(\"books\"<\/span>)\r\npublic <\/span>class <\/span>BooksController \r\n{\r\n@RequestMapping(value = \"\/{name}\"<\/span>, method = RequestMethod.GET)\r\npublic <\/span>Employee getBooksByName() \r\n{\r\nreturn <\/span>booksTemplate;\r\n}\r\n}\r\n<\/pre>\n

<\/code>\n <\/div>\n

\n @Service: <\/strong>\u4e5f\u7528\u4e8e\u7c7b\u7ea7\u522b\u3002\u5b83\u544a\u8bc9Spring\u8be5\u7c7b\u5305\u542b
\n \u4e1a\u52a1\u903b\u8f91<\/strong>\u3002\n <\/div>\n
\n \u793a\u4f8b<\/strong>\n <\/div>\n
\n
\r\npackage <\/span>com.lidihuo;\r\n@Service\r\npublic <\/span>class <\/span>TestService\r\n{\r\npublic void <\/span>service1()\r\n{\r\n\/\/business code\r\n}\r\n}\r\n<\/pre>\n

<\/code>\n <\/div>\n

\n @Repository: <\/strong>\u8fd9\u662f\u4e00\u4e2a\u7c7b\u7ea7\u522b\u7684\u6ce8\u89e3\u3002\u8be5\u5b58\u50a8\u5e93\u662f\u76f4\u63a5\u8bbf\u95ee\u6570\u636e\u5e93\u7684
\n DAO <\/strong>(\u6570\u636e\u8bbf\u95ee\u5bf9\u8c61)\u3002\u8be5\u5b58\u50a8\u5e93\u6267\u884c\u4e0e\u6570\u636e\u5e93\u6709\u5173\u7684\u6240\u6709\u64cd\u4f5c\u3002\n <\/div>\n
\n
\r\npackage <\/span>com.lidihuo;\r\n@Repository \r\npublic <\/span>class <\/span>TestRepository\r\n{\r\npublic void <\/span>delete()\r\n{   \r\n\/\/persistence code\r\n}\r\n}\r\n<\/pre>\n

<\/code>\n <\/div>\n

Spring Boot\u6ce8\u89e3<\/h2>\n

@EnableAutoConfiguration: <\/strong>: \u5b83\u81ea\u52a8\u914d\u7f6e\u7c7b\u8def\u5f84\u4e2d\u5b58\u5728\u7684bean\uff0c\u5e76\u5c06\u5176\u914d\u7f6e\u4e3a\u8fd0\u884c\u65b9\u6cd5\u3002\u5728Spring Boot 1.2.0\u53d1\u884c\u7248\u4e2d\u51cf\u5c11\u4e86\u4f7f\u7528\u6b64\u6279\u6ce8\uff0c\u56e0\u4e3a\u5f00\u53d1\u4eba\u5458\u63d0\u4f9b\u4e86\u8be5\u6279\u6ce8\u7684\u66ff\u4ee3\u65b9\u6cd5\uff0c\u5373 @SpringBootApplication <\/strong>\u3002<\/span>
\n @SpringBootApplication: <\/strong>: \u5b83\u662f\u4e09\u4e2a\u6ce8\u89e3 @ EnableAutoConfiguration\uff0c@ ComponentScan\uff0c<\/strong>\u548c @Configuration <\/strong>\u7684\u7ec4\u5408\u3002<\/span> <\/p>\n

Spring MVC\u548cREST\u6ce8\u89e3<\/h3>\n

@RequestMapping: <\/strong>\u7528\u4e8e\u6620\u5c04\u7f51\u7edc\u8bf7\u6c42<\/strong>\u3002\u5b83\u5177\u6709\u8bb8\u591a\u53ef\u9009\u5143\u7d20\uff0c\u4f8b\u5982 consumes, header, method, name, params, path, produces\u548cvalue\u3002\u6211\u4eec\u5c06\u5176\u4e0e\u7c7b\u4ee5\u53ca\u65b9\u6cd5\u4e00\u8d77\u4f7f\u7528\u3002<\/span> <\/p>\n

\n \u793a\u4f8b<\/strong>\n <\/div>\n
\n
\r\n@Controller\r\npublic <\/span>class <\/span>BooksController \r\n{\r\n@RequestMapping(\"\/computer-science\/books\"<\/span>)\r\npublic <\/span>String getAllBooks(Model model)\r\n{\r\n\/\/application code\r\nreturn <\/span>\"bookList\"<\/span>;\r\n}\r\n<\/pre>\n

<\/code>\n <\/div>\n

@GetMapping: <\/strong>\u5b83\u5c06 HTTP GET <\/strong>\u8bf7\u6c42\u6620\u5c04\u5230\u7279\u5b9a\u7684\u5904\u7406\u7a0b\u5e8f\u65b9\u6cd5\u3002\u5b83\u7528\u4e8e\u521b\u5efa\u63d0\u53d6<\/strong>\u7684Web\u670d\u52a1\u7ec8\u7ed3\u70b9\uff0c\u800c\u4e0d\u662f\u4f7f\u7528 @RequestMapping(method = RequestMethod.GET)<\/strong> <\/span>
\n @PostMapping <\/strong>\u5b83\u5c06 HTTP POST <\/strong>\u8bf7\u6c42\u6620\u5c04\u5230\u7279\u5b9a\u7684\u5904\u7406\u7a0b\u5e8f\u65b9\u6cd5\u3002\u5b83\u7528\u4e8e\u521b\u5efa\u521b\u5efa<\/strong>\u7684Web\u670d\u52a1\u7ec8\u7ed3\u70b9\uff0c\u800c\u4e0d\u662f\u4f7f\u7528: @RequestMapping(method = RequestMethod.POST)<\/strong> <\/span>
\n @PutMapping: <\/strong>\u5b83\u5c06 HTTP PUT <\/strong>\u8bf7\u6c42\u6620\u5c04\u5230\u7279\u5b9a\u7684\u5904\u7406\u7a0b\u5e8f\u65b9\u6cd5\u3002\u5b83\u7528\u4e8e\u521b\u5efa\u521b\u5efa<\/strong>\u6216\u66f4\u65b0<\/strong>\u7684Web\u670d\u52a1\u7ec8\u7ed3\u70b9\uff0c\u800c\u4e0d\u662f\u4f7f\u7528: @RequestMapping(method = RequestMethod.PUT)<\/strong> <\/span>
\n @DeleteMapping: <\/strong>\u5b83\u5c06 HTTP DELETE <\/strong>\u8bf7\u6c42\u6620\u5c04\u5230\u7279\u5b9a\u7684\u5904\u7406\u7a0b\u5e8f\u65b9\u6cd5\u3002\u5b83\u7528\u4e8e\u521b\u5efa\u5220\u9664<\/strong>\u8d44\u6e90\u7684Web\u670d\u52a1\u7ec8\u7ed3\u70b9\u3002\u4f7f\u7528\u5b83\u800c\u4e0d\u662f\u4f7f\u7528: @RequestMapping(method = RequestMethod.DELETE)<\/strong> <\/span>
\n @PatchMapping: <\/strong>\u5b83\u5c06 HTTP PATCH <\/strong>\u8bf7\u6c42\u6620\u5c04\u5230\u7279\u5b9a\u7684\u5904\u7406\u7a0b\u5e8f\u65b9\u6cd5\u3002\u4f7f\u7528\u5b83\u4ee3\u66ff\u4f7f\u7528: @RequestMapping(method = RequestMethod.PATCH)<\/strong> <\/span>
\n @RequestBody: <\/strong>\u7528\u4e8e\u5c06HTTP\u8bf7\u6c42\u4e0e\u65b9\u6cd5\u53c2\u6570\u4e2d\u7684\u5bf9\u8c61\u7ed1\u5b9a<\/strong>\u3002\u5728\u5185\u90e8\uff0c\u5b83\u4f7f\u7528 HTTP MessageConverters <\/strong>\u8f6c\u6362\u8bf7\u6c42\u7684\u6b63\u6587\u3002\u5f53\u6211\u4eec\u7528 @RequestBody\u6ce8\u89e3\u65b9\u6cd5\u53c2\u6570\u65f6\uff0cSpring\u6846\u67b6\u4f1a\u5c06\u4f20\u5165\u7684HTTP\u8bf7\u6c42\u4e3b\u4f53\u7ed1\u5b9a\u5230\u8be5\u53c2\u6570\u3002<\/strong><\/span>
\n @ResponseBody: <\/strong>\u5b83\u5c06\u65b9\u6cd5\u8fd4\u56de\u503c\u7ed1\u5b9a\u5230\u54cd\u5e94\u4e3b\u4f53\u3002\u5b83\u544a\u8bc9Spring Boot Framework\u5c06\u8fd4\u56de\u7684\u5bf9\u8c61\u5e8f\u5217\u5316\u4e3aJSON\u548cXML\u683c\u5f0f\u3002<\/span>
\n @PathVariable: <\/strong>\u7528\u4e8e\u4eceURI\u4e2d\u63d0\u53d6\u503c\u3002\u5b83\u6700\u9002\u5408RESTful Web\u670d\u52a1\uff0c\u5176\u4e2dURL\u5305\u542b\u8def\u5f84\u53d8\u91cf\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u4e00\u4e2a\u65b9\u6cd5\u4e2d\u5b9a\u4e49\u591a\u4e2a@PathVariable\u3002<\/span>
\n @RequestParam: <\/strong>\u7528\u4e8e\u4eceURL\u63d0\u53d6\u67e5\u8be2\u53c2\u6570\u3002\u4e5f\u79f0\u4e3a\u67e5\u8be2\u53c2\u6570<\/strong>\u3002\u5b83\u6700\u9002\u5408Web\u5e94\u7528\u7a0b\u5e8f\u3002\u5982\u679cURL\u4e2d\u4e0d\u5b58\u5728\u67e5\u8be2\u53c2\u6570\uff0c\u5219\u53ef\u4ee5\u6307\u5b9a\u9ed8\u8ba4\u503c\u3002<\/span>
\n @RequestHeader: <\/strong>\u7528\u4e8e\u83b7\u53d6\u6709\u5173HTTP\u8bf7\u6c42\u6807\u5934\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u6211\u4eec\u5c06\u6b64\u6ce8\u89e3\u7528\u4f5c\u65b9\u6cd5\u53c2\u6570<\/strong>\u3002\u6ce8\u89e3\u7684\u53ef\u9009\u5143\u7d20\u662f\u540d\u79f0\uff0c\u5fc5\u586b\uff0c\u503c\uff0cdefaultValue\u3002 <\/strong>\u5bf9\u4e8e\u6807\u9898\u4e2d\u7684\u6bcf\u4e2a\u7ec6\u8282\uff0c\u6211\u4eec\u5e94\u6307\u5b9a\u5355\u72ec\u7684\u6ce8\u89e3\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u4e00\u79cd\u65b9\u6cd5\u4e2d\u591a\u6b21\u4f7f\u7528\u5b83<\/span>
\n @RestController: <\/strong>\u53ef\u4ee5\u5c06\u5176\u89c6\u4e3a @Controller <\/strong>\u548c @ResponseBody <\/strong>\u6ce8\u89e3\u7684\u7ec4\u5408\u3002<\/strong> @RestController\u6ce8\u89e3\u672c\u8eab\u4f7f\u7528@ResponseBody\u6ce8\u89e3\u8fdb\u884c\u6ce8\u89e3\u3002\u65e0\u9700\u4f7f\u7528@ResponseBody\u6ce8\u89e3\u6bcf\u4e2a\u65b9\u6cd5\u3002<\/span>
\n @RequestAttribute: <\/strong>\u5b83\u5c06\u65b9\u6cd5\u53c2\u6570\u7ed1\u5b9a\u5230\u8bf7\u6c42\u5c5e\u6027\u3002\u5b83\u63d0\u4f9b\u4e86\u4ece\u63a7\u5236\u5668\u65b9\u6cd5\u65b9\u4fbf\u5730\u8bbf\u95ee\u8bf7\u6c42\u5c5e\u6027\u7684\u65b9\u6cd5\u3002\u501f\u52a9@RequestAttribute\u6279\u6ce8\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbf\u95ee\u670d\u52a1\u5668\u7aef\u586b\u5145\u7684\u5bf9\u8c61\u3002<\/span> <\/p>\n

\n \u6ce8\u610f: <\/span>\u6211\u4eec\u5728RESTful Web\u670d\u52a1\u6559\u7a0b\u4e2d\u4f7f\u7528\u4e86\u4ee5\u4e0a\u6240\u6709\u793a\u4f8b\u548c\u771f\u5b9e\u793a\u4f8b\u3002\n <\/div>\n
\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"SpringBoot \u6ce8\u89e3zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[104],"tags":[],"class_list":["post-581","post","type-post","status-publish","format-standard","hentry","category-bc_springboot_jc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/581"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=581"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/581\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}