ApplicationContext發佈事件和處理事件

ApplicationContext中的事件處理是通過ApplicationEvent類和ApplicationListener接口來提供的,通過ApplicationContext的publishEvent()方法發佈到ApplicationListener; 
在這裏包含三個角色:被髮布的事件,事件發佈者,事件的監聽者。 
事件發佈者在發佈事件的時候->通知事件的監聽者。 

1.要發佈的事件

public class OrderEvent extends ApplicationEvent {

    private Order order;

    public UseVoucherCodesEvent(Order order) {
        super(order);
        this.order = order;
    }
}

2.發佈事件

this.applicationContext.publishEvent(new OrderEvent(order));

3.處理事件

@Slf4j
@Component
@Transactional
public class OrderListener implements ApplicationListener<OrderEvent> {
    @Override
    public void onApplicationEvent(OrderPayFinishEvent event) {
        //todo 處理業務邏輯
    }
}

 

 

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