定義事件
1
2
3
4
5
6
7
8
9
|
@getter public class testevent extends applicationevent { private string msg; public testevent(object source, string msg) { super (source); this .msg = msg; } } |
定義事件監聽(注解方式)
1
2
3
4
5
6
7
|
@component public class testlisten { @eventlistener public void testlisten(testevent event) { system.out.println(event.getmsg()); } } |
注意:@component 注解
發布事件
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@autowired private applicationcontext publiser; @getmapping ( "test-listen" ) public void testlisten() { for ( int i = 0 ; i < 10 ; i++) { system.out.println( "i = " + i); } publiser.publishevent( new testevent( this , "測試事件監聽" )); for ( int j = 0 ; j < 10 ; j++) { system.out.println( "j = " + j); } } |
測試時執行順序:
- i循環
- 打印"event = [測試事件監聽]"
- j循環
異步監聽
監聽加上@async注解
1
2
3
4
5
6
7
8
9
10
|
@component public class testlisten { @eventlistener @async public void testlisten(testevent event) { for ( int i = 0 ; i < 10 ; i++) { system.out.println( "event = [" + event.getmsg() + "]" ); } } } |
測試時執行順序:
- i循環
- j循環
- 打印"event = [測試事件監聽]"
代碼:async
springboot進行事件監聽有四種方式:
1.手工向applicationcontext中添加監聽器
2.將監聽器裝載入spring容器
3.在application.properties中配置監聽器
4.通過@eventlistener注解實現事件監聽
講到事件監聽,這里我們說下自定義事件和自定義監聽器類的實現方式:
- 自定義事件:繼承自applicationevent抽象類,然后定義自己的構造器
- 自定義監聽:實現applicationlistener<t>接口,然后實現onapplicationevent方法
下面講下4種事件監聽的具體實現
方式1.
首先創建mylistener1類
1
2
3
4
5
6
7
8
9
|
public class mylistener1 implements applicationlistener<myevent> { logger logger = logger.getlogger(mylistener1. class ); public void onapplicationevent(myevent event) { logger.info(string.format( "%s監聽到事件源:%s." , mylistener1. class .getname(), event.getsource())); } } |
然后在springboot應用啟動類中獲取configurableapplicationcontext上下文,裝載監聽
1
2
3
4
5
6
7
8
9
10
|
@springbootapplication public class lisenterapplication { public static void main(string[] args) { configurableapplicationcontext context = springapplication.run(lisenterapplication. class , args); //裝載監聽 context.addapplicationlistener( new mylistener1()); } } |
方式2.
創建mylistener2類,并使用@component注解將該類裝載入spring容器中
1
2
3
4
5
6
7
8
9
10
|
@component public class mylistener2 implements applicationlistener<myevent> { logger logger = logger.getlogger(mylistener2. class ); public void onapplicationevent(myevent event) { logger.info(string.format( "%s監聽到事件源:%s." , mylistener2. class .getname(), event.getsource())); } } |
方式3.
首先創建mylistener3類
1
2
3
4
5
6
7
8
9
|
public class mylistener3 implements applicationlistener<myevent> { logger logger = logger.getlogger(mylistener3. class ); public void onapplicationevent(myevent event) { logger.info(string.format( "%s監聽到事件源:%s." , mylistener3. class .getname(), event.getsource())); } } |
然后在application.properties中配置監聽
1
|
context.listener.classes=com.listener.mylistener3 |
方式4.
創建mylistener4類,該類無需實現applicationlistener接口,使用@eventlistener裝飾具體方法
1
2
3
4
5
6
7
8
9
10
11
|
@component public class mylistener4 { logger logger = logger.getlogger(mylistener4. class ); @eventlistener public void listener(myevent event) { logger.info(string.format( "%s監聽到事件源:%s." , mylistener4. class .getname(), event.getsource())); } } |
自定義事件代碼如下:
1
2
3
4
5
6
7
8
|
@suppresswarnings ( "serial" ) public class myevent extends applicationevent { public myevent(object source) { super (source); } } |
進行測試(在啟動類中加入發布事件的邏輯):
1
2
3
4
5
6
7
8
9
10
11
12
|
@springbootapplication public class lisenterapplication { public static void main(string[] args) { configurableapplicationcontext context = springapplication.run(lisenterapplication. class , args); //裝載事件 context.addapplicationlistener( new mylistener1()); //發布事件 context.publishevent( new myevent( "測試事件." )); } } |
啟動后,日志打印如下:
2018-06-15 10:51:20.198 info 4628 --- [ main] com.listener.mylistener3 : com.listener.mylistener3監聽到事件源:測試事件..
2018-06-15 10:51:20.198 info 4628 --- [ main] com.listener.mylistener4 : com.listener.mylistener4監聽到事件源:測試事件..
2018-06-15 10:51:20.199 info 4628 --- [ main] com.listener.mylistener2 : com.listener.mylistener2監聽到事件源:測試事件..
2018-06-15 10:51:20.199 info 4628 --- [ main] com.listener.mylistener1 : com.listener.mylistener1監
聽到事件源:測試事件..
由日志打印可以看出,springboot四種事件的實現方式監聽是有序的
完整的代碼路徑:https://github.com/ingorewho/springboot-develope/tree/master/springboot-listener
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://segmentfault.com/a/1190000018839229