spring 事件監聽機制的三種實現方式

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章