美文网首页
Spring 事件监听

Spring 事件监听

作者: LinkedIn | 来源:发表于2017-05-28 20:41 被阅读0次

快速入门
在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 事件监听

      本文链接:https://www.haomeiwen.com/subject/nwbhfxtx.html