欢迎来到胡世鑫的博客

扩大
缩小

Spring Boot的事件机制(个人技术博客)

Spring Boot的事件机制

1、技术概述,描述这个技术是做什么?学习该技术的原因,技术的难点在哪里。

Spring Boot应用程序事件允许我们发送和接收特定应用程序事件,我们可以根据需要处理这些事件。事件用于在松散耦合的组件之间交换信息。由于发布者和订阅者之间没有直接耦合,因此可以在不影响发布者的情况下修改订阅者,反之亦然。学习该技术可以很好的把握后端程序的运行与出错回滚,技术的难点在于如何创建、发布和侦听自定义事件。

2、技术详述,描述你是如何实现和使用该技术的,要求配合代码和流程图详细描述。

  • 创建ApplicationEvent

    class UserCreatedEvent extends ApplicationEvent {
    private String name; 
    
        UserCreatedEvent(Object source, String name) {
        super(source);
        this.name = name;
        }
        ...
    }
    
  • 用户注册服务发布者

    @Service
    public class UserService  {
    
        @Autowired
        private ApplicationEventPublisher applicationEventPublisher;
    
        public void register(String name) {
            System.out.println("用户:" + name + " 已注册!");
            applicationEventPublisher.publishEvent(new UserRegisterEvent(name));
        }
    }
    
  • 创建事件订阅者

    @Service
    public class EmailService implements    ApplicationListener<UserRegisterEvent> {
        @Override
        public void onApplicationEvent(UserRegisterEvent userRegisterEvent) {
            System.out.println("邮件服务接到通知,给 " + userRegisterEvent.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";
        }
    }
    

3、技术使用中遇到的问题和解决过程。

在Spring Boot中使用自带的事件机制前需要进行配置META-INF/spring.factories文件,并在文件中实现,针对其使用也有两种方式,最开始自己在这方面花费了大量时间,最后依靠百度、CSDN等平台解决了问题。

// 第一种方式
public class AiInfluxdbApplicationListener implements GenericApplicationListener {
    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE;
    }
    @Override
    public boolean supportsEventType(ResolvableType eventType) {
        return ApplicationReadyEvent.class.isAssignableFrom(eventType.getRawClass());
    }
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        System.out.print("here is ApplicationReadyEvent");
    }
}
//第二种方式
public class ConfigApplicationListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {
    @Override
    public int getOrder() {
        return HIGHEST_PRECEDENCE;
    }
    @Override
    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {

    }
}

//META-INF/spring.factories文件定义
org.springframework.context.ApplicationListener=\
com.demotest.core.ApplicationStartListener

4、进行总结。

本次技术博客从我自己的经历开始,介绍了Spring Boot框架中事件从创建到测试的整个过程以及代码,以及自身最开始对于事件的使用方式。

5、列出参考文献、参考博客

Spring 中的事件机制 ApplicationEventPublisher

springBoot的事件机制GenericApplicationListener用法解析

[译]谈谈SpringBoot 事件机制

posted on 2020-06-24 21:07  xin丶star  阅读(296)  评论(0编辑  收藏  举报

导航