spring 事件监听机制的三种实现方式,无序监听(实现事件方式) ,有序监听(实现smart事件方式), 注解监听
准备一个事件:
@Slf4j
public class OrderEvent extends ApplicationEvent {
public OrderEvent(Object source,String createOrder) {
super(source);
log.debug(“订单:{}下单”,createOrder);
}
}
发布他:
@RestController
public class TestDemo {
@Autowired
ApplicationContext applicationContext;
@GetMapping(“test01”)
public void test01(@RequestParam String msg){
applicationContext.publishEvent(new OrderEvent(“testListener”,msg));
}
}
无序监听方式:
/*
-
实现事件方式的实现无序监听
*/
@Component
public class SmsListener implements ApplicationListener {
@Override
public void onApplicationEvent(OrderEvent orderEvent) {System.out.println(orderEvent.getSource()); System.out.println("发送短信");
}
}
/*
- 实现事件方式的实现无序监听
*/
@Component
public class WxListener implements ApplicationListener {
@Override
public void onApplicationEvent(OrderEvent orderEvent) {
System.out.println(orderEvent.getSource());
System.out.println(“发送微信”);
}
}
有序监听:
/**
-
有序监听
*/
@Component
public class AutoListener implements SmartApplicationListener {@Override
public boolean supportsEventType(Class<? extends ApplicationEvent> aClass) {
System.out.println(“类型对比”+aClass.toString());
return aClass == OrderEvent.class;
}@Override
public void onApplicationEvent(ApplicationEvent applicationEvent) {
System.out.println(“执行事件逻辑”);
}@Override
public boolean supportsSourceType(@Nullable Class<?> sourceType) {
System.out.println(“sourceType对比”+sourceType.toString());
return sourceType == String.class;
}@Override
public int getOrder() {
return 1;
}
}
注解监听:
/**
-
注解实现方式
*/
@Component
public class AnonaListener {@EventListener
public void test01(OrderEvent orderEvent){
System.out.println(“注解方式发送短信”+orderEvent.getSource());
}@EventListener
public void test02(OrderEvent orderEvent){
System.out.println(“注解方式发送微信”+orderEvent.getSource());
}
}
运行结果:
类型对比class org.springframework.context.event.ContextRefreshedEvent
类型对比class org.springframework.context.event.ContextRefreshedEvent
程序启动完毕
2019-06-29 18:24:47.815 INFO 4902 — [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8888 (http) with context path ‘’
类型对比class org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent
类型对比class org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent
2019-06-29 18:24:47.818 INFO 4902 — [ main] com.Application : Started Application in 17.851 seconds (JVM running for 23.512)
类型对比class org.springframework.boot.context.event.ApplicationStartedEvent
类型对比class org.springframework.boot.context.event.ApplicationStartedEvent
类型对比class org.springframework.boot.context.event.ApplicationReadyEvent
类型对比class org.springframework.boot.context.event.ApplicationReadyEvent
2019-06-29 18:25:08.626 INFO 4902 — [nio-8888-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet ‘dispatcherServlet’
2019-06-29 18:25:08.626 INFO 4902 — [nio-8888-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet ‘dispatcherServlet’
2019-06-29 18:25:08.634 INFO 4902 — [nio-8888-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 8 ms
类型对比class com.spring.event.OrderEvent
sourceType对比class java.lang.String
注解方式发送微信testListener
注解方式发送短信testListener
执行事件逻辑
testListener
发送短信
testListener
发送微信
类型对比class org.springframework.web.context.support.ServletRequestHandledEvent
类型对比class org.springframework.web.context.support.ServletRequestHandledEvent
类型对比class org.springframework.context.event.ContextClosedEvent
类型对比class org.springframework.context.event.ContextClosedEvent