{"id":216607,"date":"2021-04-30T09:18:02","date_gmt":"2021-04-30T01:18:02","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=216607"},"modified":"2021-04-23T09:19:12","modified_gmt":"2021-04-23T01:19:12","slug":"core-principle-springboot","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/core-principle-springboot.html","title":{"rendered":"springboot\u6838\u5fc3\u539f\u7406"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nSpring Boot\u662f\u4f34\u968f\u7740Spring4.0\u5171\u540c\u8bde\u751f\u7684\uff0c\u5b83\u7684\u76ee\u7684\u5c31\u662f\u7b80\u5316spring\u7684\u914d\u7f6e\u53ca\u5f00\u53d1\uff0c\u5e76\u534f\u52a9\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u6574\u4f53\u7ba1\u7406\u5e94\u7528\u7a0b\u5e8f\u7684\u914d\u7f6e\u800c\u4e0d\u518d\u50cf\u4ee5\u524d\u90a3\u6837\u9700\u8981\u505a\u5927\u91cf\u7684\u914d\u7f6e\u5de5\u4f5c\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f88\u591a\u5f00\u53d1\u7ec4\u4ef6\uff0c\u5e76\u4e14\u5185\u5d4c\u4e86web\u5e94\u7528\u5bb9\u5668\uff0c\u5982tomcat\u548cJetty\u7b49\u3002\u5176\u76ee\u7684\u4fbf\u662f\u4f7f\u6211\u4eec\u7684\u5f00\u53d1\u53d8\u5f97\u7b80\u5316\u5e76\u4e14\u80fd\u5927\u5e45\u5ea6\u63d0\u9ad8\u5f00\u53d1\u4eba\u5458\u7684\u5f00\u53d1\u6548\u7387\uff0c\u4e3a\u4e86\u7b80\u5316Spring\u529f\u80fd\u7684\u914d\u7f6e\u6211\u4eec\u53ef\u4ee5\u5f15\u5165\u6216\u542f\u52a8\u6211\u4eec\u9700\u8981\u7684Spring\u529f\u80fd\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u5c31\u662f\u907f\u514d\u5f00\u53d1\u4eba\u5458\u8fc7\u591a\u7684\u5bf9\u6846\u67b6\u7684\u5173\u6ce8\uff0c\u800c\u628a\u66f4\u591a\u7684\u7cbe\u529b\u4e0e\u65f6\u95f4\u653e\u5728\u7cfb\u7edf\u7684\u4e1a\u52a1\u903b\u8f91\u4ee3\u7801\u4e2d\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
1.\u57fa\u4e8e\u4f60\u5bf9springboot\u7684\u7406\u89e3\u63cf\u8ff0\u4e00\u4e0b\u4ec0\u4e48\u662fspringboot<\/strong><\/div>\n

\u5b83\u662f\u4e00\u4e2a\u670d\u52a1\u4e8espring\u6846\u67b6\u7684\u6846\u67b6\uff0c\u80fd\u591f\u7b80\u5316\u914d\u7f6e\u6587\u4ef6\uff0c\u5feb\u901f\u6784\u5efaweb\u5e94\u7528\uff0c
\n\u5185\u7f6etomcat\uff0c\u65e0\u9700\u6253\u5305\u90e8\u7f72\uff0c\u76f4\u63a5\u8fd0\u884c\u3002<\/p>\n

2.\u7ea6\u5b9a\u4f18\u4e8e\u914d\u7f6e\u6307\u7684\u662f\u4ec0\u4e48\uff1f<\/strong><\/div>\n

maven \u7684\u76ee\u5f55\u7ed3\u6784
\na) \u9ed8\u8ba4\u6709 resources \u6587\u4ef6\u5939\u5b58\u653e\u914d\u7f6e\u6587\u4ef6
\nb) \u9ed8\u8ba4\u6253\u5305\u65b9\u5f0f\u4e3a jar
\nspring-boot-starter-web \u4e2d\u9ed8\u8ba4\u5305\u542b spring mvc \u76f8\u5173\u4f9d\u8d56\u4ee5\u53ca\u5185\u7f6e\u7684 tomcat \u5bb9\u5668\uff0c\u4f7f\u5f97\u6784\u5efa\u4e00\u4e2a web \u5e94\u7528\u66f4\u52a0\u7b80\u5355
\n\u9ed8\u8ba4\u63d0\u4f9b application.properties\/yml \u6587\u4ef6
\n\u9ed8\u8ba4\u901a\u8fc7 spring.profiles.active \u5c5e\u6027\u6765\u51b3\u5b9a\u8fd0\u884c\u73af\u5883\u65f6\u8bfb\u53d6\u7684\u914d\u7f6e\u6587\u4ef6
\nEnableAutoConfiguration \u9ed8\u8ba4\u5bf9\u4e8e\u4f9d\u8d56\u7684 starter \u8fdb\u884c\u81ea\u52a8<\/p>\n

