{"id":7579,"date":"2023-11-28T19:39:54","date_gmt":"2023-11-28T11:39:54","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=7579"},"modified":"2023-12-05T15:35:58","modified_gmt":"2023-12-05T07:35:58","slug":"springboot-qi-dong-de-shi-hou-yun-xing-yi-xie-te-d","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/springboot-qi-dong-de-shi-hou-yun-xing-yi-xie-te-d\/","title":{"rendered":"SpringBoot\u542f\u52a8\u7684\u65f6\u5019\u8fd0\u884c\u4e00\u4e9b\u7279\u5b9a\u7684\u4ee3\u7801\uff1f"},"content":{"rendered":"<p>\u662f\u7684\uff0cSpring Boot \u63d0\u4f9b\u4e86\u4e00\u79cd\u673a\u5236\uff0c\u5141\u8bb8\u5728\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\u6267\u884c\u4e00\u4e9b\u7279\u5b9a\u7684\u4ee3\u7801\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0 <code>ApplicationRunner<\/code> \u6216 <code>CommandLineRunner<\/code> \u63a5\u53e3\u6765\u5b9e\u73b0\u3002\u8fd9\u4e24\u4e2a\u63a5\u53e3\u5747\u63d0\u4f9b\u4e86\u5728 Spring Boot \u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\u6267\u884c\u7279\u5b9a\u4ee3\u7801\u7684\u65b9\u6cd5\u3002<\/p>\n<ol>\n<li>\n<strong><code>ApplicationRunner<\/code> \u63a5\u53e3\uff1a<\/strong> \u5b9e\u73b0 <code>ApplicationRunner<\/code> \u63a5\u53e3\u7684\u7c7b\u9700\u8981\u5b9e\u73b0 <code>run<\/code> \u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u5728 <code>SpringApplication<\/code> \u542f\u52a8\u540e\u88ab\u8c03\u7528\u3002<code>ApplicationRunner<\/code> \u63a5\u53e3\u5141\u8bb8\u4f60\u8bbf\u95ee\u5e94\u7528\u7a0b\u5e8f\u7684 <code>ApplicationArguments<\/code> \u53c2\u6570\uff0c\u53ef\u4ee5\u5728\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\u6267\u884c\u4e00\u4e9b\u903b\u8f91\u3002<\/p>\n<pre><code class=\"language-java\">import org.springframework.boot.ApplicationArguments;\nimport org.springframework.boot.ApplicationRunner;\nimport org.springframework.stereotype.Component;\n\n@Component\npublic class MyApplicationRunner implements ApplicationRunner {\n\n    @Override\n    public void run(ApplicationArguments args) throws Exception {\n        System.out.println(&quot;Executing code in MyApplicationRunner&quot;);\n        \/\/ Your code here...\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong><code>CommandLineRunner<\/code> \u63a5\u53e3\uff1a<\/strong> \u5b9e\u73b0 <code>CommandLineRunner<\/code> \u63a5\u53e3\u7684\u7c7b\u9700\u8981\u5b9e\u73b0 <code>run<\/code> \u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u5728 <code>SpringApplication<\/code> \u542f\u52a8\u540e\u88ab\u8c03\u7528\u3002\u4e0e <code>ApplicationRunner<\/code> \u4e0d\u540c\uff0c<code>CommandLineRunner<\/code> \u63a5\u53e3\u5141\u8bb8\u4f60\u8bbf\u95ee\u539f\u59cb\u7684\u547d\u4ee4\u884c\u53c2\u6570\u3002<\/p>\n<pre><code class=\"language-java\">import org.springframework.boot.CommandLineRunner;\nimport org.springframework.stereotype.Component;\n\n@Component\npublic class MyCommandLineRunner implements CommandLineRunner {\n\n    @Override\n    public void run(String... args) throws Exception {\n        System.out.println(&quot;Executing code in MyCommandLineRunner&quot;);\n        \/\/ Your code here...\n    }\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u65e0\u8bba\u9009\u62e9\u54ea\u4e2a\u63a5\u53e3\uff0cSpring Boot \u5728\u542f\u52a8\u65f6\u90fd\u4f1a\u81ea\u52a8\u68c0\u6d4b\u5e76\u6267\u884c\u76f8\u5e94\u7684\u4ee3\u7801\u3002\u8fd9\u4e9b\u63a5\u53e3\u7684\u5b9e\u73b0\u7c7b\u901a\u5e38\u88ab\u6ce8\u89e3\u4e3a <code>@Component<\/code> \u6216\u8005\u88ab\u6ce8\u518c\u5230 Spring \u7684\u4e0a\u4e0b\u6587\u4e2d\uff0c\u4ee5\u786e\u4fdd\u5b83\u4eec\u5728\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\u88ab\u6b63\u786e\u8c03\u7528\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e24\u4e2a\u63a5\u53e3\u6765\u6267\u884c\u4e00\u4e9b\u521d\u59cb\u5316\u903b\u8f91\u3001\u52a0\u8f7d\u6570\u636e\u3001\u8fde\u63a5\u5230\u5916\u90e8\u670d\u52a1\u7b49\u64cd\u4f5c\u3002\u8fd9\u79cd\u673a\u5236\u4f7f\u5f97\u5728\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\u6267\u884c\u7279\u5b9a\u4ee3\u7801\u53d8\u5f97\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u662f\u7684\uff0cSpring Boot \u63d0\u4f9b\u4e86\u4e00\u79cd\u673a\u5236\uff0c\u5141\u8bb8\u5728\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\u6267\u884c\u4e00\u4e9b\u7279\u5b9a\u7684\u4ee3\u7801\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0 Appl [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[],"class_list":["post-7579","post","type-post","status-publish","format-standard","hentry","category-springboot"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7579","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=7579"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7579\/revisions"}],"predecessor-version":[{"id":16523,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7579\/revisions\/16523"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=7579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=7579"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=7579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}