{"id":868,"date":"2023-03-09T21:37:13","date_gmt":"2023-03-09T13:37:13","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=868"},"modified":"2023-04-29T16:44:28","modified_gmt":"2023-04-29T08:44:28","slug":"spring-boot-initialize-resources-when-project-starts","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/09\/spring-boot-initialize-resources-when-project-starts\/","title":{"rendered":"Spring Boot\u9879\u76ee\u542f\u52a8\u65f6\u521d\u59cb\u5316\u8d44\u6e90"},"content":{"rendered":"<p>\u5728\u6211\u4eec\u5b9e\u9645\u5de5\u4f5c\u4e2d\uff0c\u603b\u4f1a\u9047\u5230\u8fd9\u6837\u9700\u6c42\uff0c\u5728\u9879\u76ee\u542f\u52a8\u7684\u65f6\u5019\u9700\u8981\u505a\u4e00\u4e9b\u521d\u59cb\u5316\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u521d\u59cb\u5316\u7ebf\u7a0b\u6c60\uff0c\u63d0\u524d\u52a0\u8f7d\u597d\u52a0\u5bc6\u8bc1\u4e66\u7b49\u3002<code>CommandLineRunner<\/code>\u5c31\u662f\u4e13\u95e8\u89e3\u51b3\u9879\u76ee\u542f\u52a8\u521d\u59cb\u5316\u8d44\u6e90\u64cd\u4f5c\u3002<\/p>\n<h2>CommandLineRunner\u57fa\u672c\u4f7f\u7528<\/h2>\n<p><!-- more --><\/p>\n<p><code>CommandLineRunner<\/code>\u63a5\u53e3\u7684<code>Component<\/code>\u4f1a\u5728\u6240\u6709<code>Spring Beans<\/code>\u90fd\u521d\u59cb\u5316\u4e4b\u540e\uff0c<code>SpringApplication.run()<\/code>\u4e4b\u524d\u6267\u884c\uff0c\u975e\u5e38\u9002\u5408\u5728\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u4e4b\u521d\u8fdb\u884c\u4e00\u4e9b\u6570\u636e\u521d\u59cb\u5316\u7684\u5de5\u4f5c\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u8fd0\u7528\u6848\u4f8b\u6d4b\u8bd5\u5b83\u5982\u4f55\u4f7f\u7528\uff0c\u5728\u6d4b\u8bd5\u4e4b\u524d\u5728\u542f\u52a8\u7c7b\u52a0\u4e24\u884c\u6253\u5370\u63d0\u793a\uff0c\u65b9\u4fbf\u6211\u4eec\u8bc6\u522b<code>CommandLineRunner<\/code>\u7684\u6267\u884c\u65f6\u673a\u3002<\/p>\n<pre><code class=\"language-java\">@SpringBootApplication\npublic class CommandLineRunnerApplication {\n    public static void main(String[] args) {\n        System.out.println(&quot;The service to start.&quot;);\n        SpringApplication.run(CommandLineRunnerApplication.class, args);\n        System.out.println(&quot;The service has started.&quot;);\n    }\n}<\/code><\/pre>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u76f4\u63a5\u521b\u5efa\u4e00\u4e2a\u7c7b\u7ee7\u627f<code>CommandLineRunner<\/code>\uff0c\u5e76\u5b9e\u73b0\u5b83\u7684<code>run()<\/code>\u65b9\u6cd5<\/p>\n<pre><code class=\"language-java\">@Component\npublic class Runner implements CommandLineRunner {\n    @Override\n    public void run(String... args) throws Exception {\n        System.out.println(&quot;The Runner start to initialize ...&quot;);\n    }\n}<\/code><\/pre>\n<p>\u5728<code>run()<\/code>\u65b9\u6cd5\u4e2d\u6253\u5370\u4e86\u4e00\u4e9b\u53c2\u6570\u6765\u770b\u51fa\u5b83\u7684\u6267\u884c\u65f6\u673a\u3002\u5b8c\u6210\u4e4b\u540e\u542f\u52a8\u9879\u76ee\u8fdb\u884c\u6d4b\u8bd5\uff1a<\/p>\n<pre><code>The service to start.\n\n  .   ____          _            __ _ _\n \/\\\\ \/ ___&#039;_ __ _ _(_)_ __  __ _ \\ \\ \\ \\\n( ( )\\___ | &#039;_ | &#039;_| | &#039;_ \\\/ _` | \\ \\ \\ \\\n \\\\\/  ___)| |_)| | | | | || (_| |  ) ) ) )\n  &#039;  |____| .__|_| |_|_| |_\\__, | \/ \/ \/ \/\n =========|_|==============|___\/=\/_\/_\/_\/\n :: Spring Boot ::        (v2.1.3.RELEASE)\n\n...\n\nTomcat started on port(s): 8080 (http) with context path &#039;&#039;\nStarted CommandLineRunnerApplication in 2.952 seconds (JVM running for 4.556)\nThe Runner start to initialize ...\nThe service has started.<\/code><\/pre>\n<p>\u6839\u636e\u63a7\u5236\u53f0\u7684\u6253\u5370\u4fe1\u606f\u6211\u4eec\u53ef\u4ee5\u770b\u51fa<code>CommandLineRunner<\/code>\u4e2d\u7684\u65b9\u6cd5\u4f1a\u5728 Spring Boot \u5bb9\u5668\u52a0\u8f7d\u4e4b\u540e\u6267\u884c\uff0c\u6267\u884c\u5b8c\u6210\u540e\u9879\u76ee\u542f\u52a8\u5b8c\u6210\u3002<\/p>\n<h2>CommandLineRunner\u6267\u884c\u987a\u5e8f<\/h2>\n<p>\u5982\u679c\u6211\u4eec\u5728\u542f\u52a8\u5bb9\u5668\u7684\u65f6\u5019\u9700\u8981\u521d\u59cb\u5316\u5f88\u591a\u8d44\u6e90\uff0c\u5e76\u4e14\u521d\u59cb\u5316\u8d44\u6e90\u76f8\u4e92\u4e4b\u95f4\u6709\u5e8f\uff0c\u90a3\u5982\u4f55\u4fdd\u8bc1\u4e0d\u540c\u7684<code>CommandLineRunner<\/code>\u7684\u6267\u884c\u987a\u5e8f\u5462\uff1fSpring Boot \u4e5f\u7ed9\u51fa\u4e86\u89e3\u51b3\u65b9\u6848\u3002\u90a3\u5c31\u662f\u4f7f\u7528<code>@Order<\/code>\u6ce8\u89e3\u3002<\/p>\n<p>\u6211\u4eec\u521b\u5efa\u4e24\u4e2a<code>CommandLineRunner<\/code>\u7684\u5b9e\u73b0\u7c7b\u6765\u8fdb\u884c\u6d4b\u8bd5\uff1a<\/p>\n<p>\u7b2c\u4e00\u4e2a\u5b9e\u73b0\u7c7b\uff1a<\/p>\n<pre><code class=\"language-java\">@Component\n@Order(1)\npublic class OrderRunner1 implements CommandLineRunner {\n    @Override\n    public void run(String... args) throws Exception {\n        System.out.println(&quot;The OrderRunner1 start to initialize ...&quot;);\n    }\n}<\/code><\/pre>\n<p>\u7b2c\u4e8c\u4e2a\u5b9e\u73b0\u7c7b\uff1a<\/p>\n<pre><code class=\"language-java\">@Component\n@Order(2)\npublic class OrderRunner2 implements CommandLineRunner {\n    @Override\n    public void run(String... args) throws Exception {\n        System.out.println(&quot;The OrderRunner2 start to initialize ...&quot;);\n    }\n}<\/code><\/pre>\n<p>\u6dfb\u52a0\u5b8c\u6210\u4e4b\u540e\u91cd\u65b0\u542f\u52a8\uff0c\u89c2\u5bdf\u6267\u884c\u987a\u5e8f\uff1a<\/p>\n<pre><code>The service to start.\n\n  .   ____          _            __ _ _\n \/\\\\ \/ ___&#039;_ __ _ _(_)_ __  __ _ \\ \\ \\ \\\n( ( )\\___ | &#039;_ | &#039;_| | &#039;_ \\\/ _` | \\ \\ \\ \\\n \\\\\/  ___)| |_)| | | | | || (_| |  ) ) ) )\n  &#039;  |____| .__|_| |_|_| |_\\__, | \/ \/ \/ \/\n =========|_|==============|___\/=\/_\/_\/_\/\n :: Spring Boot ::        (v2.1.3.RELEASE)\n\nTomcat started on port(s): 8080 (http) with context path &#039;&#039;\nStarted CommandLineRunnerApplication in 3.119 seconds (JVM running for 4.554)\nThe OrderRunner1 start to initialize ...\nThe OrderRunner2 start to initialize ...\nThe Runner start to initialize ...\nThe service has started.<\/code><\/pre>\n<p>\u901a\u8fc7\u63a7\u5236\u53f0\u7684\u8f93\u51fa\u6211\u4eec\u53d1\u73b0\uff0c\u6dfb\u52a0<code>@Order<\/code>\u6ce8\u89e3\u7684\u5b9e\u73b0\u7c7b\u6700\u5148\u6267\u884c\uff0c\u5e76\u4e14<code>@Order()<\/code>\u91cc\u9762\u7684\u503c\u8d8a\u5c0f\u542f\u52a8\u8d8a\u65e9\u3002<\/p>\n<p>\u5728\u5b9e\u8df5\u4e2d\uff0c\u4f7f\u7528<code>ApplicationRunner<\/code>\u4e5f\u53ef\u4ee5\u8fbe\u5230\u76f8\u540c\u7684\u76ee\u7684\uff0c\u4e24\u7740\u5dee\u522b\u4e0d\u5927\u3002\u770b\u6765\u4f7f\u7528 Spring Boot \u89e3\u51b3\u521d\u59cb\u5316\u8d44\u6e90\u7684\u95ee\u9898\u975e\u5e38\u7b80\u5355\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u6211\u4eec\u5b9e\u9645\u5de5\u4f5c\u4e2d\uff0c\u603b\u4f1a\u9047\u5230\u8fd9\u6837\u9700\u6c42\uff0c\u5728\u9879\u76ee\u542f\u52a8\u7684\u65f6\u5019\u9700\u8981\u505a\u4e00\u4e9b\u521d\u59cb\u5316\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u521d\u59cb\u5316\u7ebf\u7a0b\u6c60\uff0c\u63d0\u524d\u52a0\u8f7d\u597d\u52a0\u5bc6\u8bc1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-868","post","type-post","status-publish","format-standard","hentry","category-spring-boot"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/868","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=868"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/868\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=868"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=868"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=868"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}