Spring事件

作者: 幻影翔 | 来源:发表于2019-11-23 14:24 被阅读0次

    关键类

    ApplicaitonEvent  事件源
    ApplicaitonListener 编码式事件监听
    @EventListener  注册式事件监听及处理
    applicaitonContext  Spring容器。可用于发布事件
    

    遵循流程

    自定义事件,继承ApplicaitonEvent
    定义监听事件,实现ApplicationListener
    使用容器触发事件
    发布事件,使用applicationContext发布事件
    

    异步处理

    • 默认Spring事件同步处理
    • 异步关键
      1、开启@EnableAsync
      2、方法上添加@Async

    同步

    image.png

    异步

    image.png

    流程实现

    事件类

    image.png

    事件处理类

    image.png

    发布者(启动类开启@EnableAsync)

    image.png
    localhost:8080/user/test 触发

    相关文章

      网友评论

        本文标题:Spring事件

        本文链接:https://www.haomeiwen.com/subject/qkhhwctx.html