spring中怎么监听ApplicationEvent事件现

发布时间:2021-06-21 16:48:12 作者:Leah
来源:亿速云 阅读:154

这篇文章将为大家详细讲解有关spring中怎么监听ApplicationEvent事件现,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

原理:ApplicationContextAware接口提供了publishEvent方法,实现了Observe(观察者)设计模式的传播机制,实现了对bean的传播。通过ApplicationContextAware我们可以把系统中所有ApplicationEvent传播给系统中所有的ApplicationListener。

1、直接上代码
2、定义自己的监听器(负责处理自己的监听事件)
3、定义一个bean触发监听事件
4、测试
package com.test.eventListener;

import org.springframework.context.ApplicationEvent;

/**
 * [@author](https://my.oschina.net/arthor) admin
 * [@date](https://my.oschina.net/u/2504391) 2018/5/17 17:37
 * 新建StudentAddEvent类,实现抽象类org.springframework.context.ApplicationEvent
 * StudentAddEvent类中需要实现自己的构造函数
 *  增加学生监听事件
 */
public class StudentAddEvent extends ApplicationEvent {

    private static final long serialVersionUID = 20L;

    /**
     * 学生姓名
     */
    private String name;

    /**
     * [@param](https://my.oschina.net/u/2303379) source
     */
    public StudentAddEvent(Object source, String name) {
        super(source);
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
package com.test.eventListener;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * [@author](https://my.oschina.net/arthor) admin
 * 新建StudentAddListener类,实现接口org.springframework.context.ApplicationListener中的onApplicationEvent方法,
 * 在该方法中只处理StudentAddEvent类型的ApplicationEvent事件
 * 定义StudentAddListener监听器
 */
[@Component](https://my.oschina.net/u/3907912)
public class StudentAddListener implements ApplicationListener {

    public void onApplicationEvent(ApplicationEvent event) {
        // 1.判断是否是增加学生对象的事件
        if (!(event instanceof StudentAddEvent)) {
            return;
        }

        // 2.是增加学生事件的对象,进行逻辑处理,比如记日志、积分等
        StudentAddEvent studentAddEvent = (StudentAddEvent) event;
        System.out.println("增加了学生:" + studentAddEvent.getName());
    }
}
package com.test.eventListener;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author admin
 * 定义StudentAddBean触发StudentAddEvent事件
 * 新建StudentAddBean类,实现接口 org.springframework.context.ApplicationContextAware中的setApplicationContext方法,
 * 在构造bean的时候注入Spring的上下文对象,以便通过Spring上下文对象的publishEvent方法来触发StudentAddEvent事件
 */
@Component
public class StudentAddBean implements ApplicationContextAware {
    /**
     * 定义Spring上下文对象
     */
    private ApplicationContext applicationContext = null;

    /*
     * (non-Javadoc)
     *
     * @see
     * org.springframework.context.ApplicationContextAware#setApplicationContext
     * (org.springframework.context.ApplicationContext)
     */
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        this.applicationContext = applicationContext;

    }

    /**
     * 增加一个学生
     *
     * @param studentName
     */
    public void addStudent(String studentName) {
        // 1.构造一个增加学生的事件
        StudentAddEvent aStudentEvent = new StudentAddEvent(
                applicationContext, studentName);
        // 2.触发增加学生事件
        applicationContext.publishEvent(aStudentEvent);
    }

}
package com.test.eventListener;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author admin
 * ApplicationContext在运行期会自动检测到所有实现了ApplicationListener的bean对象,并将其作为事件接收对象。
 * 当ApplicationContext的publishEvent方法被触发时,每个实现了ApplicationListener接口的bean都会收到ApplicationEvent对象,
 * 每个ApplicationListener可根据事件类型只接收处理自己感兴趣的事件,比如上面的StudentAddListener只接收StudentAddEvent事件。
 */
public class EventListenerTest {
    public static void main(String[] args) {
        String[] xmlConfig = new String[] { "spring/spring.xml" };
        // 使用ApplicationContext来初始化系统
        ApplicationContext context = new ClassPathXmlApplicationContext(xmlConfig);
        StudentAddBean studentBean = (StudentAddBean) context.getBean("studentAddBean");
        studentBean.addStudent("张三");
        studentBean.addStudent("李四");
    }
}

关于spring中怎么监听ApplicationEvent事件现就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. Spring架构揭秘-事件监听机制
  2. SpringBoot如何发布ApplicationEventPublisher和监听ApplicationEvent事件

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

spring applicationevent

上一篇:peewee中怎么动态添加model属性

下一篇:tomcat8中怎么绑定两个https域名

相关阅读

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

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