{"id":8537,"date":"2023-11-28T20:30:48","date_gmt":"2023-11-28T12:30:48","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=8537"},"modified":"2023-12-06T10:42:44","modified_gmt":"2023-12-06T02:42:44","slug":"springboot-jian-ting-qi-liu-cheng","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/springboot-jian-ting-qi-liu-cheng\/","title":{"rendered":"SpringBoot\u76d1\u542c\u5668\u6d41\u7a0b\uff1f"},"content":{"rendered":"<p>\u5728 Spring Boot \u4e2d\uff0c\u901a\u8fc7\u5b9e\u73b0 <code>ApplicationListener<\/code> \u63a5\u53e3\u6216\u4f7f\u7528 <code>@EventListener<\/code> \u6ce8\u89e3\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u76d1\u542c\u5668\u6765\u54cd\u5e94\u5e94\u7528\u7a0b\u5e8f\u751f\u547d\u5468\u671f\u4e8b\u4ef6\u6216\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u3002\u4ee5\u4e0b\u662f Spring Boot \u76d1\u542c\u5668\u7684\u57fa\u672c\u6d41\u7a0b\uff1a<\/p>\n<ol>\n<li>\n<strong>\u521b\u5efa\u76d1\u542c\u5668\u7c7b\uff1a<\/strong><\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a\u7c7b\uff0c\u5e76\u5b9e\u73b0 <code>ApplicationListener<\/code> \u63a5\u53e3\uff0c\u6216\u8005\u5728\u65b9\u6cd5\u4e0a\u4f7f\u7528 <code>@EventListener<\/code> \u6ce8\u89e3\u3002<code>ApplicationListener<\/code> \u662f Spring \u6846\u67b6\u63d0\u4f9b\u7684\u901a\u7528\u76d1\u542c\u5668\u63a5\u53e3\uff0c\u800c <code>@EventListener<\/code> \u6ce8\u89e3\u662f Spring 4.2+ \u63d0\u4f9b\u7684\u6ce8\u89e3\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">import org.springframework.context.ApplicationListener;\nimport org.springframework.context.event.ContextRefreshedEvent;\nimport org.springframework.stereotype.Component;\n\n@Component\npublic class MyApplicationListener implements ApplicationListener&lt;ContextRefreshedEvent&gt; {\n\n    @Override\n    public void onApplicationEvent(ContextRefreshedEvent event) {\n        \/\/ \u5904\u7406\u4e8b\u4ef6\u7684\u903b\u8f91\n        System.out.println(&quot;Context Refreshed Event Received!&quot;);\n    }\n}\n<\/code><\/pre>\n<p>\u6216\u8005\u4f7f\u7528 <code>@EventListener<\/code> \u6ce8\u89e3\uff1a<\/p>\n<pre><code class=\"language-java\">import org.springframework.context.event.ContextRefreshedEvent;\nimport org.springframework.context.event.EventListener;\nimport org.springframework.stereotype.Component;\n\n@Component\npublic class MyAnnotationListener {\n\n    @EventListener\n    public void handleContextRefresh(ContextRefreshedEvent event) {\n        \/\/ \u5904\u7406\u4e8b\u4ef6\u7684\u903b\u8f91\n        System.out.println(&quot;Context Refreshed Event Received!&quot;);\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u6ce8\u518c\u76d1\u542c\u5668\uff08\u53ef\u9009\uff09\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728 Spring Boot \u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u901a\u5e38\u65e0\u9700\u663e\u5f0f\u6ce8\u518c\u76d1\u542c\u5668\uff0c\u56e0\u4e3a Spring Boot \u4f1a\u81ea\u52a8\u626b\u63cf\u5e76\u6ce8\u518c\u4f7f\u7528 <code>@Component<\/code> \u6ce8\u89e3\u6807\u8bb0\u7684\u7c7b\u3002\u5982\u679c\u4f60\u6ca1\u6709\u4f7f\u7528 <code>@Component<\/code> \u6ce8\u89e3\uff0c\u53ef\u4ee5\u5728\u914d\u7f6e\u7c7b\u4e2d\u901a\u8fc7 <code>addApplicationListener<\/code> \u65b9\u6cd5\u6ce8\u518c\u76d1\u542c\u5668\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">import org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\n\n@Configuration\npublic class MyConfig {\n\n    @Bean\n    public MyApplicationListener myApplicationListener() {\n        return new MyApplicationListener();\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u5904\u7406\u4e8b\u4ef6\u903b\u8f91\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728\u76d1\u542c\u5668\u7c7b\u4e2d\u5b9e\u73b0 <code>onApplicationEvent<\/code> \u65b9\u6cd5\u6216\u4f7f\u7528 <code>@EventListener<\/code> \u6ce8\u89e3\u7684\u65b9\u6cd5\u4e2d\uff0c\u7f16\u5199\u5904\u7406\u4e8b\u4ef6\u7684\u903b\u8f91\u3002\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u4f60\u53ef\u4ee5\u83b7\u53d6\u4e8b\u4ef6\u5bf9\u8c61\uff0c\u5e76\u6839\u636e\u4e8b\u4ef6\u7684\u7c7b\u578b\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">import org.springframework.context.ApplicationListener;\nimport org.springframework.context.event.ContextRefreshedEvent;\nimport org.springframework.stereotype.Component;\n\n@Component\npublic class MyApplicationListener implements ApplicationListener&lt;ContextRefreshedEvent&gt; {\n\n    @Override\n    public void onApplicationEvent(ContextRefreshedEvent event) {\n        \/\/ \u5904\u7406\u4e8b\u4ef6\u7684\u903b\u8f91\n        System.out.println(&quot;Context Refreshed Event Received!&quot;);\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u89e6\u53d1\u4e8b\u4ef6\uff1a<\/strong><\/p>\n<ul>\n<li>\u4e8b\u4ef6\u901a\u5e38\u662f\u7531 Spring \u6846\u67b6\u6216\u5e94\u7528\u7a0b\u5e8f\u81ea\u8eab\u89e6\u53d1\u7684\u3002\u5728 Spring Boot \u4e2d\uff0c\u4e00\u4e9b\u5e38\u89c1\u7684\u4e8b\u4ef6\u5305\u62ec <code>ContextRefreshedEvent<\/code>\uff08\u5e94\u7528\u4e0a\u4e0b\u6587\u5237\u65b0\u4e8b\u4ef6\uff09\u3001<code>ContextStartedEvent<\/code>\uff08\u5e94\u7528\u4e0a\u4e0b\u6587\u542f\u52a8\u4e8b\u4ef6\uff09\u7b49\u3002\u4f60\u4e5f\u53ef\u4ee5\u521b\u5efa\u81ea\u5b9a\u4e49\u4e8b\u4ef6\uff0c\u5e76\u5728\u9002\u5f53\u7684\u65f6\u5019\u89e6\u53d1\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">import org.springframework.context.ApplicationEventPublisher;\nimport org.springframework.context.ApplicationEventPublisherAware;\nimport org.springframework.stereotype.Component;\n\n@Component\npublic class MyEventPublisher implements ApplicationEventPublisherAware {\n\n    private ApplicationEventPublisher eventPublisher;\n\n    public void doSomethingAndPublishEvent() {\n        \/\/ \u6267\u884c\u4e00\u4e9b\u64cd\u4f5c\n        \/\/ ...\n\n        \/\/ \u53d1\u5e03\u81ea\u5b9a\u4e49\u4e8b\u4ef6\n        eventPublisher.publishEvent(new MyCustomEvent(this));\n    }\n\n    @Override\n    public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) {\n        this.eventPublisher = eventPublisher;\n    }\n}\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>MyEventPublisher<\/code> \u7c7b\u901a\u8fc7 <code>eventPublisher.publishEvent(new MyCustomEvent(this))<\/code> \u53d1\u5e03\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u4e8b\u4ef6 <code>MyCustomEvent<\/code>\u3002\n<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728 Spring Boot \u4e2d\u521b\u5efa\u548c\u4f7f\u7528\u76d1\u542c\u5668\uff0c\u7528\u4e8e\u54cd\u5e94\u5e94\u7528\u7a0b\u5e8f\u7684\u751f\u547d\u5468\u671f\u4e8b\u4ef6\u6216\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u3002\u76d1\u542c\u5668\u63d0\u4f9b\u4e86\u4e00\u79cd\u677e\u8026\u5408\u7684\u65b9\u5f0f\uff0c\u8ba9\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u7ec4\u4ef6\u80fd\u591f\u5bf9\u7279\u5b9a\u4e8b\u4ef6\u505a\u51fa\u54cd\u5e94\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Spring Boot \u4e2d\uff0c\u901a\u8fc7\u5b9e\u73b0 ApplicationListener \u63a5\u53e3\u6216\u4f7f\u7528 @EventLi [&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-8537","post","type-post","status-publish","format-standard","hentry","category-springboot"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8537","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=8537"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8537\/revisions"}],"predecessor-version":[{"id":16628,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8537\/revisions\/16628"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=8537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=8537"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=8537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}