定义事件
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