温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用springboot怎么实现事件监听

发布时间:2021-04-20 16:34:22 来源:亿速云 阅读:338 作者:Leah 栏目:编程语言

这期内容当中小编将会给大家带来有关使用springboot怎么实现事件监听,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

springboot是什么

springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。

定义事件

@Getter
public class TestEvent extends ApplicationEvent {
 private String msg;

 public TestEvent(Object source, String msg) {
  super(source);
  this.msg = msg;
 }
}

定义事件监听(注解方式)

 @Component
 public class TestListen {
 @EventListener
 public void testListen(TestEvent event) {
  System.out.println(event.getMsg());
 }
}

注意:@Component 注解

发布事件

@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注解

@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类

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上下文,装载监听

@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容器中

@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类

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中配置监听

context.listener.classes=com.listener.MyListener3

方式4.

创建MyListener4类,该类无需实现ApplicationListener接口,使用@EventListener装饰具体方法

@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()));
 }
}

自定义事件代码如下:

@SuppressWarnings("serial")
public class MyEvent extends ApplicationEvent
{
 public MyEvent(Object source)
 {
 super(source);
 }
}

进行测试(在启动类中加入发布事件的逻辑):

@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怎么实现事件监听了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI