ApplicationEventPublisher的使用学习

得意犹堪夸世俗,诏黄新湿字如鸦。这篇文章主要讲述ApplicationEventPublisher的使用学习相关的知识,希望能为你提供帮助。
今天在程序中看到有使用这个接口,学习了一下,感觉作为观察者模式的一个实现方式,使用起来还是不错的。查了一些资料,结合自己的程序,分四个部分进行介绍。等程序自测完成后,补充完成,先写一部分。
一:介绍1.ApplicationEventPublisherAware
ApplicationEventPublisherAware 是由 Spring 提供的用于为 Service 注入 ApplicationEventPublisher 事件发布器的接口,使用这个接口,我们自己的 Service 就拥有了发布事件的能力。
用户注册后,不再是显示调用其他的业务 Service,而是发布一个用户注册事件。
 
2.ApplicationListener
ApplicationListener接口是由 Spring 提供的事件订阅者必须实现的接口,我们一般把该 Service 关心的事件类型作为泛型传入。处理事件,通过 event.getSource() 即可拿到事件的具体内容
 
3.ApplicationEventPublisher
ApplicationEventPublisher是ApplicationContext的父接口之一。这接口的作用是:Interface that encapsulates event publication functionality.
功能就是发布事件,也就是把某个事件告诉的所有与这个事件相关的监听器。
 
二:使用@EventLister1.示例程序【同步】
接口:

package com.jun.practice.service; public interface StudentEventRegisterService { /** * 发布事件,注册学生 */ void register(); }


接口实现:
@Service
public class StudentEventRegisterServiceImpl implements StudentEventRegisterService {
@Resource
private ApplicationEventPublisher applicationEventPublisher;

@Override
public void register() {
Student student = new Student();
student.setId(1);
student.setName("tom");
applicationEventPublisher.publishEvent(student);
System.out.println("结束了");
}
}

【ApplicationEventPublisher的使用学习】
监听:

@Component
public class StudentEventListener {
@EventListener(condition = "#student.id != null")
public void handleEvent(Student student){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(student);
}
}


测试:
package com.jun.practice.controller; import com.jun.practice.service.StudentEventRegisterService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController @RequestMapping("/event") @Api(value = "https://www.songbingjia.com/android/事件监控", tags = "事件监控") public class EventListenerController { @Resource private StudentEventRegisterService studentEventRegisterService; @ApiOperation("@EventListener测试") @GetMapping("/registerUser") public void register(){ try { studentEventRegisterService.register(); } catch (Exception e) { e.printStackTrace(); } } }

效果:
2020-07-09 18:57:14.414INFO 16704 --- [nio-9094-exec-1] o.s.web.servlet.DispatcherServlet: Completed initialization in 12 ms Student(name=tom, id=1) 结束了


2.进行异步
进行配置类:
/** * 开启异步支持 */ @Configuration @EnableAsync public class AsyncEventConfiguration implements AsyncConfigurer { @Override public Executor getAsyncExecutor() { return Executors.newFixedThreadPool(10); } }


在监听方法上添加@Async
package com.jun.practice.listener; import com.jun.practice.dto.Student; import org.springframework.context.event.EventListener; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; @Component public class StudentEventListener { @Async @EventListener(condition = "#student.id != null") public void handleEvent(Student student){ try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(student); } }

效果:
结束了 Student(name=tom, id=1)


3.实际的使用
等写完后粘贴
 
三:使用@TransactionalEventListener1.
 
四:一个小需求实战1.说明
这里是参考晚上的介绍,尝试自己实现
 
五:最原生的实现 

    推荐阅读