{"id":1881,"date":"2019-06-09T17:16:39","date_gmt":"2019-06-09T09:16:39","guid":{"rendered":"https:\/\/coderbee.net\/?p=1881"},"modified":"2019-06-10T19:25:52","modified_gmt":"2019-06-10T11:25:52","slug":"springboot-%e5%90%af%e5%8a%a8%e5%88%86%e6%9e%90%e4%ba%8c-%e5%90%af%e5%8a%a8%e4%b8%bb%e6%b5%81%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/framework\/20190609\/1881","title":{"rendered":"SpringBoot \u542f\u52a8\u5206\u6790(\u4e8c)&#8211;\u542f\u52a8\u4e3b\u6d41\u7a0b"},"content":{"rendered":"<h1>1. initialize \u65b9\u6cd5<\/h1>\n<p><code>SpringApplication<\/code> \u7684\u6784\u9020\u51fd\u6570\u53ea\u8c03\u7528\u4e86 <code>initialize<\/code> \u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"java\">private void initialize(Object[] sources) {\n    if (sources != null &amp;&amp; sources.length &gt; 0) {\n        \/\/ \u628a\u5e94\u7528\u6307\u5b9a\u7684\u914d\u7f6e\u7c7b\u52a0\u5165\u914d\u7f6e\u626b\u63cf\u7684\u542f\u52a8\u6765\u6e90\n        this.sources.addAll(Arrays.asList(sources));\n    }\n\n    \/\/ \u5224\u65ad\u5e94\u7528\u662f\u5426\u662f web \u5e94\u7528\uff0c\u4e3b\u8981\u7528\u4e8e\u51b3\u5b9a\u91c7\u7528\u54ea\u79cd\u5177\u4f53\u7684\u4e0a\u4e0b\u6587\u5b9e\u73b0\n    this.webEnvironment = deduceWebEnvironment();\n\n    \/\/ \u5229\u7528\u5b9a\u5236\u7684 SPI \u5b9e\u73b0 SpringFactoriesLoader \u52a0\u8f7d ApplicationContextInitializer \u7684\u6240\u6709\u5b9e\u73b0\u5e76\u8bbe\u7f6e\u5230 initializers \u5c5e\u6027\n    setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));\n\n    \/\/ \u5229\u7528\u5b9a\u5236\u7684 SPI \u5b9e\u73b0 SpringFactoriesLoader \u52a0\u8f7d ApplicationListener \u7684\u6240\u6709\u5b9e\u73b0\u5e76\u8bbe\u7f6e\u5230 listeners \u5c5e\u6027\n    setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));\n\n    \/\/ \u627e\u51fa\u542f\u52a8\u7c7b\uff1a\u7ebf\u7a0b\u6808\u4e0a main \u65b9\u6cd5\u6240\u5904\u7684\u7c7b\n    this.mainApplicationClass = deduceMainApplicationClass();\n}\n<\/code><\/pre>\n<p>\u8be5\u65b9\u6cd5\u7684\u903b\u8f91\u4e3b\u8981\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u628a\u542f\u52a8\u7c7b\u52a0\u5165 sources \u5c5e\u6027\u3002<\/li>\n<li>\u5224\u65ad\u662f\u5426\u662f web \u5e94\u7528\u5e76\u8bbe\u7f6e\u5230 webEnvironment \u5c5e\u6027\u3002<\/li>\n<li>\u52a0\u8f7d\u6240\u6709\u7684 ApplicationContextInitializer \u5e76\u8bbe\u7f6e\u5230 initializers \u5c5e\u6027\u3002<\/li>\n<li>\u52a0\u8f7d\u6240\u6709\u7684 ApplicationListener \u5e76\u8bbe\u7f6e\u5230 listeners \u5c5e\u6027\u3002<\/li>\n<li>\u627e\u51fa main \u7c7b\u8bbe\u7f6e\u5230 mainApplicationClass\u3002<\/li>\n<\/ol>\n<h1>2. run \u65b9\u6cd5<\/h1>\n<p><!--more--><\/p>\n<pre><code class=\"java\">public ConfigurableApplicationContext run(String... args) {\n    \/\/ StopWatch \u4e3b\u8981\u7528\u6765\u7edf\u8ba1\u5e94\u7528\u7684\u542f\u52a8\u65f6\u95f4\n    StopWatch stopWatch = new StopWatch();\n    stopWatch.start();\n    ConfigurableApplicationContext context = null;\n    FailureAnalyzers analyzers = null;\n\n    \/\/ \u914d\u7f6e AWT \u7684 headless \u5c5e\u6027\n    configureHeadlessProperty();\n\n    \/\/ \u521d\u59cb\u5316\u4e8b\u4ef6\u5e7f\u64ad\u5668\uff0c\u7528\u4e8e\u5e7f\u64ad\u4e8b\u4ef6\n    SpringApplicationRunListeners listeners = getRunListeners(args);\n\n    \/\/ \u89e6\u53d1 SpringApplicationRunListener.starting \u65b9\u6cd5\n    \/\/ \u5411 ApplicationListener \u5e7f\u64ad ApplicationStartedEvent \u4e8b\u4ef6\n    listeners.starting();\n    try {\n        \/\/ \u5c01\u88c5\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u540e\u9762\u4f1a\u6ce8\u518c\u5230\u4e0a\u4e0b\u6587\u91cc\n        ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);\n\n        \/\/ \u521b\u5efa\u5e76\u51c6\u5907 Environment\n        ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);\n\n        \/\/ \u6253\u5370 Banner\n        Banner printedBanner = printBanner(environment);\n\n        \/\/ \u521b\u5efa ApplicationContext\n        context = createApplicationContext();\n\n        \/\/ \u521b\u5efa\u5931\u8d25\u5206\u6790\u5668\n        analyzers = new FailureAnalyzers(context);\n\n        \/\/ \n        prepareContext(context, environment, listeners, applicationArguments, printedBanner);\n\n        \/\/ \u5237\u65b0\u4e0a\u4e0b\u6587\uff0c\u52a0\u8f7d\u6240\u6709\u7684Bean\u5b9a\u4e49\u5e76\u5b9e\u4f8b\u5316\u975e\u5ef6\u8fdf\u52a0\u8f7d\u7684\u5355\u4f8bBean\n        refreshContext(context);\n\n        \/\/ \u83b7\u53d6\u6240\u6709\u7684 ApplicationRunner \u548c CommandLineRunner \u5e76\u6267\u884c\u5176\u56de\u8c03\n        afterRefresh(context, applicationArguments);\n\n        \/\/ \u5e7f\u64ad ApplicationReadyEvent\u6216ApplicationFailedEvent\u4e8b\u4ef6\n        listeners.finished(context, null);\n\n        \/\/ \u7ec8\u6b62\u8ba1\u65f6\n        stopWatch.stop();\n\n        if (this.logStartupInfo) {\n            new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);\n        }\n        return context;\n    } catch (Throwable ex) {\n        handleRunFailure(context, listeners, analyzers, ex);\n        throw new IllegalStateException(ex);\n    }\n}\n<\/code><\/pre>\n<p>run \u65b9\u6cd5\u7684\u6838\u5fc3\u903b\u8f91\uff1a<\/p>\n<ol>\n<li>\u521b\u5efa StopWatch \u7528\u4e8e\u8bb0\u5f55\u542f\u52a8\u8017\u65f6\uff1b<\/li>\n<li>\u521b\u5efa\u5e76\u51c6\u5907 Enviroment\u3002\u52a0\u8f7d\u6240\u6709\u7684\u914d\u7f6e\u6587\u4ef6\u548c\u5904\u7406 Profile \u3002<\/li>\n<li>\u521b\u5efa\u5e76\u51c6\u5907 ApplicationContext \uff0c\u628a <code>ConfigurationClassPostProcessor<\/code> \u6ce8\u518c\u5230\u4e0a\u4e0b\u6587\u5e95\u5c42\u7684 <code>BeanFacotry<\/code>\u3002<\/li>\n<li>\u5237\u65b0 ApplicationContext\u3002<\/li>\n<li>\u83b7\u53d6\u6240\u6709\u7684 <code>ApplicationRunner<\/code> \u548c <code>CommandLineRunner<\/code> \u5e76\u6267\u884c\u5176\u56de\u8c03\u3002<\/li>\n<li>\u5e7f\u64ad ApplicationReadyEvent\u6216ApplicationFailedEvent\u4e8b\u4ef6\u3002<\/li>\n<\/ol>\n<p>\u8fd9\u4e2a\u6d41\u7a0b\u975e\u5e38\u7b80\u5355\uff0c\u4e0d\u6d89\u53ca\u5177\u4f53\u7684\u914d\u7f6e\u6587\u4ef6\u89e3\u6790\u3001\u52a0\u8f7d\uff0cbean \u5b9a\u4e49\u626b\u63cf\u3001\u89e3\u6790\u3001\u52a0\u8f7d\u3001\u5b9e\u4f8b\u5316 bean \u7b49\u3002\u5c31\u662f SpringBoot \u7279\u6709\u7684\u6d41\u7a0b\uff0c\u6bd4\u5982\u6253\u5370 Banner\u3001\u56de\u8c03 Runner \u65b9\u6cd5\uff0c\u4e2d\u95f4\u9002\u65f6\u5e7f\u64ad\u4e8b\u4ef6\u3002<\/p>\n<hr \/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. initialize \u65b9\u6cd5 SpringApplication \u7684\u6784\u9020\u51fd\u6570 &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/framework\/20190609\/1881\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[255],"tags":[172,308],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1881"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=1881"}],"version-history":[{"count":2,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1881\/revisions"}],"predecessor-version":[{"id":1883,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1881\/revisions\/1883"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1881"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}