Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法

什么是ApplicationContext? 
它是Spring的核心,Context我们通常解释为上下文环境,但是理解成容器会更好些。 
ApplicationContext则是应用的容器。

Spring把Bean(object)放在容器中,需要用就通过get方法取出来。

 

ApplicationEvent

是个抽象类,里面只有一个构造函数和一个长整型的timestamp。

ApplicationListener

是一个接口,里面只有一个onApplicationEvent方法。

所以自己的类在实现该接口的时候,要实装该方法。


如果在上下文中部署一个实现了ApplicationListener接口的bean,

那么每当在一个ApplicationEvent发布到 ApplicationContext时,
这个bean得到通知。其实这就是标准的Oberver设计模式。

下面给出例子:

首先创建一个ApplicationEvent实现类:

package com.spring.event;

import org.springframework.context.ApplicationEvent;

/**
 * @Package Name : ${PACKAG_NAME}
 * @Author : dongfucai@meituan.com
 * @Creation Date : 2018年07月13日下午3:46
 * @Function : todo
 */
public class EmailEvent extends ApplicationEvent {

    private static final long serialVersionUID=1L;
    public String address;
    public String text;

    public EmailEvent(Object source){
        super(source);
    }

    public EmailEvent(Object source,String address,String text){
        super(source);
        this.address=address;
        this.text=text;
    }

    public void print(){
        System.out.println("hello spring event!");
    }


}

给出监听器:

package com.spring.event;

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

/**
 * @Package Name : ${PACKAG_NAME}
 * @Author : dongfucai@meituan.com
 * @Creation Date : 2018年07月13日下午3:52
 * @Function : todo
 */



/**
 * java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例。instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例。
 *
 * 用法:
 * result = object instanceof class
 * 参数:
 * Result:布尔类型。
 * Object:必选项。任意对象表达式。
 * Class:必选项。任意已定义的对象类。
 * 说明:
 * 如果 object 是 class 的一个实例,则 instanceof 运算符返回 true。如果 object 不是指定类的一个实例,或者 object 是 null,则返回 false。
 */
public class EmailListener implements ApplicationListener {


    @Override
    public void onApplicationEvent(ApplicationEvent event){
        if (event instanceof EmailEvent){

            EmailEvent emailEvent = (EmailEvent)event;
            emailEvent.print();
            System.out.println(emailEvent.getSource());
            System.out.println(emailEvent.address);
            System.out.println(emailEvent.text);

        }
    }

}

applicationContext.xml文件配置:

<bean id="emailListener" class="com.spring.event.EmailListener"></bean>

测试类:

package com.spring.event;

import com.oplog.impl.AServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Package Name : ${PACKAG_NAME}
 * @Author : dongfucai@meituan.com
 * @Creation Date : 2018年07月13日下午4:34
 * @Function : todo
 */

/**
 *
 如果在上下文中部署一个实现了ApplicationListener接口的bean,

 那么每当在一个ApplicationEvent发布到 ApplicationContext时,
 这个bean得到通知。其实这就是标准的Oberver设计模式。


 */
public class ListerTest {

    public static void main(String[] args) {


        ApplicationContext context  = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

        EmailEvent event = new EmailEvent("hello","boylmx@163.com","this is a email text!");

        context.publishEvent(event);



    }

}
测试结果:

hello spring event!
hello
boylmx@163.com
this is a email text!


Process finished with exit code 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ApplicationListenerSpring Framework 中的一个接口,用于监听 Spring 应用程序中发生的事件。它定义了一组回调方法,当应用程序中发生特定事件时,这些方法将被调用。通过实现 ApplicationListener 接口并注册为 Spring Bean,我们可以在应用程序中监听和响应这些事件。 以下是一个 ApplicationListener 的使用案例: ```java @Component public class MyApplicationListener implements ApplicationListener<ApplicationEvent> { @Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ContextRefreshedEvent) { // 应用程序上下文已刷新事件 // 在此处添加自定义逻辑 } else if (event instanceof ContextStartedEvent) { // 应用程序上下文已启动事件 // 在此处添加自定义逻辑 } else if (event instanceof ContextStoppedEvent) { // 应用程序上下文已停止事件 // 在此处添加自定义逻辑 } else if (event instanceof ContextClosedEvent) { // 应用程序上下文已关闭事件 // 在此处添加自定义逻辑 } else { // 其他事件 } } } ``` 在这个例子中,我们创建了一个名为 MyApplicationListenerSpring Bean,并实现了 ApplicationListener 接口。我们定义了一个回调方法 onApplicationEvent,它将在任何应用程序事件发生时调用。通过检查事件的类型,我们可以执行不同的自定义逻辑。在这个例子中,我们处理了四个不同的应用程序事件:ContextRefreshedEvent、ContextStartedEvent、ContextStoppedEvent 和 ContextClosedEvent。这些事件分别表示应用程序上下文已刷新、已启动、已停止和已关闭。 要在应用程序中使用 ApplicationListener,我们需要将实现了 ApplicationListener 接口的类注册为 Spring Bean。这可以通过在类上添加 @Component 注解来实现。 总的来说,ApplicationListener 为我们提供了一种在 Spring 应用程序中监听和响应事件的方式,是一个非常有用的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值