{"id":8443,"date":"2022-11-11T09:36:32","date_gmt":"2022-11-11T01:36:32","guid":{"rendered":"http:\/\/123.57.164.21\/?p=8443"},"modified":"2022-11-11T09:36:32","modified_gmt":"2022-11-11T01:36:32","slug":"commandlinerunner%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=8443","title":{"rendered":"CommandLineRunner\u4ecb\u7ecd"},"content":{"rendered":"\n<p>\u95ee\u9898\uff1a\u5728\u9879\u76ee\u542f\u52a8\u65f6\uff0c\u6211\u4eec\u9700\u8981\u52a0\u8f7d\u6216\u8005\u9884\u5148\u5b8c\u6210\u67d0\u4e9b\u52a8\u4f5c\uff0c\u8be5\u600e\u4e48\u529e\u5462\uff1f<br>\u89e3\u51b3\u529e\u6cd5\uff1a\u5728springBoot\u4e2d\u662f\u5b9e\u73b0CommandLineRunner\u63a5\u53e3\u7c7b<br>CommandLineRunner\u7ffb\u8bd1\u8fc7\u6765\u5c31\u662f\u201c\u547d\u4ee4\u884c\u8fd0\u884c\u8005\u201d\uff0c\u5f88\u5f62\u8c61\ud83d\ude0f<\/p>\n\n\n\n<p>\u9879\u76ee\u542f\u52a8\u4e4b\u524d\uff0c\u9884\u5148\u52a0\u8f7d\u6570\u636e\u3002\u6bd4\u5982\uff0c\u6743\u9650\u5bb9\u5668\u3001\u7279\u6b8a\u7528\u6237\u6570\u636e\u7b49\u3002\u901a\u5e38\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u76d1\u542c\u5668\u3001\u4e8b\u4ef6\u6765\u64cd\u4f5c\u3002\u4f46\u662f\uff0cspringboot\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u6b64\u7c7b\u9700\u6c42\uff0c\u5373\uff0cCommandLineRunner\u3002<\/p>\n\n\n\n<p>\u5148\u4e86\u89e3\u4e00\u4e0b\u8fd9\u4e2a\u7c7b<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import org.springframework.core.Ordered;\nimport org.springframework.core.annotation.Order;\n\n\/**\n * Interface used to indicate that a bean should &lt;em>run&lt;\/em> when it is contained within\n * a {@link SpringApplication}. Multiple {@link CommandLineRunner} beans can be defined\n * within the same application context and can be ordered using the {@link Ordered}\n * interface or {@link Order @Order} annotation.\n * &lt;p>\n * If you need access to {@link ApplicationArguments} instead of the raw String array\n * consider using {@link ApplicationRunner}.\n *\n * @author Dave Syer\n * @see ApplicationRunner\n *\/\n@FunctionalInterface\npublic interface CommandLineRunner {\n\n\t\/**\n\t * Callback used to run the bean.\n\t * @param args incoming main method arguments\n\t * @throws Exception on error\n\t *\/\n\tvoid run(String... args) throws Exception;\n\n}<\/pre>\n\n\n\n<p>\u6587\u6863\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u77e5\u9053\u4ee5\u4e0b\u51e0\u70b9\u3002<\/p>\n\n\n\n<ul><li>\u8fd9\u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u7528\u6237\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5b9e\u73b0\u8be5\u63a5\u53e3\uff0c\u5177\u4f53\u5b9e\u73b0run\u65b9\u6cd5<\/li><li>\u4efb\u4f55\u5728\u4e0a\u4e0b\u6587\u5bb9\u5668\u4e4b\u5185\u7684bean\u90fd\u53ef\u4ee5\u5b9e\u73b0run\u65b9\u6cd5<\/li><li>\u5982\u679c\u5728\u4e0a\u4e0b\u6587\u4e2d\uff0c\u5b58\u5728\u591a\u4e2a\u8be5\u63a5\u53e3\u5b9e\u73b0\u7c7b\uff0c\u53ef\u4ee5\u901a\u8fc7@order\u6ce8\u89e3\uff0c\u6307\u5b9a\u52a0\u8f7d\u987a\u5e8f<\/li><\/ul>\n\n\n\n<p>\u5229\u7528command-line runner\u7684\u8fd9\u4e2a\u7279\u6027\uff0c\u518d\u914d\u5408\u4f9d\u8d56\u6ce8\u5165\uff0c\u53ef\u4ee5\u5728\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\u540e\u9996\u5148\u5f15\u5165\u4e00\u4e9b\u4f9d\u8d56bean\uff0c\u4f8b\u5982data source\u3001rpc\u670d\u52a1\u6216\u8005\u5176\u4ed6\u6a21\u5757\u7b49\u7b49\uff0c\u8fd9\u4e9b\u5bf9\u8c61\u7684\u521d\u59cb\u5316\u53ef\u4ee5\u653e\u5728run\u65b9\u6cd5\u4e2d\u3002\u4e0d\u8fc7\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728run\u65b9\u6cd5\u4e2d\u6267\u884c\u521d\u59cb\u5316\u52a8\u4f5c\u7684\u65f6\u5019\u4e00\u65e6\u9047\u5230\u4efb\u4f55\u5f02\u5e38\uff0c\u90fd\u4f1a\u4f7f\u5f97\u5e94\u7528\u7a0b\u5e8f\u505c\u6b62\u8fd0\u884c\uff0c\u56e0\u6b64\u6700\u597d\u5229\u7528try\/catch\u8bed\u53e5\u5904\u7406\u53ef\u80fd\u9047\u5230\u7684\u5f02\u5e38\u3002<\/p>\n\n\n\n<p>\u4e3b\u8981\u6709\u4ee5\u4e0b\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<ul><li>(1)<code>@Component<\/code><\/li><li>(2)<code>@SpringBootApplication<\/code><\/li><li>(3)<code>\u58f0\u660e\u4e00\u4e2a\u5b9e\u73b0\u4e86CommandLineRunner\u63a5\u53e3\u7684Bean<\/code><\/li><\/ul>\n\n\n\n<p><strong>1. @Component\u5b9e\u73b0<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@SpringBootApplication\npublic class Demo03Application {\n\n    public static void main(String[] args) {\n        System.out.println(\"step 1: The Service will start\");\n        SpringApplication.run(Demo03Application.class, args);\n        System.out.println(\"step 5: The Service has started\");\n    }\n\n}\n@Component\n@Order(1)\nclass Runner1 implements CommandLineRunner {\n\n    @Override\n    public void run(String... args) throws Exception {\n        System.out.println(\"step 3: The Runner1 run...\");\n    }\n}\n\n@Component\n@Order(2)\nclass Runner2 implements CommandLineRunner {\n\n    @Override\n    public void run(String... args) throws Exception {\n        System.out.println(\"step 4: The Runner1 run...\");\n    }\n}\n<\/pre>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u56fe\u6240\u793a\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-161-1024x993.png\" alt=\"\" class=\"wp-image-8444\" width=\"463\" height=\"448\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-161-1024x993.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-161-300x291.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-161-768x745.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-161-830x805.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-161-230x223.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-161-350x340.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-161-480x466.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-161.png 1138w\" sizes=\"(max-width: 463px) 100vw, 463px\" \/><\/figure><\/div>\n\n\n\n<p>\u7531\u4ee5\u4e0a\u4ee3\u7801\u53ef\u4ee5\u53d1\u73b0\uff1a\u5728SpringApplication.run\u8fd0\u884c\u5b8c\u4e4b\u540e\u624d\u4f1a\u8fd0\u884c\u81ea\u5df1\u521b\u5efa\u7684\u5b9e\u73b0\u7c7b\uff0c\u5e76\u4e14\u81ea\u5df1\u7684\u5b9e\u73b0\u7c7b\u901a\u8fc7<code>@Order()<\/code>\u6ce8\u89e3\u63a7\u5236\u987a\u5e8f\uff0c\u6570\u5b57\u8d8a\u5c0f\u8d8a\u9760\u524d\u3002<\/p>\n\n\n\n<p>\u601d\u8003\u4e00\u4e0b\u4e3a\u4ec0\u4e48\u8981\u52a0@Component\u6ce8\u89e3\u5462\uff1f<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(1)\u3001\u52a0\u5165@Component\u6ce8\u89e3\u540e\uff0c\u5c31\u53ef\u4ee5\u5c06\u5bf9\u8c61\u4ea4\u7ed9spring\u7ba1\u7406\u3002\n(2)\u3001\u5f53Spring \u5bb9\u5668\u521d\u59cb\u5316\u5b8c\u6210\u540e, Spring\u4f1a\u904d\u5386\u6240\u6709\u5b9e\u73b0CommandLineRunner\u63a5\u53e3\u7684\u7c7b, \u5e76\u8fd0\u884c\u5176run() \u65b9\u6cd5.<\/pre>\n\n\n\n<p><strong>2. @SpringBootApplication\u5b9e\u73b0<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5177\u4f53\u4ee3\u7801\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@SpringBootApplication\npublic class Demo03Application implements CommandLineRunner{\n\n    public static void main(String[] args) {\n        System.out.println(\"step 1: The Service will start\");\n        SpringApplication.run(Demo03Application.class, args);\n        System.out.println(\"step 5: The Service has started\");\n    }\n\n    @Override\n    public void run(String... args) throws Exception {\n        System.out.println(\"app start\");\n    }\n}\n<\/pre>\n\n\n\n<p><strong>3. @Bean\u5b9e\u73b0<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@SpringBootApplication\npublic class SpringBootWebApplication extends SpringBootServletInitializer {\n\n    public static void main(String[] args) throws Exception {\n        SpringApplication.run(SpringBootWebApplication.class, args);\n    }\n \n    @Bean\n    public ApplicationStartupRunner schedulerRunner() {\n        return new ApplicationStartupRunner();\n    }\n}\npublic class ApplicationStartupRunner implements CommandLineRunner {\n    protected final Log logger = LogFactory.getLog(getClass());\n    @Override\n    public void run(String... args) throws Exception {\n        logger.info(\"Application Started !!\");\n    }\n}\n<\/pre>\n\n\n\n<p><strong>run(String\u2026 args)\u65b9\u6cd5\u4e2d\u7684args\u53c2\u6570\u662f\u4ec0\u4e48<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>String&#8230; args\u662f\u5e94\u7528\u542f\u52a8\u7684\u65f6\u5019\u53ef\u4ee5\u4f20\u8fdb\u6765\u7684\u53c2\u6570\uff0c\u6709\u4e24\u79cd\u65b9\u5f0f\u53ef\u4ee5\u4f20\u53c2<\/p>\n\n\n\n<ul><li>\u4e00\u79cd\u662f\u547d\u4ee4\u884c\u7684\u65b9\u5f0f\u4f20\u53c2\uff0c\u6240\u4ee5\u4e3a\u4ec0\u4e48\u8fd9\u4e2a\u63a5\u53e3\u53ebCommandLineRunner<\/li><li>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u901a\u8fc7IntelliJ IDEA\u914d\u7f6e\u53c2\u6570<\/li><\/ul>\n\n\n\n<p>\u4e0b\u9762\u5206\u522b\u8bf4\u660e<\/p>\n\n\n\n<p><strong>\u547d\u4ee4\u884c\u4f20\u53c2<\/strong><\/p>\n\n\n\n<p>\u9996\u5148\u5c06\u5e94\u7528\u6253\u6210jar\u5305\uff0c\u7136\u540e\u8fd0\u884c\u5982\u4e0b\u547d\u4ee4\u884c\uff0c\u6211\u8fd9\u91cc\u4f20\u5165\u4e09\u4e2a\u53c2\u6570<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">java -jar MyProject.jar \u91ce\u733f\u65b0\u4e00 \u91ce\u733f\u65b0\u4e8c \u91ce\u733f\u65b0\u4e09<\/pre>\n\n\n\n<p><strong>IntelliJ IDEA\u4f20\u53c2<\/strong><\/p>\n\n\n\n<p>\u5982\u679c\u662f\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u60f3\u901a\u8fc7IntelliJ IDEA\u76f4\u63a5\u8fd0\u884c\u9879\u76ee\uff0c\u4e0d\u60f3\u6253\u6210jar\u5305\uff0c\u53c8\u8981\u4f20\u5165\u53c2\u6570\uff0c\u53ef\u4ee5\u914d\u7f6e\u9879\u76ee\u8fd0\u884c\u7684\u73af\u5883<\/p>\n\n\n\n<p>1.\u70b9\u51fbEdit Configurations&#8230;\u6253\u5f00\u9879\u76ee\u8fd0\u884c\u914d\u7f6e\u5bf9\u8bdd\u6846<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-162.png\" alt=\"\" class=\"wp-image-8446\" width=\"339\" height=\"88\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-162.png 560w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-162-300x78.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-162-230x60.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-162-350x91.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-162-480x125.png 480w\" sizes=\"(max-width: 339px) 100vw, 339px\" \/><\/figure><\/div>\n\n\n\n<p>2.\u5c55\u5f00Environment\uff0c\u5728Program arguments\u9879\u4e2d\u586b\u5165\u9879\u76ee\u8fd0\u884c\u7684\u53c2\u6570\uff0c\u70b9\u51fbOK\u6309\u94ae\u786e\u5b9a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-163-1024x946.png\" alt=\"\" class=\"wp-image-8447\" width=\"551\" height=\"509\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-163-1024x946.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-163-300x277.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-163-768x710.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-163-830x767.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-163-230x213.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-163-350x323.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-163-480x444.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-163.png 1290w\" sizes=\"(max-width: 551px) 100vw, 551px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u6d4b\u8bd5<\/strong><\/p>\n\n\n\n<p>\u6211\u4eec\u5c06\u4e0a\u9762\u7684\u5b9e\u4f8b\u7a0d\u5fae\u4fee\u6539\u4e0b\uff0c\u628a\u53c2\u6570args\u6253\u5370\u51fa\u6765<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Component\n@Order(value = 1) \/\/ \u6307\u5b9a\u5176\u6267\u884c\u987a\u5e8f\uff0c\u503c\u8d8a\u5c0f\u4f18\u5148\u7ea7\u8d8a\u9ad8\npublic class MyRunner1 implements CommandLineRunner {\n    @Override\n    public void run(String... args) throws Exception {\n        System.out.println(\"MyRunner1:\" + Arrays.toString(args));\n    }\n}<\/pre>\n\n\n\n<p>\u91c7\u7528\u4ee5\u4e0a\u547d\u4ee4\u884c\u7684\u65b9\u5f0f\u6216\u8005IntelliJ IDEA\u914d\u7f6e\u53c2\u6570\u7684\u65b9\u5f0f\u8fd0\u884c\u7ed3\u679c\u4e00\u6837\uff0c\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">2020-08-21 16:36:04.453 custom-logback  INFO 16244 --- [           main] com.yeyuanxinyi.MyApplication      : Started MyApplication in 10.724 seconds (JVM running for 13.727)\nMyRunner1:[\u91ce\u733f\u65b0\u4e00, \u91ce\u733f\u65b0\u4e8c, \u91ce\u733f\u65b0\u4e09]<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u9898\uff1a\u5728\u9879\u76ee\u542f\u52a8\u65f6\uff0c\u6211\u4eec\u9700\u8981\u52a0\u8f7d\u6216\u8005\u9884\u5148\u5b8c\u6210\u67d0\u4e9b\u52a8\u4f5c\uff0c\u8be5\u600e\u4e48\u529e\u5462\uff1f\u89e3\u51b3\u529e\u6cd5\uff1a\u5728springBoot\u4e2d\u662f\u5b9e\u73b0Co [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,27],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/8443"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8443"}],"version-history":[{"count":2,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/8443\/revisions"}],"predecessor-version":[{"id":8448,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/8443\/revisions\/8448"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}