{"id":315434,"date":"2024-05-21T00:00:35","date_gmt":"2024-05-20T16:00:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/315434.html"},"modified":"2024-05-21T00:00:43","modified_gmt":"2024-05-20T16:00:43","slug":"spring-%e6%a1%86%e6%9e%b6%e4%b8%ad%e6%9c%89%e5%93%aa%e4%ba%9b%e4%b8%8d%e5%90%8c%e7%b1%bb%e5%9e%8b%e7%9a%84%e4%ba%8b%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/315434.html","title":{"rendered":"Spring \u6846\u67b6\u4e2d\u6709\u54ea\u4e9b\u4e0d\u540c\u7c7b\u578b\u7684\u4e8b\u4ef6"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27035146\/9104a782-ef6a-48f4-b44d-f7737c23c46d.webp\" alt=\"Spring \u6846\u67b6\u4e2d\u6709\u54ea\u4e9b\u4e0d\u540c\u7c7b\u578b\u7684\u4e8b\u4ef6\" \/><\/p>\n<p><p>Spring\u6846\u67b6\u4e2d\u4e8b\u4ef6\u5904\u7406\u662f\u4e00\u4e2a\u91cd\u8981\u6982\u5ff5\uff0c\u5b83\u5b9e\u73b0\u4e86\u5e94\u7528\u7a0b\u5e8f\u4e4b\u95f4\u677e\u8026\u5408\u7684\u901a\u4fe1\u3002\u5728Spring\u6846\u67b6\u4e2d\uff0c\u4e3b\u8981\u5b58\u5728\u4e94\u79cd\u7c7b\u578b\u7684\u4e8b\u4ef6\uff1a<strong>\u5e94\u7528\u7a0b\u5e8f\u4e8b\u4ef6(Application Context Events)\u3001\u81ea\u5b9a\u4e49\u4e8b\u4ef6(Custom Events)\u3001\u4e8b\u52a1\u4e8b\u4ef6(Transaction Events)\u3001Web\u76f8\u5173\u4e8b\u4ef6(Web-Specific Events)\u3001\u4ee5\u53ca\u6d4b\u8bd5\u4e8b\u4ef6(Test Events)<\/strong>\u3002\u8fd9\u5176\u4e2d\uff0c\u5e94\u7528\u7a0b\u5e8f\u4e8b\u4ef6\u662f\u6700\u5e38\u89c1\u4e14\u88ab\u5e7f\u6cdb\u4f7f\u7528\u7684\u4e8b\u4ef6\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e<strong>\u5e94\u7528\u7a0b\u5e8f\u4e8b\u4ef6<\/strong>\uff0c\u5176\u4e3b\u8981\u6d89\u53caSpring\u5e94\u7528\u7a0b\u5e8f\u4e0a\u4e0b\u6587\u4e2d\u7684\u72b6\u6001\u53d8\u5316\u548c\u7279\u5b9a\u7684\u5e94\u7528\u7a0b\u5e8f\u884c\u4e3a\u3002\u4f8b\u5982\uff0c<strong>ContextStartedEvent<\/strong>\u8868\u793a\u5f53\u5e94\u7528\u7a0b\u5e8f\u6216Spring\u5bb9\u5668\u542f\u52a8\u5b8c\u6210\u65f6\u5c31\u4f1a\u53d1\u5e03\u6b64\u4e8b\u4ef6\u3002\u901a\u8fc7\u76d1\u542c\u8fd9\u4e00\u4e8b\u4ef6\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6267\u884c\u4e00\u4e9b\u521d\u59cb\u5316\u5de5\u4f5c\uff0c\u6bd4\u5982\u8d44\u6e90\u52a0\u8f7d\u3001\u6570\u636e\u9884\u5904\u7406\u7b49\u3002\u76d1\u542c\u5668\u80fd\u591f\u901a\u8fc7\u5b9e\u73b0<strong>ApplicationListener<\/strong>\u63a5\u53e3\u6216\u4f7f\u7528\u6ce8\u89e3@EventListener\u6765\u54cd\u5e94\u4e0d\u540c\u7c7b\u578b\u7684\u4e8b\u4ef6\uff0c\u4ece\u800c\u4f7f\u5f97\u5e94\u7528\u7a0b\u5e8f\u5728\u7279\u5b9a\u72b6\u6001\u4e0b\u80fd\u591f\u6267\u884c\u9884\u5b9a\u4e49\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5e94\u7528\u7a0b\u5e8f\u4e8b\u4ef6<\/h2>\n<\/p>\n<p><p>Spring\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u6807\u51c6\u7684\u5e94\u7528\u7a0b\u5e8f\u4e8b\u4ef6\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>ContextRefreshedEvent<\/strong>\uff1a\u5f53ApplicationContext\u88ab\u521d\u59cb\u5316\u6216\u5237\u65b0\u65f6\uff0c\u8be5\u4e8b\u4ef6\u88ab\u53d1\u5e03\u3002\u8fd9\u5e38\u5e38\u7528\u4e8e\u9700\u8981\u5728Spring\u5bb9\u5668\u52a0\u8f7d\u540e\u7acb\u523b\u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>ContextStartedEvent<\/strong>\uff1a\u5728ApplicationContext\u542f\u52a8\u540e\u53d1\u5e03\u6b64\u4e8b\u4ef6\u3002\u4f7f\u7528ConfigurableApplicationContext\u7684start()\u65b9\u6cd5\u53ef\u4ee5\u89e6\u53d1\u8fd9\u4e2a\u4e8b\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u5e94\u7528\u7a0b\u5e8f\u4e8b\u4ef6\u63d0\u4f9b\u4e86\u4e00\u4e2a\u6846\u67b6\uff0c\u901a\u8fc7\u5b83\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u5728\u5e94\u7528\u7a0b\u5e8f\u7684\u751f\u547d\u5468\u671f\u4e2d\u7684\u7279\u5b9a\u70b9\u8fdb\u884c\u63d2\u5165\u81ea\u5df1\u7684\u903b\u8f91\u6216\u8005\u662f\u8fdb\u884c\u67d0\u4e9b\u8d44\u6e90\u7684\u51c6\u5907\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u81ea\u5b9a\u4e49\u4e8b\u4ef6<\/h2>\n<\/p>\n<p><p>\u5f00\u53d1\u8005\u53ef\u4ee5\u57fa\u4e8eApplicationEvent\u7c7b\u521b\u5efa\u81ea\u5df1\u7684\u4e8b\u4ef6\u7c7b\u578b\uff0c\u4f7f\u5f97Spring\u5e94\u7528\u80fd\u591f\u53d1\u5e03\u548c\u76d1\u542c\u7279\u5b9a\u4e8e\u4e1a\u52a1\u573a\u666f\u7684\u4e8b\u4ef6\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u521b\u5efa\u81ea\u5b9a\u4e49\u4e8b\u4ef6<\/strong>\uff1a\u9700\u7ee7\u627fApplicationEvent\u7c7b\uff0c\u5e76\u5b9a\u4e49\u5fc5\u8981\u7684\u4fe1\u606f\u4f5c\u4e3a\u4e8b\u4ef6\u7684\u4e00\u90e8\u5206\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u53d1\u5e03\u81ea\u5b9a\u4e49\u4e8b\u4ef6<\/strong>\uff1a\u901a\u8fc7ApplicationContext\u7684publishEvent\u65b9\u6cd5\u53ef\u4ee5\u53d1\u5e03\u81ea\u5b9a\u4e49\u4e8b\u4ef6\uff0c\u9700\u8981\u6ce8\u5165ApplicationContext\u7684\u5f15\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7075\u6d3b\u5730\u5904\u7406\u4e1a\u52a1\u76f8\u5173\u7684\u4e8b\u4ef6\u901a\u77e5\uff0c\u901a\u8fc7\u4e8b\u4ef6\u7684\u53d1\u5e03\u4e0e\u76d1\u542c\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e1a\u52a1\u6a21\u5757\u95f4\u7684\u89e3\u8026\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4e8b\u52a1\u4e8b\u4ef6<\/h2>\n<\/p>\n<p><p>\u867d\u7136Spring\u76f4\u63a5\u5e76\u6ca1\u6709\u5b9a\u4e49\u7279\u5b9a\u7684\u4e8b\u52a1\u4e8b\u4ef6\u7c7b\u578b\uff0c\u4f46\u662f\u901a\u8fc7Spring\u7684\u4e8b\u4ef6\u53d1\u5e03\u673a\u5236\uff0c\u53ef\u4ee5\u5728\u4e8b\u52a1\u7684\u751f\u547d\u5468\u671f\uff08\u5982\u4e8b\u52a1\u5f00\u59cb\u3001\u4e8b\u52a1\u5b8c\u6210\uff09\u4e2d\u53d1\u5e03\u4e8b\u4ef6\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u5229\u7528\u4e8b\u52a1\u540c\u6b65\u56de\u8c03<\/strong>\uff1aSpring\u4e8b\u52a1\u7ba1\u7406\u63d0\u4f9b\u4e86TransactionSynchronizationManager\u6765\u6ce8\u518c\u4e8b\u52a1\u540c\u6b65\u56de\u8c03\uff0c\u5141\u8bb8\u5728\u4e8b\u52a1\u9636\u6bb5\uff08\u5982beforeCommit\u3001afterCommit\uff09\u5e7f\u64ad\u4e8b\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7ed3\u5408AOP\u8fdb\u884c\u4e8b\u4ef6\u53d1\u5e03<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7AOP\u62e6\u622a\u4e8b\u52a1\u65b9\u6cd5\uff0c\u7136\u540e\u5728\u4e8b\u52a1\u7684\u4e0d\u540c\u9636\u6bb5\u53d1\u5e03\u81ea\u5b9a\u4e49\u7684\u4e8b\u52a1\u4e8b\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u4e8b\u52a1\u4e8b\u4ef6\uff0c\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u5728\u4e8b\u52a1\u7684\u7279\u5b9a\u9636\u6bb5\u6267\u884c\u4e00\u4e9b\u989d\u5916\u7684\u4e1a\u52a1\u903b\u8f91\uff0c\u5982\u5ba1\u8ba1\u65e5\u5fd7\u3001\u7edf\u8ba1\u4fe1\u606f\u6536\u96c6\u7b49\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001Web\u76f8\u5173\u4e8b\u4ef6<\/h2>\n<\/p>\n<p><p>\u5728Spring Web\u5e94\u7528\u4e2d\uff0c\u5b58\u5728\u4e00\u4e9b\u7279\u5b9a\u4e8eWeb\u7684\u4e8b\u4ef6\u7c7b\u578b\uff0c\u6bd4\u5982\u8bf7\u6c42\u521d\u59cb\u5316\u6216\u7ed3\u675f\u7684\u4e8b\u4ef6\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>RequestHandledEvent<\/strong>\uff1a\u5f53\u4e00\u4e2aHTTP\u8bf7\u6c42\u5904\u7406\u5b8c\u6210\u65f6\uff0cSpring\u4f1a\u53d1\u5e03\u4e00\u4e2aRequestHandledEvent\uff0c\u8be5\u4e8b\u4ef6\u5305\u542b\u4e86\u8bf7\u6c42\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u5982\u8bf7\u6c42\u8def\u5f84\u3001\u5904\u7406\u65f6\u95f4\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>ServletContext\u76f8\u5173\u4e8b\u4ef6<\/strong>\uff1a\u901a\u8fc7\u5b9e\u73b0ServletContextListener\u63a5\u53e3\uff0c\u53ef\u4ee5\u76d1\u542cServletContext\u7684\u521d\u59cb\u5316\u4e0e\u9500\u6bc1\u4e8b\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>Web\u76f8\u5173\u4e8b\u4ef6\u8ba9\u5f00\u53d1\u8005\u80fd\u591f\u5728Web\u5e94\u7528\u7684\u8bf7\u6c42\u5904\u7406\u6d41\u7a0b\u4e2d\u63d2\u5165\u81ea\u5b9a\u4e49\u903b\u8f91\uff0c\u4f18\u5316\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u6d4b\u8bd5\u4e8b\u4ef6<\/h2>\n<\/p>\n<p><p>\u5728Spring\u6d4b\u8bd5\u6846\u67b6\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e8b\u4ef6\u6765\u6a21\u62df\u5e94\u7528\u7a0b\u5e8f\u884c\u4e3a\u6216\u76d1\u542c\u6d4b\u8bd5\u8fc7\u7a0b\u4e2d\u7684\u7279\u5b9a\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u6a21\u62df\u4e8b\u4ef6<\/strong>\uff1a\u5728\u6d4b\u8bd5\u671f\u95f4\uff0c\u53ef\u4ee5\u53d1\u5e03\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u6765\u6a21\u62df\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u4e8b\u4ef6\u884c\u4e3a\uff0c\u4ee5\u4fbf\u4e8e\u6d4b\u8bd5\u4e1a\u52a1\u903b\u8f91\u7684\u54cd\u5e94\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u76d1\u542c\u6d4b\u8bd5\u4e8b\u4ef6<\/strong>\uff1a\u901a\u8fc7\u5bf9\u6d4b\u8bd5\u4e8b\u4ef6\u7684\u76d1\u542c\uff0c\u53ef\u4ee5\u5728\u6d4b\u8bd5\u524d\u540e\u6267\u884c\u51c6\u5907\u6216\u6e05\u7406\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u6d4b\u8bd5\u4e8b\u4ef6\u662f\u5728\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u7684\u6d4b\u8bd5\u9636\u6bb5\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u5b83\u5e2e\u52a9\u5f00\u53d1\u8005\u786e\u4fdd\u5e94\u7528\u903b\u8f91\u5728\u9762\u5bf9\u4e8b\u4ef6\u65f6\u7684\u6b63\u786e\u6027\u548c\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cSpring\u6846\u67b6\u4e2d\u7684\u4e8b\u4ef6\u673a\u5236\u63d0\u4f9b\u4e86\u4e00\u79cd\u9ad8\u6548\u7684\u65b9\u5f0f\u6765\u8fdb\u884c\u5e94\u7528\u7a0b\u5e8f\u5185\u90e8\u5404\u7ec4\u4ef6\u95f4\u7684\u901a\u4fe1\u3002\u65e0\u8bba\u662f\u5185\u7f6e\u7684\u5e94\u7528\u7a0b\u5e8f\u4e8b\u4ef6\u3001\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u3001Web\u76f8\u5173\u4e8b\u4ef6\uff0c\u8fd8\u662f\u6574\u5408\u4e8b\u52a1\u548c\u6d4b\u8bd5\u4e8b\u4ef6\uff0cSpring\u7684\u4e8b\u4ef6\u6846\u67b6\u90fd\u4e3a\u6784\u5efa\u677e\u8026\u5408\u3001\u6613\u6d4b\u8bd5\u7684\u5e94\u7528\u7a0b\u5e8f\u63d0\u4f9b\u4e86\u5f3a\u6709\u529b\u7684\u652f\u6301\u3002\u901a\u8fc7\u5408\u7406\u5229\u7528\u8fd9\u4e9b\u4e8b\u4ef6\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5728\u4fdd\u6301\u4ee3\u7801\u6e05\u6670\u7684\u540c\u65f6\uff0c\u5b9e\u73b0\u590d\u6742\u7684\u4e1a\u52a1\u903b\u8f91\u548c\u6d41\u7a0b\u63a7\u5236\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. Spring\u6846\u67b6\u4e2d\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u4e8b\u4ef6\u7c7b\u578b\uff1f<\/strong><\/p>\n<p>Spring\u6846\u67b6\u4e2d\u6709\u591a\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u4e8b\u4ef6\uff0c\u4ee5\u4fbf\u4e8e\u5f00\u53d1\u4eba\u5458\u5728\u4e0d\u540c\u9636\u6bb5\u548c\u573a\u666f\u4e2d\u5904\u7406\u76f8\u5e94\u7684\u4e8b\u4ef6\u3002\u5e38\u89c1\u7684\u4e8b\u4ef6\u7c7b\u578b\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\n<p>\u5e94\u7528\u4e0a\u4e0b\u6587\u4e8b\u4ef6\uff08ApplicationContext events\uff09\uff1a\u8fd9\u4e9b\u4e8b\u4ef6\u4e0e\u5e94\u7528\u4e0a\u4e0b\u6587\u7684\u751f\u547d\u5468\u671f\u76f8\u5173\uff0c\u4f8b\u5982\u542f\u52a8\u3001\u5173\u95ed\u3001\u5237\u65b0\u7b49\u3002\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0ApplicationListener\u63a5\u53e3\u6765\u76d1\u542c\u5e76\u5904\u7406\u8fd9\u4e9b\u4e8b\u4ef6\u3002<\/p>\n<\/li>\n<li>\n<p>Bean\u751f\u547d\u5468\u671f\u4e8b\u4ef6\uff08Bean lifecycle events\uff09\uff1a\u8fd9\u4e9b\u4e8b\u4ef6\u4e0eSpring Bean\u7684\u751f\u547d\u5468\u671f\u76f8\u5173\uff0c\u4f8b\u5982Bean\u7684\u521d\u59cb\u5316\u548c\u9500\u6bc1\u3002\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0InitializingBean\u548cDisposableBean\u63a5\u53e3\uff0c\u6216\u8005\u4f7f\u7528@PostConstruct\u548c@PreDestroy\u6ce8\u89e3\u6765\u76d1\u542c\u5e76\u5904\u7406\u8fd9\u4e9b\u4e8b\u4ef6\u3002<\/p>\n<\/li>\n<li>\n<p>\u81ea\u5b9a\u4e49\u4e8b\u4ef6\uff08Custom events\uff09\uff1a\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u5b9a\u4e49\u548c\u53d1\u5e03\u81ea\u5b9a\u4e49\u4e8b\u4ef6\uff0c\u7136\u540e\u901a\u8fc7\u5b9e\u73b0ApplicationEvent\u63a5\u53e3\u6765\u76d1\u542c\u5e76\u5904\u7406\u8fd9\u4e9b\u4e8b\u4ef6\u3002\u53ef\u4ee5\u901a\u8fc7ApplicationContext\u7684publishEvent\u65b9\u6cd5\u6765\u53d1\u5e03\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u3002<\/p>\n<\/li>\n<\/ul>\n<p><strong>2. \u5982\u4f55\u76d1\u542cSpring\u6846\u67b6\u4e2d\u7684\u4e8b\u4ef6\uff1f<\/strong><\/p>\n<p>\u8981\u76d1\u542cSpring\u6846\u67b6\u4e2d\u7684\u4e8b\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0ApplicationListener\u63a5\u53e3\u6765\u521b\u5efa\u81ea\u5b9a\u4e49\u7684\u4e8b\u4ef6\u76d1\u542c\u5668\u3002\u5728\u76d1\u542c\u5668\u4e2d\uff0c\u901a\u8fc7\u91cd\u5199onApplicationEvent\u65b9\u6cd5\u6765\u5904\u7406\u7279\u5b9a\u7684\u4e8b\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-java\">public class MyEventListener implements ApplicationListener&lt;ApplicationEvent&gt; {\n    @Override\n    public void onApplicationEvent(ApplicationEvent event) {\n        \/\/\u5904\u7406\u7279\u5b9a\u7684\u4e8b\u4ef6\u903b\u8f91\n    }\n}\n<\/code><\/pre>\n<p>\u7136\u540e\uff0c\u5c06\u76d1\u542c\u5668\u6ce8\u518c\u5230\u5e94\u7528\u4e0a\u4e0b\u6587\u4e2d\uff0c\u4ee5\u4fbfSpring\u6846\u67b6\u5728\u76f8\u5e94\u7684\u4e8b\u4ef6\u53d1\u751f\u65f6\u901a\u77e5\u76d1\u542c\u5668\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u914d\u7f6e\u6216\u8005\u4f7f\u7528@Component\u6ce8\u89e3\u6765\u6ce8\u518c\u76d1\u542c\u5668\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u89e6\u53d1\u548c\u5904\u7406\u81ea\u5b9a\u4e49\u4e8b\u4ef6\uff1f<\/strong><\/p>\n<p>\u8981\u89e6\u53d1\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u5e76\u5904\u7406\u5b83\uff0c\u9996\u5148\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u7ee7\u627f\u81eaApplicationEvent\u7684\u4e8b\u4ef6\u7c7b\uff0c\u5176\u4e2d\u53ef\u4ee5\u5305\u542b\u81ea\u5b9a\u4e49\u7684\u4e8b\u4ef6\u4fe1\u606f\u3002\u7136\u540e\uff0c\u5728\u76f8\u5e94\u7684\u5730\u65b9\uff08\u4f8b\u5982\u67d0\u4e2a\u65b9\u6cd5\u4e2d\uff09\u53d1\u5e03\u8be5\u4e8b\u4ef6\uff0c\u4f7f\u7528ApplicationContext\u7684publishEvent\u65b9\u6cd5\u5373\u53ef\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-java\">public class CustomEvent extends ApplicationEvent {\n    \/\/\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u4fe1\u606f\n    public CustomEvent(Object source) {\n        super(source);\n    }\n}\n\npublic class EventPublisher {\n    @Autowired\n    private ApplicationContext applicationContext;\n\n    public void publishCustomEvent() {\n        CustomEvent event = new CustomEvent(this);\n        applicationContext.publishEvent(event);\n    }\n}\n\npublic class MyEventListener implements ApplicationListener&lt;CustomEvent&gt; {\n    @Override\n    public void onApplicationEvent(CustomEvent event) {\n        \/\/\u5904\u7406\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u7684\u903b\u8f91\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u7684\u793a\u4f8b\u4e2d\uff0cEventPublisher\u7c7b\u7528\u4e8e\u53d1\u5e03\u81ea\u5b9a\u4e49\u4e8b\u4ef6\uff0c\u800cMyEventListener\u7c7b\u7528\u4e8e\u76d1\u542c\u548c\u5904\u7406\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u3002\u8fd9\u6837\uff0c\u5f53\u8c03\u7528EventPublisher\u7684publishCustomEvent\u65b9\u6cd5\u65f6\uff0cMyEventListener\u7684onApplicationEvent\u65b9\u6cd5\u5c06\u4f1a\u88ab\u89e6\u53d1\uff0c\u4ece\u800c\u5904\u7406\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Spring\u6846\u67b6\u4e2d\u4e8b\u4ef6\u5904\u7406\u662f\u4e00\u4e2a\u91cd\u8981\u6982\u5ff5\uff0c\u5b83\u5b9e\u73b0\u4e86\u5e94\u7528\u7a0b\u5e8f\u4e4b\u95f4\u677e\u8026\u5408\u7684\u901a\u4fe1\u3002\u5728Spring\u6846\u67b6\u4e2d\uff0c\u4e3b\u8981\u5b58\u5728\u4e94\u79cd [&hellip;]","protected":false},"author":3,"featured_media":315442,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/315434"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=315434"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/315434\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/315442"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=315434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=315434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=315434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}