Spring 事件监听

快速入门
在spring中只要实现 ApplicationListener 接口 实现 onApplicationEvent 方法。即可完成监听事件, 在event方法中 因为是监听所有方法 ,在ioc容器中默认会初始化监听器 外加5个本省具有的方法:下面代码演示 几个常用的系统默认的方法,如果要实现自定义事件类监听,那么 instanof 判定即可 后面的动物类演示一个自定义的事件
总结步骤:

  1. 实现ApplicationListener<ApplicationEvent> 并且对需要事件监听
  2. 实现ApplicationContextAware 并发布 事件
    3.构建事件类
    4.配置xml

同理要使用,还要配置 bean

       <!-- 监听器 -->
    <bean id="mylistener" class="listener.myListener"></bean>

package listener;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;

public class myListener implements ApplicationListener<ApplicationEvent>{

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ContextStartedEvent) {
             System.out.println("开始执行");
        } else if (event instanceof ContextClosedEvent) {
            System.out.println("结束执行");
        }else if (event instanceof ContextRefreshedEvent) {
            System.out.println("刷新事件");
        }       
    }

}

、。- 动物类

package entiy;

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

public class Animal implements ApplicationContextAware{
    
    private ApplicationContext ac;

    public void speak(){
        //必须通过ApplicationContext发布事件
        ac.publishEvent(new AnimalSpeakEvent(ac));
    }

    @Override
    public void setApplicationContext(ApplicationContext arg0)
            throws BeansException {
          this.ac = arg0;       
    }
}

package entiy;

import org.springframework.context.ApplicationEvent;

public class AnimalSpeakEvent extends ApplicationEvent {


    public String getAnimalEvent(){
        return "动物事情";
    }
    
    
    public AnimalSpeakEvent(Object source) {
        super(source);
    }
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

}



xml 中配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">
        <!-- 监听器 -->
    <bean id="mylistener" class="listener.myListener"></bean>
    <bean id="Animal" class="entiy.Animal"></bean>
    <bean id="jdk" class="services.servicesimpl" autowire="byName"> <property name="idao" ref="IDaoImpl"></property></bean>
    <bean id="IDaoImpl" class="basedao.impl.IDaoImpl"></bean>
</beans>

只要在容器中 调用了这个方法,就会触发监听器

        Animal animal = (Animal) context.getBean("Animal");
        animal.speak();
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,973评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,466评论 25 708
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,970评论 6 342
  • 小强油画阅读 245评论 1 0
  • 姓名:陈权 公司:青柠养车 【知~学习】 《财富自由》音频打卡第6天 《原则》音频打卡第5天 《轻课口语》打卡第4...
    水青柠阅读 137评论 0 0