3.@SpringBootApplication\u7531\u54ea\u51e0\u4e2a\u6ce8\u89e3\u7ec4\u6210\uff0c\u8fd9\u51e0\u4e2a\u6ce8\u89e3\u5206\u522b\u8868\u793a\u4ec0\u4e48\u4f5c\u7528<\/strong><\/div>\n

SpringBootApplication \u672c\u8d28\u4e0a\u662f\u7531 3 \u4e2a\u6ce8\u89e3\u7ec4\u6210\uff0c\u5206\u522b\u662f<\/p>\n

@Configuration
\n@EnableAutoConfiguration
\n@ComponentScan
\n@Configuration\uff1a
\n\u5728\u542f\u52a8\u7c7b\u91cc\u9762\u6807\u6ce8\u4e86@Configuration\uff0c\u610f\u5473\u7740\u5b83\u5176\u5b9e\u4e5f\u662f\u4e00\u4e2a IoC
\n\u5bb9\u5668\u7684\u914d\u7f6e\u7c7b<\/p>\n

@EnableAutoConfiguration\uff1a
\nspringboot \u5e94\u7528\u628a\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684@Configuration \u914d\u7f6e
\n\u90fd\u52a0\u8f7d\u5230\u5f53\u524d SpringBoot \u521b\u5efa\u5e76\u4f7f\u7528\u7684 IoC \u5bb9\u5668\u4e2d\u3002<\/p>\n

@ComponentScan\uff1a
\nComponentScan \u9ed8\u8ba4\u4f1a\u626b\u63cf\u5f53\u524d package \u4e0b\u7684\u7684\u6240\u6709\u52a0
\n\u4e86@Component \u3001@Repository\u3001@Service\u3001@Controller\u7684\u7c7b\u5230 IoC \u5bb9\u5668\u4e2d\uff1b<\/p>\n

4.springboot\u81ea\u52a8\u88c5\u914d\u7684\u5b9e\u73b0\u539f\u7406<\/strong><\/div>\n

\"\"<\/p>\n

\u5982\u679c\u662f\u4e4b\u524d\u7684spring\u4e2d\u4f7f\u7528redis\u9700\u8981\u5728xml\u5b9a\u4e49bean,\u73b0\u5728\u53ea\u9700\u8981\u4f9d\u8d56\u4e00\u4e2aspring-boot-starter-data-redis
\n\u7684jar\u5305,jar\u4e2d\u5b9a\u4e49\u4e86RedisConfiguration,\u5f53\u542f\u52a8\u7684\u65f6\u5019spring\u4f1a\u81ea\u52a8\u88c5\u8f7dRedisConfiguration,\u90a3spring\u662f\u5982\u4f55\u77e5\u9053\u914d\u7f6e\u7c7b\u5728\u54ea\u91cc\u7684\u5462?RedisConfiguration\u7c7b\u7684\u8def\u5f84\u653e\u5728\u4e86classpath*META-INF\/spring.factories\u7684\u6587\u4ef6\u4e2d,spring\u4f1a\u52a0\u8f7d\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u914d\u7f6e\u7684configuration<\/p>\n

\uff081\uff09SpringApplication.run(AppConfig.class,args);\u6267\u884c\u6d41\u7a0b\u4e2d\u6709refreshContext(context);\u8fd9\u53e5\u8bdd.
\n\uff082\uff09refreshContext(context);\u5185\u90e8\u4f1a\u89e3\u6790\u6211\u4eec\u7684\u914d\u7f6e\u7c7b\u4e0a\u7684\u6807\u7b7e.\u5b9e\u73b0\u81ea\u52a8\u88c5\u914d\u529f\u80fd\u7684\u6ce8\u89e3@EnableAutoConfiguration
\n\uff083\uff09\u4f1a\u89e3\u6790@EnableAutoConfiguration\u8fd9\u4e2a\u6ce8\u89e3\u91cc\u9762\u7684@Import\u5f15\u5165\u7684\u914d\u7f6e\u7c7b.AutoConfigurationImportSelector
\n\uff084\uff09AutoConfigurationImportSelector\u8fd9\u4e2a\u7c7b\u4e2d\u6709\u8fd9\u4e2a\u65b9
\n\u6cd5.SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());
\n\uff085\uff09SpringFactoriesLoader.loadFactoryNames\u7684\u4f5c\u7528\u5c31\u662f\u8bfb\u53d6jar\u5305\u4e2d\u7684\/\u9879\u76ee\u4e2d\u7684META-INF\/spring.factories\u6587\u4ef6.
\n\uff086\uff09spring.factories\u914d\u7f6e\u4e86\u8981\u81ea\u52a8\u88c5\u914d\u7684Configuration\u7c7b<\/p>\n

5.spring\u4e2d\u7684spi\u673a\u5236\u7684\u539f\u7406\u662f\u4ec0\u4e48\uff1f<\/strong><\/div>\n

