Spring 中的事件机制 ApplicationEventPublisher

摘要:
“);applicationEventPublisher.publishEvent;}注意:在Spring中,服务必须由Spring容器托管。ApplicationEventPublisherware是Spring提供的一个接口,用于将applicationEventPublisher事件发布器注入服务。使用此接口,我们自己的服务可以发布事件。创建一个事件订阅者@ServicepublicclassEmailServiceimplementsAppli阳离子侦听器<UserRe registerEvent>{@OverridepublicvotionApplicationEvent{System.out.println;}}注意:事件订阅者的服务也需要托管在Spring容器中。ApplicationListener接口是Spring提供的一个接口,事件订阅者必须实现该接口。我们通常将服务所关注的事件类型作为泛型类型传递。要处理事件,可以通过事件获取事件的特定内容。getSource()。在本例中,它是用户的名称。

需求

当用户注册后,给他发送一封邮件通知他注册成功了,然后给他初始化积分,再发放一张新用户注册优惠券等。

用户注册事件

public class UserRegisterEvent extends ApplicationEvent{
    public UserRegisterEvent(String name) { //name即source 复杂的对象,但注意要了解清楚序列化机制
        super(name);
    }
}

用户注册服务发布者

@Service
public class UserService  {
    
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    public void register(String name) {
        System.out.println("用户:" + name + " 已注册!");
        applicationEventPublisher.publishEvent(new UserRegisterEvent(name));
    }
}

注意:再Spring中,服务必须交给 Spring 容器托管。ApplicationEventPublisherAware 是由 Spring 提供的用于为 Service 注入 ApplicationEventPublisher 事件发布器的接口,使用这个接口,我们自己的 Service 就拥有了发布事件的能力。用户注册后,不再是显示调用其他的业务 Service,而是发布一个用户注册事件。

创建事件订阅者(邮件服务、积分服务等)

@Service
public class EmailService implements ApplicationListener<UserRegisterEvent> {
    @Override
    public void onApplicationEvent(UserRegisterEvent userRegisterEvent) {
        System.out.println("邮件服务接到通知,给 " + userRegisterEvent.getSource() + " 发送邮件...");
    }
}

注意:事件订阅者的服务同样需要托管于 Spring 容器,ApplicationListener接口是由 Spring 提供的事件订阅者必须实现的接口,我们一般把该 Service 关心的事件类型作为泛型传入。处理事件,通过 event.getSource() 即可拿到事件的具体内容,在本例中便是用户的姓名。

SpringBoot 测试启动类

@SpringBootApplication
@RestController
public class EventDemoApp {
    public static void main(String[] args) {
        SpringApplication.run(EventDemoApp.class, args);
    }
    @Autowired
    UserService userService;
    @RequestMapping("/register")
    public String register(){
        userService.register("zhangsan");
        return "success";
    }
}

完成

至此, 完成需求,后期无论如何扩展,我们只需要添加相应的事件订阅者即可。

免责声明:文章转载自《Spring 中的事件机制 ApplicationEventPublisher》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇【微信小游戏实战】零基础制作《欢乐停车场》一、游戏设计Golang的基础数据类型-浮点型下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

小程序真机上报错 for developer: some selectors are not allowed in component wxss , including tag name selectors, id selectors, and attribute selectors

在引用组件的组件和页面中使用后代选择器在某些极端情况下会产生意想不到的性能。如果是,请避免使用它们。子元素选择器只能在视图组件及其子节点之间使用,其他组件可能会导致意外情况。继承的样式(如字体和颜色)将从组件外部继承到组件内部。除了继承样式之外,app.wxss中的样式和组件所在页面的样式对于自定义组件无效。...

字符串解压缩类库(zip、GZIP、QuickLz、snappy、lzf、jzlib)介绍

它旨在提供高压缩速度和合理的压缩比=-1){out.write;}字节[]未压缩=输出。到字节数组();--返回提取字符串的字节数组。介绍使用预先选择的解压缩类库-GZIP压缩字符串=“这是一个用于测试的字符串”;ByteArrayOutputStreamout=新的ByteArray输出流();GZipOutputStreamgout=newGZipOut...

2022年可用QQ机器人框架

4.小李子机器人官网:https://xiaolz.cn评估:支持多个Q登录和论坛似乎是目前最活跃的。它支持许多api,可以满足许多需求。没有限制,但有很多错误。...

选包

安装系统后,将不会安装一些基本工具。此时,您可以根据yum的要求安装它们。你也可以使用任何你想要的时尚。...

fiddler抓包+雷电模拟器 完成手机app抓包的配置

找到系统应用,点击设置,点击无线网络WLAN—˃左键常按点击已连接网络—˃修改网络鼠标左键长按在桌面找到下面这个文件之后双击打开上面证书弄完之后。可以说本机已经安装过证书了,如果你能在模拟器上找到这个证书就不用将这个证书再拉入模拟器了在模拟器中打开系统应用—˃设置—˃安全—˃从SD卡安装。找到FiddlerRoot.cer文件,按提示导入即可,注意在此过程需...

IPv6地址的ping、telnet等操作

最近,我在研究https协议如何传输数据。我用wireshark捕捉数据包并分析它们。我发现客户端和谷歌网站在传输数据时使用了IPv6地址,因此我测试了与IPv6地址相关的基本功能。...