{"id":2819,"date":"2018-05-26T20:03:41","date_gmt":"2018-05-26T12:03:41","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=2819"},"modified":"2018-05-26T20:03:41","modified_gmt":"2018-05-26T12:03:41","slug":"spring-boot-%e5%90%af%e5%8a%a8%e9%85%8d%e7%bd%ae%e5%8e%9f%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=2819","title":{"rendered":"Spring boot \u542f\u52a8\u914d\u7f6e\u539f\u7406"},"content":{"rendered":"<p>\n\tSpring boot \u7684\u542f\u52a8\u65b9\u6cd5\u8d70\u7684\u662f main \u65b9\u6cd5\uff0cmain \u65b9\u6cd5\u4e2d\u4e00\u822c\u8c03\u7528&nbsp;SpringApplication.run()\u3002run \u65b9\u6cd5\u7684\u7b2c\u4e00\u53c2\u6570\u662f\u4f7f\u7528&nbsp;@SpringBootApplication \u7684\u7c7b\u3002\u90a3\u4e48&nbsp;@SpringBootApplication \u6ce8\u89e3\u7684\u539f\u7406\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u9700\u8981&nbsp;@SpringBootApplication \u6ce8\u89e3\uff1fSpring boot\u7684\u542f\u52a8\u539f\u7406\u53c8\u662f\u4ec0\u4e48\uff1f\u672c\u6587\u5c06\u5e26\u4f60\u901a\u8fc7\u9605\u8bfb\u6e90\u7801\u6765\u4e86\u89e3\u5b83\u3002\n<\/p>\n<p>\n\tSpringApplication \u7684 run() \u5176\u5b9e\u662f\u6784\u5efa\u4e86\u4e00\u4e2a&nbsp;SpringApplication \u5bf9\u8c61\u3002\u8be5\u5bf9\u8c61\u5728\u521b\u5efa\u65f6\uff0c\u4f1a\u6267\u884c&nbsp;initialize(sources) \uff0c\u8fdb\u884c\u521d\u59cb\u5316\u64cd\u4f5c\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\nprivate void initialize(Object[] sources) {\r\n    \/\/\u4fdd\u5b58\u4e3b\u914d\u7f6e\u7c7b\r\n    if (sources != null &amp;&amp; sources.length &gt; 0) {\r\n        this.sources.addAll(Arrays.asList(sources));\r\n    }\r\n    \/\/\u5224\u65ad\u5f53\u524d\u662f\u5426\u4e00\u4e2aweb\u5e94\u7528\r\n    this.webEnvironment = deduceWebEnvironment();\r\n    \/\/\u4ece\u7c7b\u8def\u5f84\u4e0b\u627e\u5230META-INF\/spring.factories\u914d\u7f6e\u7684\u6240\u6709ApplicationContextInitializer\uff1b\u7136\u540e\u4fdd\u5b58\u8d77\u6765\r\n    setInitializers((Collection) getSpringFactoriesInstances(\r\n        ApplicationContextInitializer.class));\r\n    \/\/\u4ece\u7c7b\u8def\u5f84\u4e0b\u627e\u5230ETA-INF\/spring.factories\u914d\u7f6e\u7684\u6240\u6709ApplicationListener\r\n    setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));\r\n    \/\/\u4ece\u591a\u4e2a\u914d\u7f6e\u7c7b\u4e2d\u627e\u5230\u6709main\u65b9\u6cd5\u7684\u4e3b\u914d\u7f6e\u7c7b\r\n    this.mainApplicationClass = deduceMainApplicationClass();\r\n}<\/pre>\n<p>\n\tinitialize \u4f1a\u521d\u59cb\u5316\u5f88\u591a&nbsp;ApplicationContextInitializer \u7c7b\u3002\u4ece\u7c7b\u8def\u5f84\u4e0b\u627e\u5230 META-INF\/spring.factories \u914d\u7f6e\u7684\u6240\u6709 ApplicationContextInitializer\uff0c\u5177\u4f53\u5982\u4e0b\u56fe\u6240\u793a\uff1a\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"ApplicationContextInitializer\" src=\"http:\/\/oeb6uoqqa.bkt.clouddn.com\/applicationContextInitializer.png\" style=\"width: 490px;height: 161px\" \/>\n<\/p>\n<p>\n\t\u8fd8\u4f1a\u4ece\u7c7b\u8def\u5f84\u4e0b\u627e\u5230 META-INF\/spring.factories \u914d\u7f6e\u7684\u6240\u6709 ApplicationContextInitializer\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"ApplicationListener\" src=\"http:\/\/oeb6uoqqa.bkt.clouddn.com\/ApplicationListener.png\" style=\"width: 490px;height: 251px\" \/>\n<\/p>\n<p>\n\t\u5f53\u4e0a\u9762\u7684\u90fd\u52a0\u8f7d\u5b8c\u6210\u540e\uff0c\u771f\u6b63\u7684\u5f00\u59cb\u6267\u884c run() \u65b9\u6cd5\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\npublic ConfigurableApplicationContext run(String... args) {\r\n   StopWatch stopWatch = new StopWatch();\r\n   stopWatch.start();\r\n   ConfigurableApplicationContext context = null;\r\n   FailureAnalyzers analyzers = null;\r\n   configureHeadlessProperty();\r\n    \r\n   \/\/\u83b7\u53d6SpringApplicationRunListeners\uff1b\u4ece\u7c7b\u8def\u5f84\u4e0bMETA-INF\/spring.factories\r\n   SpringApplicationRunListeners listeners = getRunListeners(args);\r\n    \/\/\u56de\u8c03\u6240\u6709\u7684\u83b7\u53d6SpringApplicationRunListener.starting()\u65b9\u6cd5\r\n   listeners.starting();\r\n   try {\r\n       \/\/\u5c01\u88c5\u547d\u4ee4\u884c\u53c2\u6570\r\n      ApplicationArguments applicationArguments = new DefaultApplicationArguments(\r\n            args);\r\n      \/\/\u51c6\u5907\u73af\u5883\r\n      ConfigurableEnvironment environment = prepareEnvironment(listeners,\r\n            applicationArguments);\r\n       \t\t\/\/\u521b\u5efa\u73af\u5883\u5b8c\u6210\u540e\u56de\u8c03SpringApplicationRunListener.environmentPrepared()\uff1b\u8868\u793a\u73af\u5883\u51c6\u5907\u5b8c\u6210\r\n       \r\n      Banner printedBanner = printBanner(environment);\r\n       \r\n       \/\/\u521b\u5efaApplicationContext\uff1b\u51b3\u5b9a\u521b\u5efaweb\u7684ioc\u8fd8\u662f\u666e\u901a\u7684ioc\r\n      context = createApplicationContext();\r\n       \r\n      analyzers = new FailureAnalyzers(context);\r\n       \/\/\u51c6\u5907\u4e0a\u4e0b\u6587\u73af\u5883;\u5c06environment\u4fdd\u5b58\u5230ioc\u4e2d\uff1b\u800c\u4e14applyInitializers()\uff1b\r\n       \/\/applyInitializers()\uff1a\u56de\u8c03\u4e4b\u524d\u4fdd\u5b58\u7684\u6240\u6709\u7684ApplicationContextInitializer\u7684initialize\u65b9\u6cd5\r\n       \/\/\u56de\u8c03\u6240\u6709\u7684SpringApplicationRunListener\u7684contextPrepared()\uff1b\r\n       \/\/\r\n      prepareContext(context, environment, listeners, applicationArguments,\r\n            printedBanner);\r\n       \/\/prepareContext\u8fd0\u884c\u5b8c\u6210\u4ee5\u540e\u56de\u8c03\u6240\u6709\u7684SpringApplicationRunListener\u7684contextLoaded\uff08\uff09\uff1b\r\n       \r\n       \/\/s\u5237\u65b0\u5bb9\u5668\uff1bioc\u5bb9\u5668\u521d\u59cb\u5316\uff08\u5982\u679c\u662fweb\u5e94\u7528\u8fd8\u4f1a\u521b\u5efa\u5d4c\u5165\u5f0f\u7684Tomcat\uff09\uff1bSpring\u6ce8\u89e3\u7248\r\n       \/\/\u626b\u63cf\uff0c\u521b\u5efa\uff0c\u52a0\u8f7d\u6240\u6709\u7ec4\u4ef6\u7684\u5730\u65b9\uff1b\uff08\u914d\u7f6e\u7c7b\uff0c\u7ec4\u4ef6\uff0c\u81ea\u52a8\u914d\u7f6e\uff09\r\n      refreshContext(context);\r\n       \/\/\u4eceioc\u5bb9\u5668\u4e2d\u83b7\u53d6\u6240\u6709\u7684ApplicationRunner\u548cCommandLineRunner\u8fdb\u884c\u56de\u8c03\r\n       \/\/ApplicationRunner\u5148\u56de\u8c03\uff0cCommandLineRunner\u518d\u56de\u8c03\r\n      afterRefresh(context, applicationArguments);\r\n       \/\/\u6240\u6709\u7684SpringApplicationRunListener\u56de\u8c03finished\u65b9\u6cd5\r\n      listeners.finished(context, null);\r\n      stopWatch.stop();\r\n      if (this.logStartupInfo) {\r\n         new StartupInfoLogger(this.mainApplicationClass)\r\n               .logStarted(getApplicationLog(), stopWatch);\r\n      }\r\n       \/\/\u6574\u4e2aSpringBoot\u5e94\u7528\u542f\u52a8\u5b8c\u6210\u4ee5\u540e\u8fd4\u56de\u542f\u52a8\u7684ioc\u5bb9\u5668\uff1b\r\n      return context;\r\n   }\r\n   catch (Throwable ex) {\r\n      handleRunFailure(context, listeners, analyzers, ex);\r\n      throw new IllegalStateException(ex);\r\n   }\r\n}<\/pre>\n<p>\n\t\u6bcf\u4e00\u4e2a\u6b65\u9aa4\u7684\u6267\u884c\u90fd\u6709\u6ce8\u91ca\u3002\u719f\u6089\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u719f\u6089\u6574\u4e2a Spring boot \u7684\u542f\u52a8\u6d41\u7a0b\u540e\uff0c\u6211\u4eec\u81ea\u5b9a\u4e49 stater \u5c31\u53d8\u5f97\u5f88\u7b80\u5355\u4e86\u3002\u5173\u4e8e\u81ea\u5b9a\u4e49 stater \u6211\u4eec\u4ee5\u540e\u518d\u8bb2\uff01<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=2819\">Spring boot \u542f\u52a8\u914d\u7f6e\u539f\u7406<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>Spring boot \u7684\u542f\u52a8\u65b9\u6cd5\u8d70\u7684\u662f main \u65b9\u6cd5\uff0cmain \u65b9\u6cd5\u4e2d\u4e00\u822c\u8c03\u7528&nbsp;SpringAp [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[991,587,588,592,510],"class_list":["post-2819","post","type-post","status-publish","format-standard","hentry","category-java","tag-shiro-spring","tag-spring-boot","tag-spring-cloud","tag-springjunit4classrunner","tag-510"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2819","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2819"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2819\/revisions"}],"predecessor-version":[{"id":2820,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2819\/revisions\/2820"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2819"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}