java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > ApplicationListener的使用

Spring中ApplicationListener的使用解析

作者:云川之下

这篇文章主要介绍了Spring中ApplicationListener的使用解析,ApplicationContext事件机制是观察者设计模式的实现,通过ApplicationEvent类和ApplicationListener接口,需要的朋友可以参考下

背景

ApplicationContext事件机制是观察者设计模式的实现,通过ApplicationEvent类和ApplicationListener接口,可以实现ApplicationContext事件处理;

如果容器中存在ApplicationListener的Bean,当ApplicationContext调用publishEvent方法时,对应的Bean会被触发。

spring内置事件

同样事件可以自定义、监听也可以自定义,完全根据自己的业务逻辑来处理。

ApplicationListener源码

@FunctionalInterface
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
    /**
     * Handle an application event.
     * @param event the event to respond to
     */
    void onApplicationEvent(E event);
}

ContextRefreshedEvent事件的监听

以Spring的内置事件ContextRefreshedEvent为例,当ApplicationContext被初始化或刷新时,会触发ContextRefreshedEvent事件,下面我们就实现一个ApplicationListener来监听此事件的发生。

@Component
public class MyListener implements ApplicationListener<ContextRefreshedEvent> {
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        System.out.println("容器中初始化Bean数量:" + event.getApplicationContext().getBeanDefinitionCount());
    }
}

启动服务,可以看到

在这里插入图片描述

至此,便完成了一个事件及监听类的实现和实例化。

自定义事件及监听,以发送邮件为例

自定义邮件通知事件类:EmailEvent

package com.lw.coodytest.event;
import org.springframework.context.ApplicationEvent;
/**
 * @Classname EmailEvent
 * @Description 邮件通知事件
 * @Author lw
 * @Date 2019-12-20 11:05
 */
public class EmailEvent extends ApplicationEvent {
    private String email;
    private String content;
    public EmailEvent(Object source){
        super(source);
    }
    public EmailEvent(Object source, String email, String content){
        super(source);
        this.email = email;
        this.content = content;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
}

自定义邮件通知监听类:EmailListener:

package com.lw.coodytest.event;
import org.springframework.context.ApplicationEvent;
/**
 * @Classname EmailEvent
 * @Description 邮件通知事件
 * @Author lw
 * @Date 2019-12-20 11:05
 */
public class EmailEvent extends ApplicationEvent {
    private String email;
    private String content;
    public EmailEvent(Object source){
        super(source);
    }
    public EmailEvent(Object source, String email, String content){
        super(source);
        this.email = email;
        this.content = content;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
}

注意:onApplicationEvent方法的入参,必须是自定义的那个类型

单元测试类:

package com.lw.coodytest.junit;
import com.lw.coodytest.event.EmailEvent;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.context.WebApplicationContext;
/**
 * @Classname ListenerTest
 * @Description 监听测试类
 * @Author lw
 * @Date 2019-12-20 11:12
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class ListenerTest {
    @Autowired
    private WebApplicationContext webapplicationcontext;
    @Test
    public void testListener(){
        EmailEvent emailEvent = new EmailEvent("object", "172572575@qq.com", "###listener");
        webapplicationcontext.publishEvent(emailEvent);
    }
}

监听器通过@Component注解进行实例化,并在onApplicationEvent中打印相关信息: 执行测试类,可以看到

在这里插入图片描述

至此,便完成了一个自定义事件及监听类的实现和实例化。

特别注意:不管是内置监听还是外部自定义监听一定要把实现ApplicationListener的类定义成一个bean才行,可以通过注解@Component或者在bean.xml中定义来实现。

也就是说通过注册为bean,才能实现事件的绑定。

到此这篇关于Spring中ApplicationListener的使用解析的文章就介绍到这了,更多相关ApplicationListener的使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文