{"id":9022,"date":"2023-11-28T20:51:04","date_gmt":"2023-11-28T12:51:04","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=9022"},"modified":"2023-12-06T16:16:19","modified_gmt":"2023-12-06T08:16:19","slug":"springboot-qi-dong-de-shi-hou-yun-xing-yi-xie-te-d-2","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-2\/","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\u5728\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\u8fd0\u884c\u7279\u5b9a\u4ee3\u7801\u7684\u673a\u5236\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>CommandLineRunner<\/code>\u6216<code>ApplicationRunner<\/code>\u63a5\u53e3\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7<code>@PostConstruct<\/code>\u6ce8\u89e3\u5728bean\u521d\u59cb\u5316\u65f6\u6267\u884c\u7279\u5b9a\u7684\u4ee3\u7801\u3002\u4e0b\u9762\u5206\u522b\u4ecb\u7ecd\u8fd9\u4e09\u79cd\u65b9\u5f0f\uff1a<\/p>\n<h3><a id=\"1-commandlinerunner%E5%92%8C-applicationrunner%E6%8E%A5%E5%8F%A3%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>1. CommandLineRunner \u548c ApplicationRunner \u63a5\u53e3\uff1a<\/h3>\n<p>\u8fd9\u4e24\u4e2a\u63a5\u53e3\u63d0\u4f9b\u4e86\u4e00\u4e2a<code>run<\/code>\u65b9\u6cd5\uff0c\u5b83\u5728Spring Boot\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u540e\u7acb\u5373\u6267\u884c\u3002\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5b9e\u73b0\u8fd9\u4e9b\u63a5\u53e3\u7684Bean\uff0c\u5e76\u5728\u5176\u4e2d\u7f16\u5199\u542f\u52a8\u65f6\u9700\u8981\u6267\u884c\u7684\u4ee3\u7801\u3002<\/p>\n<h4><a id=\"%E4%BD%BF%E7%94%A8commandlinerunner%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f7f\u7528 CommandLineRunner\uff1a<\/h4>\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        \/\/ \u5728\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\u6267\u884c\u7684\u4ee3\u7801\n        System.out.println(&quot;CommandLineRunner executed!&quot;);\n    }\n}\n<\/code><\/pre>\n<h4><a id=\"%E4%BD%BF%E7%94%A8applicationrunner%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f7f\u7528 ApplicationRunner\uff1a<\/h4>\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        \/\/ \u5728\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\u6267\u884c\u7684\u4ee3\u7801\n        System.out.println(&quot;ApplicationRunner executed!&quot;);\n    }\n}\n<\/code><\/pre>\n<h3><a id=\"2-postconstruct%E6%B3%A8%E8%A7%A3%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>2. @PostConstruct \u6ce8\u89e3\uff1a<\/h3>\n<p>\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>@PostConstruct<\/code>\u6ce8\u89e3\u6765\u6807\u8bb0\u4e00\u4e2a\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u4f1a\u5728Bean\u521d\u59cb\u5316\u65f6\u6267\u884c\u3002\u8fd9\u662f\u4e00\u4e2a\u66f4\u4f20\u7edf\u7684\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u975eCommandLineRunner\u6216ApplicationRunner\u7684\u573a\u666f\u3002<\/p>\n<pre><code class=\"language-java\">import org.springframework.stereotype.Component;\nimport javax.annotation.PostConstruct;\n\n@Component\npublic class MyPostConstructBean {\n\n    @PostConstruct\n    public void init() {\n        \/\/ \u5728\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\u6267\u884c\u7684\u4ee3\u7801\n        System.out.println(&quot;@PostConstruct executed!&quot;);\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e09\u79cd\u65b9\u5f0f\u4e2d\uff0c\u9009\u62e9\u9002\u5408\u4f60\u9700\u6c42\u7684\u65b9\u5f0f\u3002\u901a\u5e38\uff0c\u5982\u679c\u4f60\u7684\u4ee3\u7801\u9700\u8981\u8bbf\u95ee\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528CommandLineRunner\u6216ApplicationRunner\u3002\u5982\u679c\u53ea\u662f\u5728Bean\u521d\u59cb\u5316\u65f6\u6267\u884c\u4e00\u4e9b\u4ee3\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528@PostConstruct\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u662f\u7684\uff0cSpring Boot\u63d0\u4f9b\u4e86\u4e00\u79cd\u5728\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\u8fd0\u884c\u7279\u5b9a\u4ee3\u7801\u7684\u673a\u5236\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528CommandLineRu [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[117],"tags":[],"class_list":["post-9022","post","type-post","status-publish","format-standard","hentry","category-springcloud"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9022","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=9022"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9022\/revisions"}],"predecessor-version":[{"id":16683,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9022\/revisions\/16683"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=9022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=9022"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=9022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}