Spring事件发布

Spring中事件模型的实现。
事件类:需要继承ApplicationEvent类。
发送者:需要实现ApplicationEventPublisherAware接口。同时需要托管给容器。
接受者:需要实现ApplicationListener接口。同时需要托管给容器。

发送者调用publishEvent发送事件,接受者收到事件后进行处理

需要注意的是Spring3.0后:

对于接受者ApplicationListener<E extends ApplicationEvent>,多了泛型,泛型的类型决定了要关注的事件。

因为发送的事件,所有的接受者都可以收到。现在可以根据泛型进行过滤了。

如果泛型直接指定为ApplicationEvent,则接收所有事件,包括容器自身的一些事件。

/**事件 
 * @author ibm
 *
 */
public class MyEvent extends ApplicationEvent {

    public MyEvent(Object source) {/*source为发布者的引用,在发布者内传入this*/
        super(source);
    }
}
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;

/**发送者
 * @author ibm
 *
 */
@Component
public class Teacher implements ApplicationEventPublisherAware{
    private ApplicationEventPublisher applicationEventPublisher;

    @Override
    public void setApplicationEventPublisher( ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher=applicationEventPublisher;
    }

    /**发布一个事件
     * @param msg
     */
    public void notifyListener(String msg){
        applicationEventPublisher.publishEvent(new MyEvent(msg));/*这里一般为new MyEvent(this,msg)   MyEvent类应该增加一个构造器MyEvent(Object source,String msg)*/  
    }
    
}
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**事件接受者,泛型为ApplicationEvent则接受所有事件,包括容器自身的事件
 * @author ibm
 *
 */
@Component
public class MyListener implements ApplicationListener<ApplicationEvent>{

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        System.out.println("event="+event.getSource());
    }
}
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringEventTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        AbstractApplicationContext appCtx = new ClassPathXmlApplicationContext("beans.xml");
        Teacher t = (Teacher)appCtx.getBean("teacher");
        t.notifyListener("hello world");//发送一条消息
        appCtx.close();
    }
}

配置文件beans.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"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/aop   
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <context:component-scan base-package="com.zk">
    </context:component-scan>
</beans>
原文地址:https://www.cnblogs.com/beenupper/p/2797303.html