{"id":12055,"date":"2023-11-28T22:41:36","date_gmt":"2023-11-28T14:41:36","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=12055"},"modified":"2023-12-11T15:08:08","modified_gmt":"2023-12-11T07:08:08","slug":"spring-zhongeventlistener-de-gong-zuo-yuan-li","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/spring-zhongeventlistener-de-gong-zuo-yuan-li\/","title":{"rendered":"Spring \u4e2d@EventListener \u7684\u5de5\u4f5c\u539f\u7406\uff1f"},"content":{"rendered":"<p><code>@EventListener<\/code> \u662fSpring\u6846\u67b6\u4e2d\u7528\u4e8e\u5904\u7406\u4e8b\u4ef6\u7684\u6ce8\u89e3\u3002\u901a\u8fc7<code>@EventListener<\/code>\u6ce8\u89e3\uff0c\u4f60\u53ef\u4ee5\u5c06\u65b9\u6cd5\u6807\u8bb0\u4e3a\u4e8b\u4ef6\u76d1\u542c\u5668\uff0c\u7528\u4e8e\u54cd\u5e94\u7279\u5b9a\u7c7b\u578b\u7684\u4e8b\u4ef6\u3002\u8fd9\u4e2a\u6ce8\u89e3\u7684\u5de5\u4f5c\u539f\u7406\u4e3b\u8981\u57fa\u4e8eSpring\u7684\u4e8b\u4ef6\u9a71\u52a8\u6a21\u578b\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f<code>@EventListener<\/code>\u7684\u5de5\u4f5c\u539f\u7406\uff1a<\/p>\n<ol>\n<li>\n<strong>\u5b9a\u4e49\u4e8b\u4ef6\uff1a<\/strong><\/p>\n<ul>\n<li>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u4e8b\u4ef6\u7c7b\uff0c\u901a\u5e38\u662f\u4e00\u4e2a\u666e\u901a\u7684POJO\u7c7b\uff0c\u7528\u4e8e\u5c01\u88c5\u4e8b\u4ef6\u7684\u76f8\u5173\u4fe1\u606f\u3002\u4e8b\u4ef6\u7c7b\u4e00\u822c\u9700\u8981\u7ee7\u627f\u81ea<code>ApplicationEvent<\/code>\u7c7b\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">public class MyCustomEvent extends ApplicationEvent {\n\n    public MyCustomEvent(Object source) {\n        super(source);\n    }\n\n    \/\/ Additional event-related methods or properties...\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u53d1\u5e03\u4e8b\u4ef6\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728\u5e94\u7528\u7a0b\u5e8f\u7684\u67d0\u4e2a\u5730\u65b9\uff0c\u4f60\u9700\u8981\u53d1\u5e03\u4e8b\u4ef6\uff0c\u901a\u5e38\u901a\u8fc7<code>ApplicationEventPublisher<\/code>\u63a5\u53e3\u7684<code>publishEvent<\/code>\u65b9\u6cd5\u5b9e\u73b0\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@Autowired\nprivate ApplicationEventPublisher eventPublisher;\n\npublic void someBusinessLogic() {\n    \/\/ ... some business logic ...\n    MyCustomEvent event = new MyCustomEvent(this);\n    eventPublisher.publishEvent(event);\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u521b\u5efa\u4e8b\u4ef6\u76d1\u542c\u5668\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528<code>@EventListener<\/code>\u6ce8\u89e3\u6807\u8bb0\u4e00\u4e2a\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u5c06\u4f5c\u4e3a\u4e8b\u4ef6\u76d1\u542c\u5668\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@Component\npublic class MyEventListener {\n\n    @EventListener\n    public void handleMyCustomEvent(MyCustomEvent event) {\n        \/\/ Handle the event...\n        System.out.println(&quot;Received custom event: &quot; + event.toString());\n    }\n}\n<\/code><\/pre>\n<ul>\n<li>\u6ce8\u610f\uff1a<code>@EventListener<\/code>\u6ce8\u89e3\u53ef\u4ee5\u7528\u5728\u4efb\u4f55Spring\u7ba1\u7406\u7684Bean\u7684\u65b9\u6cd5\u4e0a\uff0c\u4e0d\u4ec5\u4ec5\u662f<code>@Component<\/code>\u6807\u8bb0\u7684\u7c7b\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<strong>\u76d1\u542c\u4e8b\u4ef6\uff1a<\/strong><\/p>\n<ul>\n<li>\n\u5f53\u4e8b\u4ef6\u88ab\u53d1\u5e03\u65f6\uff0c\u5e26\u6709<code>@EventListener<\/code>\u6ce8\u89e3\u7684\u65b9\u6cd5\u5c06\u88ab\u81ea\u52a8\u8c03\u7528\uff0c\u5e76\u4e14\u65b9\u6cd5\u7684\u53c2\u6570\u5c06\u88ab\u6ce8\u5165\u4e3a\u76f8\u5e94\u7684\u4e8b\u4ef6\u3002\n<\/li>\n<li>\n<code>MyEventListener<\/code>\u4e2d\u7684<code>handleMyCustomEvent<\/code>\u65b9\u6cd5\u5c06\u5728<code>MyCustomEvent<\/code>\u88ab\u53d1\u5e03\u65f6\u88ab\u8c03\u7528\u3002\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c<code>@EventListener<\/code>\u6ce8\u89e3\u7684\u5de5\u4f5c\u539f\u7406\u53ef\u4ee5\u603b\u7ed3\u4e3a\uff1a<\/p>\n<ul>\n<li>Spring\u5bb9\u5668\u4f1a\u68c0\u6d4b\u6240\u6709\u5e26\u6709<code>@EventListener<\/code>\u6ce8\u89e3\u7684\u65b9\u6cd5\u3002<\/li>\n<li>\u5f53\u76f8\u5173\u4e8b\u4ef6\u88ab\u53d1\u5e03\u65f6\uff0cSpring\u6846\u67b6\u4f1a\u8c03\u7528\u5339\u914d\u4e8b\u4ef6\u7c7b\u578b\u7684<code>@EventListener<\/code>\u6ce8\u89e3\u7684\u65b9\u6cd5\uff0c\u5e76\u5c06\u4e8b\u4ef6\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u79cd\u4e8b\u4ef6\u9a71\u52a8\u7684\u673a\u5236\u5141\u8bb8\u89e3\u8026\u5e94\u7528\u7a0b\u5e8f\u7684\u4e0d\u540c\u90e8\u5206\uff0c\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>@EventListener \u662fSpring\u6846\u67b6\u4e2d\u7528\u4e8e\u5904\u7406\u4e8b\u4ef6\u7684\u6ce8\u89e3\u3002\u901a\u8fc7@EventListener\u6ce8\u89e3\uff0c\u4f60 [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[],"class_list":["post-12055","post","type-post","status-publish","format-standard","hentry","category-spring"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12055","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=12055"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12055\/revisions"}],"predecessor-version":[{"id":46234,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12055\/revisions\/46234"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=12055"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=12055"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=12055"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}