likes
comments
collection
share

一个领导看了就能涨薪的解耦编程新技能

作者站长头像
站长
· 阅读数 24

hello大家好,今天要给大家介绍一个新的编程技能,基于事件驱动来编程。 在spring容器中,提供了一种事件机制,帮助springbean之间进行通信。他提供了一系列基础类,简化了程序员的事件功能开发,通过事件机制可以将我们的程序代码进行解耦,下面我们就让我们开始了解下Spring的事件机制吧。

1、事件源:ApplicationEvent

public abstract class ApplicationEvent extends EventObject {​  
//记录事件发生时间  
private final long timestamp;​  
public ApplicationEvent(Object 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) {   
     
     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内置事件列表

上面是自自定义的事件,其实在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容器将自动对该事件进行发布,例如:

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

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

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

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

异步事件支持

上文介绍到spring事件默认是同步的,其实spring也支持异步的处理事件,只需要添加异步支持(开启Spring TaskExecutor)并且添加@Async注解即可开启异步的处理事件

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

总结

Spring提供了比较简单事件基础类让Spring bean之间可以通信,让程序员很容易实现事件的发布,监听,支持顺序监听,异步监听,通过事件监听也可以使我们的程序进行解耦。 这种编码方式是你喜欢的吗,可以在合适的场景试一试。

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

喜欢就点个在看再走吧一个领导看了就能涨薪的解耦编程新技能