{"id":8663,"date":"2023-11-28T20:34:05","date_gmt":"2023-11-28T12:34:05","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=8663"},"modified":"2023-12-06T10:57:48","modified_gmt":"2023-12-06T02:57:48","slug":"springboot-zhong-qi-dong-de-shi-hou-yun-xing-yi-xi","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/springboot-zhong-qi-dong-de-shi-hou-yun-xing-yi-xi\/","title":{"rendered":"SpringBoot\u4e2d\u542f\u52a8\u7684\u65f6\u5019\u8fd0\u884c\u4e00\u4e9b\u7279\u5b9a\u7684\u4ee3\u7801\uff1f"},"content":{"rendered":"<p>\u5728 Spring Boot \u4e2d\uff0c\u4f60\u53ef\u4ee5\u5728\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\u6267\u884c\u4e00\u4e9b\u7279\u5b9a\u7684\u4ee3\u7801\u3002\u6709\u51e0\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e2a\u76ee\u6807\uff1a<\/p>\n<ol>\n<li>\n<strong>\u4f7f\u7528 <code>CommandLineRunner<\/code> \u6216 <code>ApplicationRunner<\/code> \u63a5\u53e3\uff1a<\/strong><\/p>\n<ul>\n<li>\u5b9e\u73b0 <code>CommandLineRunner<\/code> \u6216 <code>ApplicationRunner<\/code> \u63a5\u53e3\u7684\u7c7b\u53ef\u4ee5\u5728 Spring Boot \u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\u6267\u884c\u7279\u5b9a\u7684\u903b\u8f91\u3002\u8fd9\u4e24\u4e2a\u63a5\u53e3\u90fd\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>run<\/code> \u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u4f1a\u5728\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u540e\u88ab\u8c03\u7528\u3002<\/li>\n<\/ul>\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\u540e\u6267\u884c\u7684\u4ee3\u7801\n        System.out.println(&quot;Application started. Executing CommandLineRunner...&quot;);\n    }\n}\n<\/code><\/pre>\n<pre><code class=\"language-java\">import 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\u540e\u6267\u884c\u7684\u4ee3\u7801\n        System.out.println(&quot;Application started. Executing ApplicationRunner...&quot;);\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u4f7f\u7528 <code>@PostConstruct<\/code> \u6ce8\u89e3\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f60\u53ef\u4ee5\u5728\u4efb\u4f55\u5e26\u6709 <code>@Service<\/code>\u3001<code>@Component<\/code> \u6216\u5176\u4ed6 Spring \u5bb9\u5668\u7ba1\u7406\u6ce8\u89e3\u7684\u7c7b\u4e2d\u4f7f\u7528 <code>@PostConstruct<\/code> \u6ce8\u89e3\uff0c\u6807\u8bb0\u4e00\u4e2a\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u5c06\u5728\u8be5 Bean \u521d\u59cb\u5316\u5b8c\u6210\u540e\u8c03\u7528\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">import org.springframework.stereotype.Component;\n\n@Component\npublic class MyPostConstructBean {\n\n    @PostConstruct\n    public void postConstruct() {\n        \/\/ \u5728\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u540e\u6267\u884c\u7684\u4ee3\u7801\n        System.out.println(&quot;Application started. Executing @PostConstruct method...&quot;);\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u4f7f\u7528 <code>ApplicationListener<\/code> \u63a5\u53e3\uff1a<\/strong><\/p>\n<ul>\n<li>\u5b9e\u73b0 <code>ApplicationListener<\/code> \u63a5\u53e3\uff0c\u76d1\u542c <code>ApplicationReadyEvent<\/code> \u4e8b\u4ef6\uff0c\u8be5\u4e8b\u4ef6\u8868\u793a\u5e94\u7528\u7a0b\u5e8f\u5df2\u51c6\u5907\u597d\u670d\u52a1\u8bf7\u6c42\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">import org.springframework.boot.context.event.ApplicationReadyEvent;\nimport org.springframework.context.ApplicationListener;\nimport org.springframework.stereotype.Component;\n\n@Component\npublic class MyApplicationReadyListener implements ApplicationListener&lt;ApplicationReadyEvent&gt; {\n\n    @Override\n    public void onApplicationEvent(ApplicationReadyEvent event) {\n        \/\/ \u5728\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u540e\u6267\u884c\u7684\u4ee3\u7801\n        System.out.println(&quot;Application started. Executing ApplicationListener...&quot;);\n    }\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u5f0f\uff0c\u90fd\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u5728\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\u6267\u884c\u7279\u5b9a\u7684\u4ee3\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Spring Boot \u4e2d\uff0c\u4f60\u53ef\u4ee5\u5728\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\u6267\u884c\u4e00\u4e9b\u7279\u5b9a\u7684\u4ee3\u7801\u3002\u6709\u51e0\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e2a\u76ee\u6807\uff1a \u4f7f\u7528  [&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-8663","post","type-post","status-publish","format-standard","hentry","category-springboot"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8663","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=8663"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8663\/revisions"}],"predecessor-version":[{"id":16649,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8663\/revisions\/16649"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=8663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=8663"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=8663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}