Spring的Event事件處理使用詳解

2021-10-07 01:38:15 字數 1595 閱讀 7425

spring中使用事件非常簡單,只需要以下的幾個步驟:

public

class

userregisterevent

extends

public string getusername()

}定義傳送郵件和發放優惠券的監聽:

@component

@order(2

)//可以使用order指定順序,越小優先順序越高

public

class

mailuserregisterlistener

implements

}//也可以使用@eventlistener監聽事件

@component

@order(1

)public

class

couponuserregisterlistener

}使用者註冊成功以後傳送事件出來:

@service

public

class

userregisterservice

implements

public

boolean

userregister

(string username)

@override

public

void

throws bean***ception

@override

public

void

}

執行下:

public

class

asyncmain

}

@enableasync

//啟用非同步

@configuration

@componentscan

public

class

@component

@order(3

)public

class

asyncuserregisterlistener

implements

}

protected

void()

}else

}}

@override

public

void

multicastevent

(@nullable resolvabletype eventtype)

else

}}

@enableasync

@configuration

@componentscan

public

class

/** * 注意這個bean的名字,如果是taskexecutor,那麼@async也會使用這個執行緒池,否則@async不會使用,原因請參考前一篇

* */

@bean

public threadpoolexecuto***ctorybean taskexecutor()

}

Spring 註解事件Event

從spring的4.2版本後,開始支援註解來進行事件廣播接收,這使得我們非常方便 當然了spring也支援jms訊息中介軟體,這個就可以做多個系統整合了,感覺有點偏題了,先看看事件怎麼通過註解來開發 先來看看支援哪些預設事件 event 描述contextrefreshedeventcontexts...

event 事件的使用

event用於兩個執行緒間的協作,比如乙個執行緒得到了資料發訊號給另乙個執行緒讓它來處理 多執行緒裡的event from threading import thread,event import time,random event event def light print light is li...

事件物件event

1.e.client 相對於當前頁面 視窗,不是這個元素 可視區域的左頂點 document.onmousemove function e 2.e.screen 相對於計算機螢幕的左頂點 document.body.onclick function e 3.offset 事件觸發時滑鼠距離該元素左邊...