{"id":6718,"date":"2025-04-07T21:28:25","date_gmt":"2025-04-07T13:28:25","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6718"},"modified":"2025-04-07T21:28:25","modified_gmt":"2025-04-07T13:28:25","slug":"spring-boot-%e6%ba%90%e7%a0%81%e4%b8%ad%e5%a5%bd%e7%9a%84%e8%ae%be%e8%ae%a1%e7%90%86%e5%bf%b5","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6718","title":{"rendered":"spring-boot \u6e90\u7801\u4e2d\u597d\u7684\u8bbe\u8ba1\u7406\u5ff5"},"content":{"rendered":"<p>Spring Boot \u4f5c\u4e3a\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684 Java \u5f00\u53d1\u6846\u67b6\uff0c\u5176\u6e90\u7801\u4e2d\u8574\u542b\u4e86\u4f17\u591a\u4f18\u79c0\u7684\u8bbe\u8ba1\u7406\u5ff5\uff0c\u5b66\u4e60\u8fd9\u4e9b\u7406\u5ff5\u53ef\u4ee5\u63d0\u5347\u4f60\u5728\u8f6f\u4ef6\u8bbe\u8ba1\u548c\u5f00\u53d1\u65b9\u9762\u7684\u80fd\u529b\u3002\u4ee5\u4e0b\u662f\u4ece Spring Boot \u6e90\u7801\u4e2d\u53ef\u4ee5\u5b66\u4e60\u5230\u7684\u4e00\u4e9b\u597d\u7684\u8bbe\u8ba1\u7406\u5ff5\uff1a<\/p>\n<h3>1. \u7ea6\u5b9a\u4f18\u4e8e\u914d\u7f6e\uff08Convention over Configuration\uff09<\/h3>\n<ul>\n<li><strong>\u7406\u5ff5\u9610\u91ca<\/strong>\uff1aSpring Boot \u91c7\u7528\u4e86\u7ea6\u5b9a\u4f18\u4e8e\u914d\u7f6e\u7684\u539f\u5219\uff0c\u51cf\u5c11\u4e86\u5f00\u53d1\u8005\u9700\u8981\u7f16\u5199\u7684\u914d\u7f6e\u91cf\u3002\u6846\u67b6\u4e3a\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u63d0\u4f9b\u4e86\u9ed8\u8ba4\u914d\u7f6e\uff0c\u5f00\u53d1\u8005\u53ea\u9700\u9075\u5faa\u8fd9\u4e9b\u7ea6\u5b9a\uff0c\u5c31\u53ef\u4ee5\u5feb\u901f\u642d\u5efa\u8d77\u4e00\u4e2a\u53ef\u7528\u7684\u5e94\u7528\u3002\u53ea\u6709\u5728\u9ed8\u8ba4\u914d\u7f6e\u4e0d\u6ee1\u8db3\u9700\u6c42\u65f6\uff0c\u624d\u9700\u8981\u8fdb\u884c\u989d\u5916\u7684\u914d\u7f6e\u3002<\/li>\n<li><strong>\u6e90\u7801\u4f53\u73b0<\/strong>\uff1a\u5728 Spring Boot \u4e2d\uff0c\u5f88\u591a\u914d\u7f6e\u90fd\u6709\u9ed8\u8ba4\u503c\u3002\u4f8b\u5982\uff0c\u5d4c\u5165\u5f0f\u670d\u52a1\u5668\uff08\u5982 Tomcat\u3001Jetty\uff09\u7684\u9ed8\u8ba4\u7aef\u53e3\u662f 8080\uff0c\u5f00\u53d1\u8005\u5982\u679c\u6ca1\u6709\u7279\u522b\u6307\u5b9a\u7aef\u53e3\uff0c\u5e94\u7528\u5c31\u4f1a\u4f7f\u7528\u8fd9\u4e2a\u9ed8\u8ba4\u7aef\u53e3\u542f\u52a8\u3002\u6e90\u7801\u91cc\u901a\u8fc7\u4e00\u7cfb\u5217\u7684\u81ea\u52a8\u914d\u7f6e\u7c7b\uff08\u5982 <code>ServerProperties<\/code>\uff09\u6765\u5b9e\u73b0\u8fd9\u4e9b\u9ed8\u8ba4\u914d\u7f6e\u3002<\/li>\n<\/ul>\n<h3>2. \u81ea\u52a8\u914d\u7f6e\uff08Auto - Configuration\uff09<\/h3>\n<ul>\n<li><strong>\u7406\u5ff5\u9610\u91ca<\/strong>\uff1aSpring Boot \u7684\u81ea\u52a8\u914d\u7f6e\u673a\u5236\u80fd\u591f\u6839\u636e\u7c7b\u8def\u5f84\u4e0b\u7684\u4f9d\u8d56\u548c\u5e94\u7528\u7684\u914d\u7f6e\uff0c\u81ea\u52a8\u4e3a\u5e94\u7528\u6dfb\u52a0\u5408\u9002\u7684\u914d\u7f6e\u3002\u8fd9\u5927\u5927\u7b80\u5316\u4e86\u5f00\u53d1\u8fc7\u7a0b\uff0c\u5f00\u53d1\u8005\u65e0\u9700\u624b\u52a8\u914d\u7f6e\u5404\u79cd Bean \u548c\u7ec4\u4ef6\u3002<\/li>\n<li><strong>\u6e90\u7801\u4f53\u73b0<\/strong>\uff1aSpring Boot \u4e2d\u6709\u5927\u91cf\u7684\u81ea\u52a8\u914d\u7f6e\u7c7b\uff0c\u8fd9\u4e9b\u7c7b\u4f4d\u4e8e <code>org.springframework.boot.autoconfigure<\/code> \u5305\u4e0b\u3002\u4f8b\u5982\uff0c<code>DataSourceAutoConfiguration<\/code> \u7c7b\u4f1a\u6839\u636e\u7c7b\u8def\u5f84\u4e0b\u662f\u5426\u5b58\u5728\u6570\u636e\u5e93\u8fde\u63a5\u76f8\u5173\u7684\u4f9d\u8d56\uff08\u5982 JDBC \u9a71\u52a8\uff09\uff0c\u81ea\u52a8\u914d\u7f6e\u6570\u636e\u6e90\u3002\u8fd9\u4e9b\u81ea\u52a8\u914d\u7f6e\u7c7b\u4f7f\u7528\u4e86 <code>@Conditional<\/code> \u6ce8\u89e3\u6765\u6839\u636e\u4e0d\u540c\u7684\u6761\u4ef6\u8fdb\u884c\u914d\u7f6e\u3002<\/li>\n<\/ul>\n<h3>3. \u6a21\u5757\u5316\u8bbe\u8ba1<\/h3>\n<ul>\n<li><strong>\u7406\u5ff5\u9610\u91ca<\/strong>\uff1aSpring Boot \u5c06\u4e0d\u540c\u7684\u529f\u80fd\u62c6\u5206\u6210\u591a\u4e2a\u6a21\u5757\uff0c\u6bcf\u4e2a\u6a21\u5757\u90fd\u6709\u660e\u786e\u7684\u804c\u8d23\u3002\u8fd9\u6837\u7684\u8bbe\u8ba1\u4f7f\u5f97\u4ee3\u7801\u7ed3\u6784\u6e05\u6670\uff0c\u6613\u4e8e\u7ef4\u62a4\u548c\u6269\u5c55\u3002\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u9009\u62e9\u5f15\u5165\u76f8\u5e94\u7684\u6a21\u5757\u3002<\/li>\n<li><strong>\u6e90\u7801\u4f53\u73b0<\/strong>\uff1aSpring Boot \u9879\u76ee\u672c\u8eab\u7531\u591a\u4e2a\u6a21\u5757\u7ec4\u6210\uff0c\u5982 <code>spring - boot<\/code>\u3001<code>spring - boot - autoconfigure<\/code>\u3001<code>spring - boot - starter<\/code> \u7b49\u3002\u5176\u4e2d\uff0c<code>spring - boot - starter<\/code> \u6a21\u5757\u662f\u4e00\u7cfb\u5217\u4f9d\u8d56\u7684\u96c6\u5408\uff0c\u6bcf\u4e2a <code>starter<\/code> \u90fd\u5bf9\u5e94\u4e00\u4e2a\u7279\u5b9a\u7684\u529f\u80fd\uff0c\u5982 <code>spring - boot - starter - web<\/code> \u7528\u4e8e\u5f00\u53d1 Web \u5e94\u7528\uff0c<code>spring - boot - starter - data - jpa<\/code> \u7528\u4e8e\u4f7f\u7528 JPA \u8fdb\u884c\u6570\u636e\u8bbf\u95ee\u3002<\/li>\n<\/ul>\n<h3>4. \u4f9d\u8d56\u6ce8\u5165\uff08Dependency Injection\uff09<\/h3>\n<ul>\n<li><strong>\u7406\u5ff5\u9610\u91ca<\/strong>\uff1a\u4f9d\u8d56\u6ce8\u5165\u662f Spring \u6846\u67b6\u7684\u6838\u5fc3\u7279\u6027\u4e4b\u4e00\uff0cSpring Boot \u4e5f\u5145\u5206\u5229\u7528\u4e86\u8fd9\u4e00\u7279\u6027\u3002\u901a\u8fc7\u4f9d\u8d56\u6ce8\u5165\uff0c\u5bf9\u8c61\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\u7531\u5bb9\u5668\u6765\u7ba1\u7406\uff0c\u964d\u4f4e\u4e86\u4ee3\u7801\u7684\u8026\u5408\u5ea6\uff0c\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u6d4b\u8bd5\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/li>\n<li><strong>\u6e90\u7801\u4f53\u73b0<\/strong>\uff1a\u5728 Spring Boot \u4e2d\uff0c\u4f7f\u7528 <code>@Autowired<\/code>\u3001<code>@Inject<\/code> \u7b49\u6ce8\u89e3\u6765\u5b9e\u73b0\u4f9d\u8d56\u6ce8\u5165\u3002\u4f8b\u5982\uff0c\u5728\u4e00\u4e2a\u670d\u52a1\u7c7b\u4e2d\u53ef\u4ee5\u901a\u8fc7 <code>@Autowired<\/code> \u6ce8\u89e3\u6ce8\u5165\u53e6\u4e00\u4e2a\u670d\u52a1\u7c7b\u6216 Repository \u63a5\u53e3\u3002\u6e90\u7801\u4e2d\u901a\u8fc7 <code>BeanFactory<\/code> \u548c <code>ApplicationContext<\/code> \u6765\u7ba1\u7406\u548c\u6ce8\u5165 Bean\u3002<\/li>\n<\/ul>\n<h3>5. \u4e8b\u4ef6\u9a71\u52a8\u67b6\u6784<\/h3>\n<ul>\n<li><strong>\u7406\u5ff5\u9610\u91ca<\/strong>\uff1aSpring Boot \u91c7\u7528\u4e86\u4e8b\u4ef6\u9a71\u52a8\u67b6\u6784\uff0c\u901a\u8fc7\u53d1\u5e03\u548c\u76d1\u542c\u4e8b\u4ef6\u6765\u5b9e\u73b0\u7ec4\u4ef6\u4e4b\u95f4\u7684\u89e3\u8026\u3002\u5f53\u67d0\u4e2a\u4e8b\u4ef6\u53d1\u751f\u65f6\uff0c\u76f8\u5173\u7684\u76d1\u542c\u5668\u4f1a\u88ab\u89e6\u53d1\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u6e90\u7801\u4f53\u73b0<\/strong>\uff1aSpring Boot \u4e2d\u5b9a\u4e49\u4e86\u8bb8\u591a\u4e8b\u4ef6\uff0c\u5982 <code>ApplicationStartedEvent<\/code>\u3001<code>ApplicationReadyEvent<\/code> \u7b49\u3002\u5f00\u53d1\u8005\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0 <code>ApplicationListener<\/code> \u63a5\u53e3\u6765\u76d1\u542c\u8fd9\u4e9b\u4e8b\u4ef6\u3002\u5728\u6e90\u7801\u4e2d\uff0c\u4e8b\u4ef6\u7684\u53d1\u5e03\u548c\u76d1\u542c\u673a\u5236\u7531 <code>ApplicationEventPublisher<\/code> \u548c <code>SimpleApplicationEventMulticaster<\/code> \u7b49\u7c7b\u6765\u5b9e\u73b0\u3002<\/li>\n<\/ul>\n<h3>6. \u9762\u5411\u63a5\u53e3\u7f16\u7a0b<\/h3>\n<ul>\n<li><strong>\u7406\u5ff5\u9610\u91ca<\/strong>\uff1aSpring Boot \u6e90\u7801\u4e2d\u5e7f\u6cdb\u4f7f\u7528\u4e86\u9762\u5411\u63a5\u53e3\u7f16\u7a0b\u7684\u601d\u60f3\u3002\u901a\u8fc7\u5b9a\u4e49\u63a5\u53e3\uff0c\u4e0d\u540c\u7684\u5b9e\u73b0\u7c7b\u53ef\u4ee5\u5b9e\u73b0\u76f8\u540c\u7684\u63a5\u53e3\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002<\/li>\n<li><strong>\u6e90\u7801\u4f53\u73b0<\/strong>\uff1a\u4f8b\u5982\uff0c<code>CommandLineRunner<\/code> \u548c <code>ApplicationRunner<\/code> \u63a5\u53e3\u7528\u4e8e\u5728\u5e94\u7528\u542f\u52a8\u540e\u6267\u884c\u4e00\u4e9b\u521d\u59cb\u5316\u64cd\u4f5c\u3002\u5f00\u53d1\u8005\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e9b\u63a5\u53e3\u6765\u7f16\u5199\u81ea\u5df1\u7684\u542f\u52a8\u903b\u8f91\u3002\u53e6\u5916\uff0c\u5728\u6570\u636e\u8bbf\u95ee\u5c42\uff0cSpring Data JPA \u5b9a\u4e49\u4e86\u4e00\u7cfb\u5217\u7684 Repository \u63a5\u53e3\uff0c\u5f00\u53d1\u8005\u53ea\u9700\u8981\u5b9a\u4e49\u63a5\u53e3\uff0cSpring Data JPA \u4f1a\u81ea\u52a8\u751f\u6210\u5b9e\u73b0\u7c7b\u3002<\/li>\n<\/ul>\n<h3>7. \u65e5\u5fd7\u62bd\u8c61\u548c\u9002\u914d<\/h3>\n<ul>\n<li><strong>\u7406\u5ff5\u9610\u91ca<\/strong>\uff1aSpring Boot \u5bf9\u65e5\u5fd7\u6846\u67b6\u8fdb\u884c\u4e86\u62bd\u8c61\u548c\u9002\u914d\uff0c\u652f\u6301\u591a\u79cd\u65e5\u5fd7\u6846\u67b6\uff08\u5982 Logback\u3001Log4j2 \u7b49\uff09\u3002\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65e5\u5fd7\u6846\u67b6\uff0c\u800c\u4e0d\u9700\u8981\u4fee\u6539\u5e94\u7528\u7684\u4ee3\u7801\u3002<\/li>\n<li><strong>\u6e90\u7801\u4f53\u73b0<\/strong>\uff1aSpring Boot \u901a\u8fc7 <code>spring - boot - starter - logging<\/code> \u6a21\u5757\u6765\u5b9e\u73b0\u65e5\u5fd7\u7684\u62bd\u8c61\u548c\u9002\u914d\u3002\u5728\u6e90\u7801\u4e2d\uff0c\u4f7f\u7528\u4e86 <code>SLF4J<\/code>\uff08Simple Logging Facade for Java\uff09\u4f5c\u4e3a\u65e5\u5fd7\u95e8\u9762\uff0c\u5e95\u5c42\u53ef\u4ee5\u6839\u636e\u7c7b\u8def\u5f84\u4e0b\u7684\u4f9d\u8d56\u9009\u62e9\u4e0d\u540c\u7684\u65e5\u5fd7\u5b9e\u73b0\u3002 <\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Spring Boot \u4f5c\u4e3a\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684 Java \u5f00\u53d1\u6846\u67b6\uff0c\u5176\u6e90\u7801\u4e2d\u8574\u542b\u4e86\u4f17\u591a\u4f18\u79c0\u7684\u8bbe\u8ba1\u7406\u5ff5\uff0c\u5b66\u4e60\u8fd9\u4e9b\u7406\u5ff5&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6718\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[123],"tags":[],"class_list":["post-6718","post","type-post","status-publish","format-standard","hentry","category-123"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6718","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6718"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6718\/revisions"}],"predecessor-version":[{"id":6719,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6718\/revisions\/6719"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}