{"id":871,"date":"2023-03-09T21:44:04","date_gmt":"2023-03-09T13:44:04","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=871"},"modified":"2023-04-29T16:43:47","modified_gmt":"2023-04-29T08:43:47","slug":"spring-boot-use-application-event-and-listener-to-quickly-achieve-business-decoupling","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/09\/spring-boot-use-application-event-and-listener-to-quickly-achieve-business-decoupling\/","title":{"rendered":"Spring Boot\u4f7f\u7528ApplicationEvent\u548cListener\u5feb\u901f\u5b9e\u73b0\u4e1a\u52a1\u89e3\u8026"},"content":{"rendered":"<h2>ApplicationEvent\u7b80\u4ecb<\/h2>\n<p>ApplicationEvent\u4ee5\u53caListener\u662fSpring\u4e3a\u6211\u4eec\u63d0\u4f9b\u7684\u4e00\u4e2a\u4e8b\u4ef6\u76d1\u542c\u3001\u8ba2\u9605\u7684\u5b9e\u73b0\uff0c\u5185\u90e8\u5b9e\u73b0\u539f\u7406\u662f\u89c2\u5bdf\u8005\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u8bbe\u8ba1\u521d\u8877\u4e5f\u662f\u4e3a\u4e86\u7cfb\u7edf\u4e1a\u52a1\u903b\u8f91\u4e4b\u95f4\u7684\u89e3\u8026\uff0c\u63d0\u9ad8\u53ef\u6269\u5c55\u6027\u4ee5\u53ca\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p><!-- more --><\/p>\n<p>\u901a\u8fc7 ApplicationEvent \u7c7b\u548c ApplicationListener \u63a5\u53e3\u6765\u63d0\u4f9b\u5728 ApplicationContext \u4e2d\u5904\u7406\u4e8b\u4ef6\u3002\u5982\u679c\u4e00\u4e2a bean \u5b9e\u73b0 ApplicationListener\uff0c\u90a3\u4e48\u6bcf\u6b21 ApplicationEvent \u88ab\u53d1\u5e03\u5230 ApplicationContext \u4e0a\uff0c\u90a3\u4e2a bean \u4f1a\u88ab\u901a\u77e5\u3002<\/p>\n<p>Spring\u5185\u7f6e\u4ee5\u4e0b\u6807\u51c6\u4e8b\u4ef6\uff1a<\/p>\n<ul>\n<li><strong>ContextRefreshedEvent<\/strong><\/li>\n<\/ul>\n<p>ApplicationContext \u88ab\u521d\u59cb\u5316\u6216\u5237\u65b0\u65f6\uff0c\u8be5\u4e8b\u4ef6\u88ab\u53d1\u5e03\u3002\u8fd9\u4e5f\u53ef\u4ee5\u5728 ConfigurableApplicationContext \u63a5\u53e3\u4e2d\u4f7f\u7528 refresh() \u65b9\u6cd5\u6765\u53d1\u751f\u3002<\/p>\n<ul>\n<li><strong>ContextStartedEvent<\/strong><\/li>\n<\/ul>\n<p>\u5f53\u4f7f\u7528 ConfigurableApplicationContext \u63a5\u53e3\u4e2d\u7684 start() \u65b9\u6cd5\u542f\u52a8 ApplicationContext \u65f6\uff0c\u8be5\u4e8b\u4ef6\u88ab\u53d1\u5e03\u3002\u4f60\u53ef\u4ee5\u8c03\u67e5\u4f60\u7684\u6570\u636e\u5e93\uff0c\u6216\u8005\u4f60\u53ef\u4ee5\u5728\u63a5\u53d7\u5230\u8fd9\u4e2a\u4e8b\u4ef6\u540e\u91cd\u542f\u4efb\u4f55\u505c\u6b62\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<ul>\n<li><strong>ContextStoppedEvent<\/strong><\/li>\n<\/ul>\n<p>\u5f53\u4f7f\u7528 ConfigurableApplicationContext \u63a5\u53e3\u4e2d\u7684 stop() \u65b9\u6cd5\u505c\u6b62 ApplicationContext \u65f6\uff0c\u53d1\u5e03\u8fd9\u4e2a\u4e8b\u4ef6\u3002\u4f60\u53ef\u4ee5\u5728\u63a5\u53d7\u5230\u8fd9\u4e2a\u4e8b\u4ef6\u540e\u505a\u5fc5\u8981\u7684\u6e05\u7406\u7684\u5de5\u4f5c\u3002<\/p>\n<ul>\n<li><strong>ContextClosedEvent<\/strong><\/li>\n<\/ul>\n<p>\u5f53\u4f7f\u7528 ConfigurableApplicationContext \u63a5\u53e3\u4e2d\u7684 close() \u65b9\u6cd5\u5173\u95ed ApplicationContext \u65f6\uff0c\u8be5\u4e8b\u4ef6\u88ab\u53d1\u5e03\u3002\u4e00\u4e2a\u5df2\u5173\u95ed\u7684\u4e0a\u4e0b\u6587\u5230\u8fbe\u751f\u547d\u5468\u671f\u672b\u7aef\uff1b\u5b83\u4e0d\u80fd\u88ab\u5237\u65b0\u6216\u91cd\u542f\u3002<\/p>\n<ul>\n<li><strong>RequestHandledEvent<\/strong><\/li>\n<\/ul>\n<p>\u8fd9\u662f\u4e00\u4e2a web-specific \u4e8b\u4ef6\uff0c\u544a\u8bc9\u6240\u6709 bean HTTP \u8bf7\u6c42\u5df2\u7ecf\u88ab\u670d\u52a1\u3002<\/p>\n<h2>\u6784\u5efaModel<\/h2>\n<pre><code class=\"language-java\">@Data\n@NoArgsConstructor\npublic class User {\n    private String name;\n    private String email;\n}<\/code><\/pre>\n<h2>\u521b\u5efaEvent\u4e8b\u4ef6<\/h2>\n<p>\u9996\u5148\u6ce8\u610f\uff1alistener\u90fd\u662f\u56f4\u7ed5\u7740event\u6765\u6302\u8d77\u7684<\/p>\n<pre><code class=\"language-java\">public class UserRegisterEvent extends ApplicationEvent {\n    @Getter @Setter\n    public User user;\n\n    \/**\n     *\n     * @param source \u53d1\u751f\u4e8b\u4ef6\u7684\u5bf9\u8c61\n     * @param user \u6ce8\u518c\u7528\u6237\u5bf9\u8c61\n     *\/\n    public UserRegisterEvent(Object source,User user) {\n        super(source);\n        this.user = user;\n    }\n}<\/code><\/pre>\n<h2>\u4e8b\u4ef6\u53d1\u5e03<\/h2>\n<p>\u6b64\u90e8\u5206\u4e3b\u8981\u5728\u4e1a\u52a1\u903b\u8f91\u6a21\u5757\u89e6\u53d1\uff0c\u4e3b\u8981\u7528\u5230ApplicationContext\u7c7b\u4ee5\u53ca\u5b83\u7684<code>publishEvent(ApplicationEvent t)<\/code>\u65b9\u6cd5<\/p>\n<pre><code class=\"language-java\">@Slf4j\n@Service\npublic class UserService {\n    @Autowired\n    private ApplicationContext applicationContext;\n\n    public void register(User user){\n        \/\/\u7701\u7565\u4e1a\u52a1\u903b\u8f91\n        \/\/\u53d1\u5e03UserRegisterEvent\u4e8b\u4ef6\n        log.info(&quot;-------- \u53d1\u5e03UserRegisterEvent\u4e8b\u4ef6 --------&quot;);\n        applicationContext.publishEvent(new UserRegisterEvent(this, user));\n    }\n}<\/code><\/pre>\n<p>\u6d4b\u8bd5\u7528\u4f8b<\/p>\n<pre><code>@Test\npublic void publishEvent() {\n    User user = new User();\n    user.setName(&quot;Joe.Ye&quot;);\n    user.setEmail(&quot;test@test.com&quot;);\n    userService.register(user);\n}<\/code><\/pre>\n<h2>\u521b\u5efaEvent\u4e8b\u4ef6\u76d1\u542c<\/h2>\n<h3>\u57fa\u4e8e<code>@EventListener<\/code>\u6ce8\u89e3\u5b9e\u73b0<\/h3>\n<p>\u6ce8\u89e3\u65b9\u5f0f\u6bd4\u8f83\u7b80\u5355\uff0c\u5e76\u4e0d\u9700\u8981\u5b9e\u73b0\u4efb\u4f55\u63a5\u53e3<\/p>\n<pre><code class=\"language-java\">\/**\n * \u6ce8\u89e3\u65b9\u5f0f\u6bd4\u8f83\u7b80\u5355\uff0c\u5e76\u4e0d\u9700\u8981\u5b9e\u73b0\u4efb\u4f55\u63a5\u53e3\uff0c\u6392\u5e8f\u968f\u673a\n *\/\n@Component\npublic class AnnotationRegisterListener {\n\n    @EventListener\n    public void sendEmail(UserRegisterEvent userRegisterEvent) {\n        \/\/\u83b7\u53d6\u6ce8\u518c\u7528\u6237\u5bf9\u8c61\n        User user = userRegisterEvent.getUser();\n        \/\/\u8f93\u51fa\u6ce8\u518c\u7528\u6237\u4fe1\u606f\n        System.out.println(&quot;AnnotationRegisterListener: \u53d1\u9001\u90ae\u4ef6, \u7528\u6237\u540d: &quot; + user.getName()+&quot;, \u90ae\u7bb1: &quot; + user.getEmail());\n    }\n\n    \/**\n     * \u6ce8\u518c\u76d1\u542c\u5b9e\u73b0\u65b9\u6cd5\n     * @param userRegisterEvent \u7528\u6237\u6ce8\u518c\u4e8b\u4ef6\n     *\/\n    @EventListener\n    public void register(UserRegisterEvent userRegisterEvent) {\n        \/\/\u83b7\u53d6\u6ce8\u518c\u7528\u6237\u5bf9\u8c61\n        User user = userRegisterEvent.getUser();\n        \/\/\u8f93\u51fa\u6ce8\u518c\u7528\u6237\u4fe1\u606f\n        System.out.println(&quot;AnnotationRegisterListener: \u6ce8\u518c\u4fe1\u606f, \u7528\u6237\u540d: &quot; + user.getName() + &quot;, \u90ae\u7bb1: &quot; + user.getEmail());\n    }\n}<\/code><\/pre>\n<h3>\u5b9e\u73b0 ApplicationListener\u63a5\u53e3<\/h3>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4e5f\u662fSpring\u4e4b\u524d\u6bd4\u8f83\u5e38\u7528\u7684\u76d1\u542c\u4e8b\u4ef6\u65b9\u5f0f\uff0c\u5728\u5b9e\u73b0ApplicationListener\u63a5\u53e3\u65f6\u9700\u8981\u5c06\u76d1\u542c\u4e8b\u4ef6\u4f5c\u4e3a\u6cdb\u578b\u53c2\u6570\u4f20\u9012<\/p>\n<pre><code class=\"language-java\">@Component\npublic class RegisterListener implements ApplicationListener&lt;UserRegisterEvent&gt; {\n\n    @Override\n    public void onApplicationEvent(UserRegisterEvent userRegisterEvent) {\n        \/\/\u83b7\u53d6\u6ce8\u518c\u7528\u6237\u5bf9\u8c61\n        User user = userRegisterEvent.getUser();\n        \/\/\u8f93\u51fa\u6ce8\u518c\u7528\u6237\u4fe1\u606f\n        System.out.println(&quot;RegisterListener: \u6ce8\u518c\u4fe1\u606f, \u7528\u6237\u540d: &quot; + user.getName()+&quot;, \u5bc6\u7801: &quot; + user.getEmail());\n    }\n\n}<\/code><\/pre>\n<h3>\u5b9e\u73b0SmartApplicationListener\u63a5\u53e3<\/h3>\n<p>\u53ef\u4ee5\u5b9e\u73b0\u6709\u5e8f\u76d1\u542c<\/p>\n<pre><code class=\"language-java\">@Component\npublic class SmartRegisterListener implements SmartApplicationListener {\n\n    @Override\n    public boolean supportsEventType(Class&lt;? extends ApplicationEvent&gt; aClass) {\n        \/\/\u53ea\u6709UserRegisterEvent\u76d1\u542c\u7c7b\u578b\u624d\u4f1a\u6267\u884c\u4e0b\u9762\u903b\u8f91\n        return aClass == UserRegisterEvent.class;\n    }\n\n    @Override\n    public boolean supportsSourceType(Class&lt;?&gt; sourceType) {\n        \/\/\u53ea\u6709\u5728UserService\u5185\u53d1\u5e03\u7684UserRegisterEvent\u4e8b\u4ef6\u65f6\u624d\u4f1a\u6267\u884c\u4e0b\u9762\u903b\u8f91\n        return sourceType == UserService.class;\n    }\n\n    \/**\n     * supportsEventType &amp; supportsSourceType \u4e24\u4e2a\u65b9\u6cd5\u8fd4\u56detrue\u65f6\u8c03\u7528\u8be5\u65b9\u6cd5\u6267\u884c\u4e1a\u52a1\u903b\u8f91\n     * @param applicationEvent applicationEvent \u5177\u4f53\u76d1\u542c\u5b9e\u4f8b\uff0c\u8fd9\u91cc\u662fUserRegisterEvent\n     *\/\n    @Override\n    public void onApplicationEvent(ApplicationEvent applicationEvent) {\n        \/\/\u8f6c\u6362\u4e8b\u4ef6\u7c7b\u578b\n        UserRegisterEvent userRegisterEvent = (UserRegisterEvent) applicationEvent;\n        \/\/\u83b7\u53d6\u6ce8\u518c\u7528\u6237\u5bf9\u8c61\u4fe1\u606f\n        User user = userRegisterEvent.getUser();\n        \/\/\u5b8c\u6210\u6ce8\u518c\u4e1a\u52a1\u903b\u8f91\n        System.out.println(&quot;SmartRegisterListener: &quot; + user.getName());\n    }\n\n    \/**\n     * return \u7684\u6570\u503c\u8d8a\u5c0f\u8bc1\u660e\u4f18\u5148\u7ea7\u8d8a\u9ad8\uff0c\u6267\u884c\u987a\u5e8f\u8d8a\u9760\u524d\n     * @return\n     *\/\n    @Override\n    public int getOrder() {\n        return 10;\n    }\n}<\/code><\/pre>\n<blockquote>\n<p>SmartApplicationListener\u63a5\u53e3\u7ee7\u627f\u4e86\u5168\u5c40\u76d1\u542cApplicationListener\uff0c\u5e76\u4e14\u6cdb\u578b\u5bf9\u8c61\u4f7f\u7528\u7684ApplicationEvent\u6765\u4f5c\u4e3a\u5168\u5c40\u76d1\u542c\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u4f7f\u7528SmartApplicationListener\u4f5c\u4e3a\u76d1\u542c\u7236\u63a5\u53e3\u7684\u5b9e\u73b0\uff0c\u76d1\u542c\u6240\u6709\u4e8b\u4ef6\u53d1\u5e03\u3002<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\u9664\u4e86\u4e0a\u9762\u7684\u65b9\u6cd5\uff0c\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e2agetOrder\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5c31\u53ef\u4ee5\u89e3\u51b3\u6267\u884c\u76d1\u542c\u7684\u987a\u5e8f\u95ee\u9898\uff0creturn\u7684\u6570\u503c\u8d8a\u5c0f\u8bc1\u660e\u4f18\u5148\u7ea7\u8d8a\u9ad8\uff0c\u6267\u884c\u987a\u5e8f\u8d8a\u9760\u524d\u3002<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>ApplicationEvent\u7b80\u4ecb ApplicationEvent\u4ee5\u53caListener\u662fSpring\u4e3a\u6211\u4eec [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-871","post","type-post","status-publish","format-standard","hentry","category-spring-boot"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/871","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=871"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/871\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}