SPI\u7684\u5168\u540d\u4e3aService Provider Interface\uff0c\u4e3a\u67d0\u4e2a\u63a5\u53e3\u5bfb\u627e\u670d\u52a1\u5b9e\u73b0\u7684\u673a\u5236\u3002
\n\u5f53\u670d\u52a1\u7684\u63d0\u4f9b\u8005\uff0c\u63d0\u4f9b\u4e86\u670d\u52a1\u63a5\u53e3\u7684\u4e00\u79cd\u5b9e\u73b0\u4e4b\u540e\uff0c\u5728jar\u5305\u7684META-INF\/services\/\u76ee\u5f55\u91cc\u540c\u65f6\u521b\u5efa\u4e00\u4e2a\u4ee5\u670d\u52a1\u63a5\u53e3\u547d\u540d\u7684\u6587\u4ef6\u3002\u8be5\u6587\u4ef6\u91cc\u5c31\u662f\u5b9e\u73b0\u8be5\u670d\u52a1\u63a5\u53e3\u7684\u5177\u4f53\u5b9e\u73b0\u7c7b\u3002\u800c\u5f53\u5916\u90e8\u7a0b\u5e8f\u88c5\u914d\u8fd9\u4e2a\u6a21\u5757\u7684\u65f6\u5019\uff0c\u5c31\u80fd\u901a\u8fc7\u8be5jar\u5305META-INF\/services\/\u91cc\u7684\u914d\u7f6e\u6587\u4ef6\u627e\u5230\u5177\u4f53\u7684\u5b9e\u73b0\u7c7b\u540d\uff0c\u5e76\u88c5\u8f7d\u5b9e\u4f8b\u5316\uff0c\u5b8c\u6210\u6a21\u5757\u7684\u6ce8\u5165\u3002\u901a\u8fc7\u8fd9\u4e2a\u7ea6\u5b9a\uff0c\u5c31\u4e0d\u9700\u8981\u628a\u670d\u52a1\u653e\u5728\u4ee3\u7801\u4e2d\u4e86\uff0c\u901a\u8fc7\u6a21\u5757\u88ab\u88c5\u914d\u7684\u65f6\u5019\u5c31\u53ef\u4ee5\u53d1\u73b0\u670d\u52a1\u7c7b\u4e86\u3002<\/p>\n

\u5728springboot\u7684\u81ea\u52a8\u88c5\u914d\u8fc7\u7a0b\u4e2d\uff0c\u6700\u7ec8\u4f1a\u52a0\u8f7dMETA-INF\/spring.factories\u6587\u4ef6\uff0c\u800c\u52a0\u8f7d\u7684\u8fc7\u7a0b\u662f\u7531SpringFactoriesLoader\u52a0\u8f7d\u7684\u3002\u4eceCLASSPATH\u4e0b\u7684\u6bcf\u4e2aJar\u5305\u4e2d\u641c\u5bfb\u6240\u6709META-INF\/spring.factories\u914d\u7f6e\u6587\u4ef6\uff0c\u7136\u540e\u5c06\u89e3\u6790properties\u6587\u4ef6\uff0c\u627e\u5230\u6307\u5b9a\u540d\u79f0\u7684\u914d\u7f6e\u540e\u8fd4\u56de\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5176\u5b9e\u8fd9\u91cc\u4e0d\u4ec5\u4ec5\u662f\u4f1a\u53bbClassPath\u8def\u5f84\u4e0b\u67e5\u627e\uff0c\u4f1a\u626b\u63cf\u6240\u6709\u8def\u5f84\u4e0b\u7684Jar\u5305\uff0c\u53ea\u4e0d\u8fc7\u8fd9\u4e2a\u6587\u4ef6\u53ea\u4f1a\u5728Classpath\u4e0b\u7684jar\u5305\u4e2d\u3002<\/p>\n

\u901a\u8fc7spi\u6280\u672f\u53ef\u4ee5\u81ea\u5b9a\u4e49starter,\u5728\u81ea\u5b9a\u4e49\u7684\u914d\u7f6e\u6587\u4ef6META-INF\/spring.factories\u4e2d\u52a0\u5165\u5b9e\u73b0\u7c7b,\u4f9d\u8d56\u8fd9\u4e2astarter\u7684\u9879\u76ee\u5c31\u4f1a\u626b\u63cfjar\u5305\u4e0b\u7684\u914d\u7f6e,\u627e\u5230\u5b9e\u73b0\u7c7b\u8fdb\u884c\u88c5\u8f7d\u5b9e\u4f8b\u5316<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5b83\u662f\u4e00\u4e2a\u670d\u52a1\u4e8espring\u6846\u67b6\u7684\u6846\u67b6\uff0c\u80fd\u591f\u7b80\u5316\u914d\u7f6e\u6587\u4ef6\uff0c\u5feb\u901f\u6784\u5efaweb\u5e94\u7528\uff0c \u5185\u7f6etomcat\uff0c\u65e0\u9700\u6253\u5305\u90e8\u7f72\uff0c […]<\/p>\n","protected":false},"author":1903,"featured_media":163508,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-216607","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/216607","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1903"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=216607"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/216607\/revisions"}],"predecessor-version":[{"id":216776,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/216607\/revisions\/216776"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/163508"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=216607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=216607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=216607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}