ITPub博客

首页 > 数据库 > 数据库开发技术 > Spring Event 是什么,看这里就知道了

Spring Event 是什么,看这里就知道了

原创 数据库开发技术 作者:服务端技术栈 时间:2020-07-19 23:48:43 0 删除 编辑

在spring容器中,提供了一种事件机制,帮助springbean之间进行通信。他提供了一系列基础类,简化了程序员的事件功能开发,通过事件机制可以将我们的程序代码进行解耦,下面我们就让我们开始了解下Spring的事件机制吧。

1、事件源:ApplicationEvent

public abstract class ApplicationEvent extends EventObject {
  //记录事件发生时间 private final long timestamp;
public ApplicationEvent(Object source) { super(source); this.timestamp = System.currentTimeMillis();  }   public final long getTimestamp() { return this.timestamp;  }}

2、事件监听器:ApplicationListener

public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
  // 事件处理方法  void onApplicationEvent(E event);}

3、事件发布器:ApplicationEventPublisher


public interface ApplicationEventPublisher {
default void publishEvent(ApplicationEvent event) { publishEvent((Object) event);  }  void publishEvent(Object event);}

先做个总结:通过上面3个基础抽象类和接口,就可以实现在spring容器内发布事件了,例如下面发生邮件黑名单例子:

1、自定义黑名单处理事件

public class BlackListEvent extends ApplicationEvent {
private final String address; private final String content;
public BlackListEvent(Object source, String address, String content) { super(source); this.address = address; this.content = content;    }}

2、邮件发布者

public class EmailService implements ApplicationEventPublisherAware {
private List<String> blackList; private ApplicationEventPublisher publisher;
public void setBlackList(List<String> blackList) { this.blackList = blackList; }
public void setApplicationEventPublisher(ApplicationEventPublisher publisher) { this.publisher = publisher; }
public void sendEmail(String address, String content) { if (blackList.contains(address)) {        //发布黑名单事件 publisher.publishEvent(new BlackListEvent(this, address, content)); return; } // do send email... }}

3、黑名单监听器处理

public class BlackListNotifier implements ApplicationListener<BlackListEvent> {
private String notificationAddress;
public void setNotificationAddress(String notificationAddress) { this.notificationAddress = notificationAddress; }

    //该方法接受参数化类型BlackListEvent,避免了向下转型,保证类型安全 public void onApplicationEvent(BlackListEvent event) {        // 黑名单处理逻辑 }}

通过上面三步,就可以完成事件发布监听了,是不是很简单。不过需要注意的是,这里的事件监听器是同步的,也就是会阻塞事件发布者,直到事件监听器处理完毕。

上面是自自定义的事件,其实在spring 中内置了几种事件:

ContextRefreshedEvent

该事件是在spring容器初始化后触发的,

比如ConfigurableApplicationContext

容器的refresh()方法调用

ContextStartedEvent

该事件是在spring容器开始启动后触发的,

比如ConfigurableApplicationContext容

器的start()方法调用

ContextStoppedEvent

该事件是在spring容器停止后触发的,比

如ConfigurableApplicationContext容

器的stop()方法调用

ContextClosedEvent

该事件是在spring容器停止后触发的,

比如ConfigurableApplicationContext容

器的close()方法调用,spring容器close后,

是不能恢复的

RequestHandledEvent

该事件是spring web应用中,对于web请

求完成后触发的事件

ServletRequestHandledEvent

他是RequestHandledEvent的子类

触发时机是一样的

上面我们是通过实现spring接口来实现事件监听功能的,在Spring 4.2之后,spring提供了基于注解的方式来定义和实现事件监听功能,我们只需要将监听器类重新成如下,一样可以实现监听器的效果:

public class BlackListNotifier {    

   private String notificationAddress;

   

   public void setNotificationAddress(String notificationAddress) {                    this.notificationAddress = notificationAddress;    }    

   

   @EventListener    public void processBlackListEvent(BlackListEvent event) {

        //黑名单事件处理    } }

没错,就是通过@EventListener注解来完成监听器的注册,同时该注解还支持监听指定多个事件类型监听需要的事件:

@EventListener({ContextStartedEvent.class, ContextRefreshedEvent.class})public void handleContextStart() {    // ...}

或者el表达式参数来过滤需要监听的事件:

@EventListener(condition = "#blEvent.content == 'my-event'")public void processBlackListEvent(BlackListEvent blEvent) {    // notify appropriate parties via notificationAddress...}

如果监听事件之后,需要再次发布事件,只要将监听器方法返回对于的事件对象,spring容器将自动对该事件进行发布,例如:

@EventListenerpublic ListUpdateEvent handleBlackListEvent(BlackListEvent event) {    // notify appropriate parties via notificationAddress and    // then publish a ListUpdateEvent...}

像上面一样,ListUpdateEvent事件将在handleBlackListEvent事件处理完成后自动发布,如果需要发布多个事件还可以将返回的对象变成集合类型。


spring还支持事件顺序,只需要在监听器方法上加上@Order注解即可,例如:

@EventListener@Order(42)public void processBlackListEvent(BlackListEvent event) {    // notify appropriate parties via notificationAddress...}

上文介绍到spring事件默认是同步的,其实spring也支持异步的处理事件,只

需要添加异步支持(开启Spring TaskExecutor)并且添加@Async注解即可开启异步的处理事件

@Asyncpublic void processBlackListEvent(BlackListEvent event) {    // BlackListEvent is processed in a separate thread}

最后在总结下,spring提供了比较简单事件基础类让Spring bean之间可以通信,让程序员很容易实现事件的发布,监听,支持顺序监听,异步监听,通过事件监听也可以使我们的程序进行解耦。

好了,Spring的事件机制就介绍到这里了,大家有没有问题啊,欢迎留言探讨!!


喜欢就点个在看再走吧



原文作者:服务端技术栈;如有侵权,请联系管理员删除。

请登录后发表评论 登录
全部评论

注册时间:2019-03-08

  • 博文量
    97
  • 访问量
    41430